From 42bc2817be1d8e50a4d3d12fc74767f7920bacd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BE=E3=81=A3=E3=81=A1=E3=82=83=E3=81=A8=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E3=82=85?= <17376330+u1-liquid@users.noreply.github.com> Date: Sun, 29 Oct 2023 09:40:48 +0900 Subject: [PATCH] =?UTF-8?q?=E9=A0=BB=E7=B9=81=E3=81=ABAP=E3=83=AA=E3=82=AF?= =?UTF-8?q?=E3=82=A8=E3=82=B9=E3=83=88=E3=81=8C=E7=99=BA=E7=94=9F=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=82=B5=E3=83=BC=E3=83=90=E3=83=BC=E3=81=AB=E5=AF=BE?= =?UTF-8?q?=E3=81=97=E3=81=A6fetchInstanceMetadata=E6=89=8B=E5=8B=95?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E3=81=8C=E7=84=A1=E8=A6=96=E3=81=95=E3=82=8C?= =?UTF-8?q?=E3=82=8B=E5=95=8F=E9=A1=8C=E3=82=92=E4=BF=AE=E6=AD=A3=20(Missk?= =?UTF-8?q?eyIO#194)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/backend/src/core/FetchInstanceMetadataService.ts | 8 ++++---- .../backend/test/unit/FetchInstanceMetadataService.ts | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) 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; }