From a71c3fe43a448dcc21592654111059759b8010a1 Mon Sep 17 00:00:00 2001 From: Fredrik Strand Oseberg Date: Mon, 18 Sep 2023 16:31:42 +0200 Subject: [PATCH] feat: add ids to scheduled jobs (#4764) * Adds an ID to scheduled jobs so that they are easier to identify in the future --- src/lib/services/index.ts | 9 +++++++ src/lib/services/scheduler-service.test.ts | 30 +++++++++++----------- src/lib/services/scheduler-service.ts | 7 +++-- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/lib/services/index.ts b/src/lib/services/index.ts index bbb483741b..a58ff3e6f0 100644 --- a/src/lib/services/index.ts +++ b/src/lib/services/index.ts @@ -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', ); }; diff --git a/src/lib/services/scheduler-service.test.ts b/src/lib/services/scheduler-service.test.ts index 18afad30b1..0a03a7b0f8 100644 --- a/src/lib/services/scheduler-service.test.ts +++ b/src/lib/services/scheduler-service.test.ts @@ -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'], ]); }); diff --git a/src/lib/services/scheduler-service.ts b/src/lib/services/scheduler-service.ts index 2b555b86f0..c2f8ae0478 100644 --- a/src/lib/services/scheduler-service.ts +++ b/src/lib/services/scheduler-service.ts @@ -17,6 +17,7 @@ export class SchedulerService { async schedule( scheduledFunction: () => void, timeMs: number, + id: string, ): Promise { 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}`); } }