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

refactor: separate scheduling from DI (#3017)

This commit is contained in:
Mateusz Kwasniewski 2023-01-30 11:13:17 +01:00 committed by GitHub
parent 1e35c30fde
commit d9110b6ce5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 27 deletions

View File

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

View File

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

View File

@ -1,4 +1,4 @@
import SchedulerService from './scheduler-service';
import { SchedulerService } from './scheduler-service';
function ms(timeMs) {
return new Promise((resolve) => setTimeout(resolve, timeMs));

View File

@ -1,6 +1,6 @@
import { Logger, LogProvider } from '../logger';
export default class SchedulerService {
export class SchedulerService {
private intervalIds: NodeJS.Timer[] = [];
private logger: Logger;

View File

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