refactor: use before shutdown hook name
Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
@@ -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<string>();
|
||||
@@ -480,7 +480,7 @@ export class BackendInitializer {
|
||||
async #getRootLifecycleImpl(): Promise<
|
||||
RootLifecycleService & {
|
||||
startup(): Promise<void>;
|
||||
preShutdown(): Promise<void>;
|
||||
beforeShutdown(): Promise<void>;
|
||||
shutdown(): Promise<void>;
|
||||
}
|
||||
> {
|
||||
|
||||
@@ -29,7 +29,7 @@ export class DefaultRootHealthService implements RootHealthService {
|
||||
options.lifecycle.addStartupHook(() => {
|
||||
this.#isRunning = true;
|
||||
});
|
||||
options.lifecycle.addPreShutdownHook(() => {
|
||||
options.lifecycle.addBeforeShutdownHook(() => {
|
||||
this.#isRunning = false;
|
||||
});
|
||||
}
|
||||
|
||||
+1
-1
@@ -120,7 +120,7 @@ const rootHttpRouterServiceFactoryWithOptions = (
|
||||
},
|
||||
});
|
||||
|
||||
lifecycle.addPreShutdownHook(() => server.stop());
|
||||
lifecycle.addBeforeShutdownHook(() => server.stop());
|
||||
|
||||
await server.start();
|
||||
|
||||
|
||||
+15
-13
@@ -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<void> {
|
||||
if (this.#hasPreShutdown) {
|
||||
async beforeShutdown(): Promise<void> {
|
||||
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}`);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -24,5 +24,5 @@ import { LifecycleService } from './LifecycleService';
|
||||
* @public
|
||||
*/
|
||||
export interface RootLifecycleService extends LifecycleService {
|
||||
addPreShutdownHook(hook: () => void): void;
|
||||
addBeforeShutdownHook(hook: () => void): void;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user