From ab5da2116ae886b78534b2168f787a973cd76ea8 Mon Sep 17 00:00:00 2001 From: sjaanus Date: Mon, 30 Jan 2023 13:01:44 +0200 Subject: [PATCH] Remove timeout from store (#3018) --- src/lib/db/client-instance-store.ts | 7 ++----- src/lib/services/client-metrics/instance-service.ts | 4 ++++ src/lib/services/index.ts | 13 ++++++++++--- src/lib/types/stores/client-instance-store.ts | 1 + src/test/fixtures/fake-client-instance-store.ts | 4 ++++ 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/lib/db/client-instance-store.ts b/src/lib/db/client-instance-store.ts index a90c77b041..515544d9b6 100644 --- a/src/lib/db/client-instance-store.ts +++ b/src/lib/db/client-instance-store.ts @@ -5,7 +5,7 @@ import { IClientInstanceStore, INewClientInstance, } from '../types/stores/client-instance-store'; -import { hoursToMilliseconds, subDays } from 'date-fns'; +import { subDays } from 'date-fns'; import Timeout = NodeJS.Timeout; import { Db } from './db'; @@ -62,12 +62,9 @@ export default class ClientInstanceStore implements IClientInstanceStore { store: 'instance', action, }); - const clearer = () => this._removeInstancesOlderThanTwoDays(); - setTimeout(clearer, 10).unref(); - this.timer = setInterval(clearer, hoursToMilliseconds(24)).unref(); } - async _removeInstancesOlderThanTwoDays(): Promise { + async removeInstancesOlderThanTwoDays(): Promise { const rows = await this.db(TABLE) .whereRaw("created_at < now() - interval '2 days'") .del(); diff --git a/src/lib/services/client-metrics/instance-service.ts b/src/lib/services/client-metrics/instance-service.ts index 5b5f9e3979..a0d8655fa8 100644 --- a/src/lib/services/client-metrics/instance-service.ts +++ b/src/lib/services/client-metrics/instance-service.ts @@ -253,6 +253,10 @@ export default class ClientInstanceService { return metrics; } + async removeInstancesOlderThanTwoDays(): Promise { + return this.clientInstanceStore.removeInstancesOlderThanTwoDays(); + } + destroy(): void { this.timers.forEach(clearInterval); } diff --git a/src/lib/services/index.ts b/src/lib/services/index.ts index 60ea30dc16..db26ad5961 100644 --- a/src/lib/services/index.ts +++ b/src/lib/services/index.ts @@ -40,7 +40,7 @@ import { InstanceStatsService } from './instance-stats-service'; import { FavoritesService } from './favorites-service'; import MaintenanceService from './maintenance-service'; import ExportImportService from './export-import-service'; -import { minutesToMilliseconds } from 'date-fns'; +import { hoursToMilliseconds, minutesToMilliseconds } from 'date-fns'; import { AccountService } from './account-service'; import { SchedulerService } from './scheduler-service'; @@ -52,6 +52,7 @@ export const scheduleServices = ( schedulerService, apiTokenService, instanceStatsService, + clientInstanceService, projectService, } = services; @@ -70,11 +71,17 @@ export const scheduleServices = ( minutesToMilliseconds(5), ); + schedulerService.schedule( + clientInstanceService.removeInstancesOlderThanTwoDays.bind( + clientInstanceService, + ), + hoursToMilliseconds(24), + ); + if (config.flagResolver.isEnabled('projectStatusApi')) { - const ONE_DAY = 1440; schedulerService.schedule( projectService.statusJob.bind(projectService), - minutesToMilliseconds(ONE_DAY), + hoursToMilliseconds(24), ); } }; diff --git a/src/lib/types/stores/client-instance-store.ts b/src/lib/types/stores/client-instance-store.ts index 3a1d32a0a1..bae30dc46f 100644 --- a/src/lib/types/stores/client-instance-store.ts +++ b/src/lib/types/stores/client-instance-store.ts @@ -24,4 +24,5 @@ export interface IClientInstanceStore getDistinctApplications(): Promise; getDistinctApplicationsCount(daysBefore?: number): Promise; deleteForApplication(appName: string): Promise; + removeInstancesOlderThanTwoDays(): Promise; } diff --git a/src/test/fixtures/fake-client-instance-store.ts b/src/test/fixtures/fake-client-instance-store.ts index 614872d95b..6d7a0cd296 100644 --- a/src/test/fixtures/fake-client-instance-store.ts +++ b/src/test/fixtures/fake-client-instance-store.ts @@ -82,4 +82,8 @@ export default class FakeClientInstanceStore implements IClientInstanceStore { async insert(details: INewClientInstance): Promise { this.instances.push({ createdAt: new Date(), ...details }); } + + removeInstancesOlderThanTwoDays(): Promise { + return Promise.resolve(undefined); + } }