1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

Remove timeout from store (#3018)

This commit is contained in:
sjaanus 2023-01-30 13:01:44 +02:00 committed by GitHub
parent 83aa4cc91b
commit ab5da2116a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 8 deletions

View File

@ -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<void> {
async removeInstancesOlderThanTwoDays(): Promise<void> {
const rows = await this.db(TABLE)
.whereRaw("created_at < now() - interval '2 days'")
.del();

View File

@ -253,6 +253,10 @@ export default class ClientInstanceService {
return metrics;
}
async removeInstancesOlderThanTwoDays(): Promise<void> {
return this.clientInstanceStore.removeInstancesOlderThanTwoDays();
}
destroy(): void {
this.timers.forEach(clearInterval);
}

View File

@ -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),
);
}
};

View File

@ -24,4 +24,5 @@ export interface IClientInstanceStore
getDistinctApplications(): Promise<string[]>;
getDistinctApplicationsCount(daysBefore?: number): Promise<number>;
deleteForApplication(appName: string): Promise<void>;
removeInstancesOlderThanTwoDays(): Promise<void>;
}

View File

@ -82,4 +82,8 @@ export default class FakeClientInstanceStore implements IClientInstanceStore {
async insert(details: INewClientInstance): Promise<void> {
this.instances.push({ createdAt: new Date(), ...details });
}
removeInstancesOlderThanTwoDays(): Promise<void> {
return Promise.resolve(undefined);
}
}