From 255e829be2c006fe0651324ea91ed7d6815a932c Mon Sep 17 00:00:00 2001 From: Camila Belo Date: Tue, 3 Dec 2024 14:44:40 +0100 Subject: [PATCH] refactor: use before shutdown hook name Signed-off-by: Camila Belo --- .../src/wiring/BackendInitializer.ts | 4 +-- .../rootHealth/rootHealthServiceFactory.ts | 2 +- .../rootHttpRouterServiceFactory.ts | 2 +- .../rootLifecycleServiceFactory.ts | 28 ++++++++++--------- .../definitions/RootLifecycleService.ts | 2 +- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/packages/backend-app-api/src/wiring/BackendInitializer.ts b/packages/backend-app-api/src/wiring/BackendInitializer.ts index 86a9aa6405..53c87c06c2 100644 --- a/packages/backend-app-api/src/wiring/BackendInitializer.ts +++ b/packages/backend-app-api/src/wiring/BackendInitializer.ts @@ -452,7 +452,7 @@ export class BackendInitializer { const rootLifecycleService = await this.#getRootLifecycleImpl(); // Root services like the health one need to immediatelly be notified of the shutdown - await rootLifecycleService.preShutdown(); + await rootLifecycleService.beforeShutdown(); // Get all plugins. const allPlugins = new Set(); @@ -480,7 +480,7 @@ export class BackendInitializer { async #getRootLifecycleImpl(): Promise< RootLifecycleService & { startup(): Promise; - preShutdown(): Promise; + beforeShutdown(): Promise; shutdown(): Promise; } > { diff --git a/packages/backend-defaults/src/entrypoints/rootHealth/rootHealthServiceFactory.ts b/packages/backend-defaults/src/entrypoints/rootHealth/rootHealthServiceFactory.ts index 83efc5a055..ab9fc86a41 100644 --- a/packages/backend-defaults/src/entrypoints/rootHealth/rootHealthServiceFactory.ts +++ b/packages/backend-defaults/src/entrypoints/rootHealth/rootHealthServiceFactory.ts @@ -29,7 +29,7 @@ export class DefaultRootHealthService implements RootHealthService { options.lifecycle.addStartupHook(() => { this.#isRunning = true; }); - options.lifecycle.addPreShutdownHook(() => { + options.lifecycle.addBeforeShutdownHook(() => { this.#isRunning = false; }); } diff --git a/packages/backend-defaults/src/entrypoints/rootHttpRouter/rootHttpRouterServiceFactory.ts b/packages/backend-defaults/src/entrypoints/rootHttpRouter/rootHttpRouterServiceFactory.ts index 3a5747057f..755535dc70 100644 --- a/packages/backend-defaults/src/entrypoints/rootHttpRouter/rootHttpRouterServiceFactory.ts +++ b/packages/backend-defaults/src/entrypoints/rootHttpRouter/rootHttpRouterServiceFactory.ts @@ -120,7 +120,7 @@ const rootHttpRouterServiceFactoryWithOptions = ( }, }); - lifecycle.addPreShutdownHook(() => server.stop()); + lifecycle.addBeforeShutdownHook(() => server.stop()); await server.start(); diff --git a/packages/backend-defaults/src/entrypoints/rootLifecycle/rootLifecycleServiceFactory.ts b/packages/backend-defaults/src/entrypoints/rootLifecycle/rootLifecycleServiceFactory.ts index 9711bd36fd..9a8f64edb5 100644 --- a/packages/backend-defaults/src/entrypoints/rootLifecycle/rootLifecycleServiceFactory.ts +++ b/packages/backend-defaults/src/entrypoints/rootLifecycle/rootLifecycleServiceFactory.ts @@ -65,32 +65,34 @@ export class BackendLifecycleImpl implements RootLifecycleService { ); } - #hasPreShutdown = false; - #preShutdownTasks: Array<{ hook: () => void }> = []; + #hasBeforeShutdown = false; + #beforeShutdownTasks: Array<{ hook: () => void }> = []; - addPreShutdownHook(hook: () => void): void { - if (this.#hasPreShutdown) { - throw new Error('Attempted to add pre shutdown hook after pre shutdown'); + addBeforeShutdownHook(hook: () => void): void { + if (this.#hasBeforeShutdown) { + throw new Error( + 'Attempt to add before shutdown hook after shutdown has started', + ); } - this.#preShutdownTasks.push({ hook }); + this.#beforeShutdownTasks.push({ hook }); } - async preShutdown(): Promise { - if (this.#hasPreShutdown) { + async beforeShutdown(): Promise { + if (this.#hasBeforeShutdown) { return; } - this.#hasPreShutdown = true; + this.#hasBeforeShutdown = true; this.logger.debug( - `Running ${this.#preShutdownTasks.length} pre shutdown tasks...`, + `Running ${this.#beforeShutdownTasks.length} before shutdown tasks...`, ); await Promise.all( - this.#preShutdownTasks.map(async ({ hook }) => { + this.#beforeShutdownTasks.map(async ({ hook }) => { try { await hook(); - this.logger.debug(`Pre shutdown hook succeeded`); + this.logger.debug(`Before shutdown hook succeeded`); } catch (error) { - this.logger.error(`Pre shutdown hook failed, ${error}`); + this.logger.error(`Before shutdown hook failed, ${error}`); } }), ); diff --git a/packages/backend-plugin-api/src/services/definitions/RootLifecycleService.ts b/packages/backend-plugin-api/src/services/definitions/RootLifecycleService.ts index 15b744e35b..085c21e9e2 100644 --- a/packages/backend-plugin-api/src/services/definitions/RootLifecycleService.ts +++ b/packages/backend-plugin-api/src/services/definitions/RootLifecycleService.ts @@ -24,5 +24,5 @@ import { LifecycleService } from './LifecycleService'; * @public */ export interface RootLifecycleService extends LifecycleService { - addPreShutdownHook(hook: () => void): void; + addBeforeShutdownHook(hook: () => void): void; }