From ffbdfa912388c705ac288b68f45aa31fc9e75c40 Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Wed, 27 Nov 2024 23:19:14 -0500 Subject: [PATCH] Synchronize server startup This prevents an edge case where the server begins processing inbound API / AP requests before any of the chart / management daemons are ready, potentially leading to incorrect chart statistics. --- packages/backend/src/boot/common.ts | 17 +++++++++-------- .../backend/src/daemons/QueueStatsService.ts | 4 ++-- .../backend/src/daemons/ServerStatsService.ts | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/backend/src/boot/common.ts b/packages/backend/src/boot/common.ts index ad59a55688..9ff1efb3c2 100644 --- a/packages/backend/src/boot/common.ts +++ b/packages/backend/src/boot/common.ts @@ -19,17 +19,18 @@ export async function server() { logger: new NestLogger(), }); - const serverService = app.get(ServerService); - await serverService.launch(); - if (process.env.NODE_ENV !== 'test') { - app.get(ChartManagementService).start(); + await app.get(ChartManagementService).start(); } if (!envOption.noDaemons) { - app.get(QueueStatsService).start(); - app.get(ServerStatsService).start(); + await app.get(QueueStatsService).start(); + await app.get(ServerStatsService).start(); } + // Start server last so the other services can register hooks first + const serverService = app.get(ServerService); + await serverService.launch(); + return app; } @@ -38,8 +39,8 @@ export async function jobQueue() { logger: new NestLogger(), }); - jobQueue.get(QueueProcessorService).start(); - jobQueue.get(ChartManagementService).start(); + await jobQueue.get(QueueProcessorService).start(); + await jobQueue.get(ChartManagementService).start(); return jobQueue; } diff --git a/packages/backend/src/daemons/QueueStatsService.ts b/packages/backend/src/daemons/QueueStatsService.ts index ede104b9fe..77fcd71337 100644 --- a/packages/backend/src/daemons/QueueStatsService.ts +++ b/packages/backend/src/daemons/QueueStatsService.ts @@ -33,7 +33,7 @@ export class QueueStatsService implements OnApplicationShutdown { * Report queue stats regularly */ @bindThis - public start(): void { + public async start(): Promise { const log = [] as any[]; ev.on('requestQueueStatsLog', x => { @@ -82,7 +82,7 @@ export class QueueStatsService implements OnApplicationShutdown { activeInboxJobs = 0; }; - tick(); + await tick(); this.intervalId = setInterval(tick, interval); } diff --git a/packages/backend/src/daemons/ServerStatsService.ts b/packages/backend/src/daemons/ServerStatsService.ts index 6e9d29dcbd..2ab087477a 100644 --- a/packages/backend/src/daemons/ServerStatsService.ts +++ b/packages/backend/src/daemons/ServerStatsService.ts @@ -68,7 +68,7 @@ export class ServerStatsService implements OnApplicationShutdown { if (log.length > 200) log.pop(); }; - tick(); + await tick(); this.intervalId = setInterval(tick, interval); }