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

feat: add ids to scheduled jobs (#4764)

* Adds an ID to scheduled jobs so that they are easier to identify in
the future
This commit is contained in:
Fredrik Strand Oseberg 2023-09-18 16:31:42 +02:00 committed by GitHub
parent 2c55e929ae
commit a71c3fe43a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 17 deletions

View File

@ -93,16 +93,19 @@ export const scheduleServices = async (
schedulerService.schedule(
apiTokenService.fetchActiveTokens.bind(apiTokenService),
minutesToMilliseconds(1),
'fetchActiveTokens',
);
schedulerService.schedule(
apiTokenService.updateLastSeen.bind(apiTokenService),
minutesToMilliseconds(3),
'updateLastSeen',
);
schedulerService.schedule(
instanceStatsService.refreshStatsSnapshot.bind(instanceStatsService),
minutesToMilliseconds(5),
'refreshStatsSnapshot',
);
schedulerService.schedule(
@ -110,16 +113,19 @@ export const scheduleServices = async (
clientInstanceService,
),
hoursToMilliseconds(24),
'removeInstancesOlderThanTwoDays',
);
schedulerService.schedule(
projectService.statusJob.bind(projectService),
hoursToMilliseconds(24),
'statusJob',
);
schedulerService.schedule(
projectHealthService.setHealthRating.bind(projectHealthService),
hoursToMilliseconds(1),
'setHealthRating',
);
schedulerService.schedule(
@ -127,6 +133,7 @@ export const scheduleServices = async (
configurationRevisionService,
),
secondsToMilliseconds(1),
'updateMaxRevisionId',
);
schedulerService.schedule(
@ -134,6 +141,7 @@ export const scheduleServices = async (
eventAnnouncerService,
),
secondsToMilliseconds(1),
'publishUnannouncedEvents',
);
schedulerService.schedule(
@ -141,6 +149,7 @@ export const scheduleServices = async (
featureToggleService,
),
minutesToMilliseconds(1),
'updatePotentiallyStaleFeatures',
);
};

View File

@ -26,7 +26,7 @@ test('Schedules job immediately', async () => {
const schedulerService = new SchedulerService(logger);
const job = jest.fn();
schedulerService.schedule(job, 10);
schedulerService.schedule(job, 10, 'test-id');
expect(job).toBeCalledTimes(1);
schedulerService.stop();
@ -38,7 +38,7 @@ test('Does not schedule job immediately when paused', async () => {
const job = jest.fn();
schedulerService.pause();
schedulerService.schedule(job, 10);
schedulerService.schedule(job, 10, 'test-id-2');
expect(job).toBeCalledTimes(0);
schedulerService.stop();
@ -49,7 +49,7 @@ test('Can schedule a single regular job', async () => {
const schedulerService = new SchedulerService(logger);
const job = jest.fn();
schedulerService.schedule(job, 50);
schedulerService.schedule(job, 50, 'test-id-3');
await ms(75);
expect(job).toBeCalledTimes(2);
@ -62,7 +62,7 @@ test('Scheduled job ignored in a paused mode', async () => {
const job = jest.fn();
schedulerService.pause();
schedulerService.schedule(job, 50);
schedulerService.schedule(job, 50, 'test-id-4');
await ms(75);
expect(job).toBeCalledTimes(0);
@ -75,7 +75,7 @@ test('Can resume paused job', async () => {
const job = jest.fn();
schedulerService.pause();
schedulerService.schedule(job, 50);
schedulerService.schedule(job, 50, 'test-id-5');
schedulerService.resume();
await ms(75);
@ -89,8 +89,8 @@ test('Can schedule multiple jobs at the same interval', async () => {
const job = jest.fn();
const anotherJob = jest.fn();
schedulerService.schedule(job, 50);
schedulerService.schedule(anotherJob, 50);
schedulerService.schedule(job, 50, 'test-id-6');
schedulerService.schedule(anotherJob, 50, 'test-id-7');
await ms(75);
expect(job).toBeCalledTimes(2);
@ -104,8 +104,8 @@ test('Can schedule multiple jobs at the different intervals', async () => {
const job = jest.fn();
const anotherJob = jest.fn();
schedulerService.schedule(job, 100);
schedulerService.schedule(anotherJob, 200);
schedulerService.schedule(job, 100, 'test-id-8');
schedulerService.schedule(anotherJob, 200, 'test-id-9');
await ms(250);
expect(job).toBeCalledTimes(3);
@ -120,13 +120,13 @@ test('Can handle crash of a async job', async () => {
await Promise.reject('async reason');
};
schedulerService.schedule(job, 50);
schedulerService.schedule(job, 50, 'test-id-10');
await ms(75);
schedulerService.stop();
expect(getRecords()).toEqual([
['scheduled job failed', 'async reason'],
['scheduled job failed', 'async reason'],
['scheduled job failed | id: test-id-10 | async reason'],
['scheduled job failed | id: test-id-10 | async reason'],
]);
});
@ -137,12 +137,12 @@ test('Can handle crash of a sync job', async () => {
throw new Error('sync reason');
};
schedulerService.schedule(job, 50);
schedulerService.schedule(job, 50, 'test-id-11');
await ms(75);
schedulerService.stop();
expect(getRecords()).toEqual([
['scheduled job failed', new Error('sync reason')],
['scheduled job failed', new Error('sync reason')],
['scheduled job failed | id: test-id-11 | Error: sync reason'],
['scheduled job failed | id: test-id-11 | Error: sync reason'],
]);
});

View File

@ -17,6 +17,7 @@ export class SchedulerService {
async schedule(
scheduledFunction: () => void,
timeMs: number,
id: string,
): Promise<void> {
this.intervalIds.push(
setInterval(async () => {
@ -25,7 +26,9 @@ export class SchedulerService {
await scheduledFunction();
}
} catch (e) {
this.logger.error('scheduled job failed', e);
this.logger.error(
`scheduled job failed | id: ${id} | ${e}`,
);
}
}, timeMs).unref(),
);
@ -34,7 +37,7 @@ export class SchedulerService {
await scheduledFunction();
}
} catch (e) {
this.logger.error('scheduled job failed', e);
this.logger.error(`scheduled job failed | id: ${id} | ${e}`);
}
}