1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-06-27 01:19:00 +02:00
unleash.unleash/src/lib/features/scheduler/schedule-services.ts
Gastón Fournier fa3352786a
chore: reimplementation of app stats (#6155)
## About the changes
App stats is mainly used to cap the number of applications reported to
Unleash based on the last 7 days information:
cc2ccb1134/src/lib/middleware/response-time-metrics.ts (L24-L28)

Instead of getting all stats, just calculate appCount statistics

Use scheduler service instead of setInterval
2024-02-08 17:15:42 +01:00

168 lines
4.5 KiB
TypeScript

import {
hoursToMilliseconds,
minutesToMilliseconds,
secondsToMilliseconds,
} from 'date-fns';
import { IUnleashServices } from '../../server-impl';
/**
* Schedules service methods.
*
* In order to promote runtime control, you should **not use** a flagResolver inside this method. Instead, implement your flag usage inside the scheduled methods themselves.
* @param services
*/
export const scheduleServices = async (
services: IUnleashServices,
): Promise<void> => {
const {
accountService,
schedulerService,
apiTokenService,
instanceStatsService,
clientInstanceService,
projectService,
projectHealthService,
configurationRevisionService,
eventAnnouncerService,
featureToggleService,
eventService,
versionService,
lastSeenService,
proxyService,
clientMetricsServiceV2,
} = services;
schedulerService.schedule(
lastSeenService.cleanLastSeen.bind(lastSeenService),
hoursToMilliseconds(1),
'cleanLastSeen',
);
schedulerService.schedule(
lastSeenService.store.bind(lastSeenService),
secondsToMilliseconds(30),
'storeLastSeen',
);
schedulerService.schedule(
apiTokenService.fetchActiveTokens.bind(apiTokenService),
minutesToMilliseconds(1),
'fetchActiveTokens',
);
schedulerService.schedule(
apiTokenService.updateLastSeen.bind(apiTokenService),
minutesToMilliseconds(3),
'updateLastSeen',
);
schedulerService.schedule(
instanceStatsService.refreshAppCountSnapshot.bind(instanceStatsService),
minutesToMilliseconds(5),
'refreshAppCountSnapshot',
);
schedulerService.schedule(
clientInstanceService.removeInstancesOlderThanTwoDays.bind(
clientInstanceService,
),
hoursToMilliseconds(24),
'removeInstancesOlderThanTwoDays',
);
schedulerService.schedule(
clientInstanceService.bulkAdd.bind(clientInstanceService),
secondsToMilliseconds(5),
'bulkAddInstances',
);
schedulerService.schedule(
clientInstanceService.announceUnannounced.bind(clientInstanceService),
minutesToMilliseconds(5),
'announceUnannounced',
);
schedulerService.schedule(
projectService.statusJob.bind(projectService),
hoursToMilliseconds(24),
'statusJob',
);
schedulerService.schedule(
projectHealthService.setHealthRating.bind(projectHealthService),
hoursToMilliseconds(1),
'setHealthRating',
);
schedulerService.schedule(
configurationRevisionService.updateMaxRevisionId.bind(
configurationRevisionService,
),
secondsToMilliseconds(1),
'updateMaxRevisionId',
);
schedulerService.schedule(
eventAnnouncerService.publishUnannouncedEvents.bind(
eventAnnouncerService,
),
secondsToMilliseconds(1),
'publishUnannouncedEvents',
);
schedulerService.schedule(
featureToggleService.updatePotentiallyStaleFeatures.bind(
featureToggleService,
),
minutesToMilliseconds(1),
'updatePotentiallyStaleFeatures',
);
schedulerService.schedule(
versionService.checkLatestVersion.bind(versionService),
hoursToMilliseconds(48),
'checkLatestVersion',
);
schedulerService.schedule(
proxyService.fetchFrontendSettings.bind(proxyService),
minutesToMilliseconds(2),
'fetchFrontendSettings',
);
schedulerService.schedule(
() => {
clientMetricsServiceV2.bulkAdd().catch(console.error);
},
secondsToMilliseconds(5),
'bulkAddMetrics',
);
schedulerService.schedule(
() => {
clientMetricsServiceV2.clearMetrics(48).catch(console.error);
},
hoursToMilliseconds(12),
'clearMetrics',
);
schedulerService.schedule(
accountService.updateLastSeen.bind(accountService),
minutesToMilliseconds(3),
'updateAccountLastSeen',
);
schedulerService.schedule(
eventService.setEventCreatedByUserId.bind(eventService),
minutesToMilliseconds(2),
'setEventCreatedByUserId',
);
schedulerService.schedule(
featureToggleService.setFeatureCreatedByUserIdFromEvents.bind(
featureToggleService,
),
minutesToMilliseconds(15),
'setFeatureCreatedByUserIdFromEvents',
);
};