diff --git a/packages/backend/src/core/FetchInstanceMetadataService.ts b/packages/backend/src/core/FetchInstanceMetadataService.ts index 24efd91c70..3f55a12f6e 100644 --- a/packages/backend/src/core/FetchInstanceMetadataService.ts +++ b/packages/backend/src/core/FetchInstanceMetadataService.ts @@ -52,20 +52,20 @@ export class FetchInstanceMetadataService { @bindThis public async tryLock(host: string): Promise { - const mutex = await this.redisClient.set(`fetchInstanceMetadata:mutex:${host}`, '1', 'EX', 60 * 5, 'NX', 'GET'); - return mutex !== '1'; + const mutex = await this.redisClient.set(`fetchInstanceMetadata:mutex:${host}`, Date.now(), 'EX', 60 * 5, 'NX'); + return mutex !== null; } @bindThis public unlock(host: string): Promise { - return this.redisClient.del(`fetchInstanceMetadata:mutex:${host}`); + return this.redisClient.unlink(`fetchInstanceMetadata:mutex:${host}`); } @bindThis public async fetchInstanceMetadata(instance: MiInstance, force = false): Promise { const host = instance.host; // Acquire mutex to ensure no parallel runs - if (!await this.tryLock(host)) return; + if (!await this.tryLock(host) && !force) return; try { if (!force) { const _instance = await this.federatedInstanceService.fetch(host); diff --git a/packages/backend/test/unit/FetchInstanceMetadataService.ts b/packages/backend/test/unit/FetchInstanceMetadataService.ts index 22ce023216..9850aa181d 100644 --- a/packages/backend/test/unit/FetchInstanceMetadataService.ts +++ b/packages/backend/test/unit/FetchInstanceMetadataService.ts @@ -23,9 +23,10 @@ import type { MockFunctionMetadata } from 'jest-mock'; function mockRedis() { const hash = {}; const set = jest.fn((key, value) => { - const ret = hash[key]; + // このテストで呼び出すSETにはNXオプションが付いてる + if (hash[key]) return null; hash[key] = value; - return ret; + return 'OK'; }); return set; }