diff --git a/packages/backend-app-api/src/services/implementations/lifecycle/lifecycleServiceFactory.ts b/packages/backend-app-api/src/services/implementations/lifecycle/lifecycleServiceFactory.ts index c0cfcf7d8c..2b68a81807 100644 --- a/packages/backend-app-api/src/services/implementations/lifecycle/lifecycleServiceFactory.ts +++ b/packages/backend-app-api/src/services/implementations/lifecycle/lifecycleServiceFactory.ts @@ -44,6 +44,9 @@ export class BackendPluginLifecycleImpl implements LifecycleService { hook: LifecycleServiceStartupHook, options?: LifecycleServiceStartupOptions, ): void { + if (this.#hasStarted) { + throw new Error('Attempted to add startup hook after startup'); + } this.#startupTasks.push({ hook, options }); } diff --git a/packages/backend-app-api/src/services/implementations/rootLifecycle/rootLifecycleServiceFactory.test.ts b/packages/backend-app-api/src/services/implementations/rootLifecycle/rootLifecycleServiceFactory.test.ts index 8d2b2af3d5..91cb9031ae 100644 --- a/packages/backend-app-api/src/services/implementations/rootLifecycle/rootLifecycleServiceFactory.test.ts +++ b/packages/backend-app-api/src/services/implementations/rootLifecycle/rootLifecycleServiceFactory.test.ts @@ -44,4 +44,17 @@ describe('lifecycleService', () => { }); await expect(service.shutdown()).resolves.toBeUndefined(); }); + + it('should reject hooks after trigger', async () => { + const service = new BackendLifecycleImpl(getVoidLogger()); + await service.startup(); + expect(() => { + service.addStartupHook(() => {}); + }).toThrow('Attempted to add startup hook after startup'); + + await service.shutdown(); + expect(() => { + service.addShutdownHook(() => {}); + }).toThrow('Attempted to add shutdown hook after shutdown'); + }); }); diff --git a/packages/backend-app-api/src/services/implementations/rootLifecycle/rootLifecycleServiceFactory.ts b/packages/backend-app-api/src/services/implementations/rootLifecycle/rootLifecycleServiceFactory.ts index 39e66458c3..197f99b97a 100644 --- a/packages/backend-app-api/src/services/implementations/rootLifecycle/rootLifecycleServiceFactory.ts +++ b/packages/backend-app-api/src/services/implementations/rootLifecycle/rootLifecycleServiceFactory.ts @@ -39,6 +39,9 @@ export class BackendLifecycleImpl implements RootLifecycleService { hook: LifecycleServiceStartupHook, options?: LifecycleServiceStartupOptions, ): void { + if (this.#hasStarted) { + throw new Error('Attempted to add startup hook after startup'); + } this.#startupTasks.push({ hook, options }); } @@ -72,6 +75,9 @@ export class BackendLifecycleImpl implements RootLifecycleService { hook: LifecycleServiceShutdownHook, options?: LifecycleServiceShutdownOptions, ): void { + if (this.#hasShutdown) { + throw new Error('Attempted to add shutdown hook after shutdown'); + } this.#shutdownTasks.push({ hook, options }); }