From d9110b6ce5aa4195f63e90c50f0cdf374cbe71a9 Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Mon, 30 Jan 2023 11:13:17 +0100 Subject: [PATCH] refactor: separate scheduling from DI (#3017) --- src/lib/server-impl.ts | 3 +- src/lib/services/index.ts | 61 ++++++++++++++-------- src/lib/services/scheduler-service.test.ts | 2 +- src/lib/services/scheduler-service.ts | 2 +- src/lib/types/services.ts | 4 +- 5 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/lib/server-impl.ts b/src/lib/server-impl.ts index 6102620eaf..7e3b7d903d 100644 --- a/src/lib/server-impl.ts +++ b/src/lib/server-impl.ts @@ -5,7 +5,7 @@ import { migrateDb } from '../migrator'; import getApp from './app'; import { createMetricsMonitor } from './metrics'; import { createStores } from './db'; -import { createServices } from './services'; +import { createServices, scheduleServices } from './services'; import { createConfig } from './create-config'; import { addEventHook } from './event-hook'; import registerGracefulShutdown from './util/graceful-shutdown'; @@ -43,6 +43,7 @@ async function createApp( const db = createDb(config); const stores = createStores(config, db); const services = createServices(stores, config); + scheduleServices(services, config); const metricsMonitor = createMetricsMonitor(); const unleashSession = sessionDb(config, db); diff --git a/src/lib/services/index.ts b/src/lib/services/index.ts index 54c1adf673..60ea30dc16 100644 --- a/src/lib/services/index.ts +++ b/src/lib/services/index.ts @@ -40,9 +40,44 @@ import { InstanceStatsService } from './instance-stats-service'; import { FavoritesService } from './favorites-service'; import MaintenanceService from './maintenance-service'; import ExportImportService from './export-import-service'; -import SchedulerService from './scheduler-service'; import { minutesToMilliseconds } from 'date-fns'; import { AccountService } from './account-service'; +import { SchedulerService } from './scheduler-service'; + +export const scheduleServices = ( + services: IUnleashServices, + config: IUnleashConfig, +): void => { + const { + schedulerService, + apiTokenService, + instanceStatsService, + projectService, + } = services; + + schedulerService.schedule( + apiTokenService.fetchActiveTokens.bind(apiTokenService), + minutesToMilliseconds(1), + ); + + schedulerService.schedule( + apiTokenService.updateLastSeen.bind(apiTokenService), + minutesToMilliseconds(3), + ); + + schedulerService.schedule( + instanceStatsService.refreshStatsSnapshot.bind(instanceStatsService), + minutesToMilliseconds(5), + ); + + if (config.flagResolver.isEnabled('projectStatusApi')) { + const ONE_DAY = 1440; + schedulerService.schedule( + projectService.statusJob.bind(projectService), + minutesToMilliseconds(ONE_DAY), + ); + } +}; export const createServices = ( stores: IUnleashStores, @@ -146,28 +181,6 @@ export const createServices = ( ); const schedulerService = new SchedulerService(config.getLogger); - schedulerService.schedule( - apiTokenService.fetchActiveTokens.bind(apiTokenService), - minutesToMilliseconds(1), - ); - - schedulerService.schedule( - apiTokenService.updateLastSeen.bind(apiTokenService), - minutesToMilliseconds(3), - ); - - schedulerService.schedule( - instanceStatsService.refreshStatsSnapshot.bind(instanceStatsService), - minutesToMilliseconds(5), - ); - - if (config.flagResolver.isEnabled('projectStatusApi')) { - const ONE_DAY = 1440; - schedulerService.schedule( - projectService.statusJob.bind(projectService), - minutesToMilliseconds(ONE_DAY), - ); - } return { accessService, @@ -212,6 +225,7 @@ export const createServices = ( favoritesService, maintenanceService, exportImportService, + schedulerService, }; }; @@ -255,4 +269,5 @@ export { InstanceStatsService, FavoritesService, ExportImportService, + SchedulerService, }; diff --git a/src/lib/services/scheduler-service.test.ts b/src/lib/services/scheduler-service.test.ts index ec38ac36ee..1fcf656859 100644 --- a/src/lib/services/scheduler-service.test.ts +++ b/src/lib/services/scheduler-service.test.ts @@ -1,4 +1,4 @@ -import SchedulerService from './scheduler-service'; +import { SchedulerService } from './scheduler-service'; function ms(timeMs) { return new Promise((resolve) => setTimeout(resolve, timeMs)); diff --git a/src/lib/services/scheduler-service.ts b/src/lib/services/scheduler-service.ts index 64fe37a8a9..ec66225906 100644 --- a/src/lib/services/scheduler-service.ts +++ b/src/lib/services/scheduler-service.ts @@ -1,6 +1,6 @@ import { Logger, LogProvider } from '../logger'; -export default class SchedulerService { +export class SchedulerService { private intervalIds: NodeJS.Timer[] = []; private logger: Logger; diff --git a/src/lib/types/services.ts b/src/lib/types/services.ts index c8d595ad80..cd7a31a16f 100644 --- a/src/lib/types/services.ts +++ b/src/lib/types/services.ts @@ -35,10 +35,11 @@ import PatService from '../services/pat-service'; import { PublicSignupTokenService } from '../services/public-signup-token-service'; import { LastSeenService } from '../services/client-metrics/last-seen-service'; import { InstanceStatsService } from '../services/instance-stats-service'; -import { FavoritesService } from '../services'; +import { FavoritesService } from '../services/favorites-service'; import MaintenanceService from '../services/maintenance-service'; import ExportImportService from 'lib/services/export-import-service'; import { AccountService } from '../services/account-service'; +import { SchedulerService } from '../services/scheduler-service'; export interface IUnleashServices { accessService: AccessService; @@ -83,4 +84,5 @@ export interface IUnleashServices { favoritesService: FavoritesService; maintenanceService: MaintenanceService; exportImportService: ExportImportService; + schedulerService: SchedulerService; }