fix(orchestrator): own startup and shutdown resources (CL16)
This commit is contained in:
parent
21666242f4
commit
8546fce4a9
8 changed files with 384 additions and 117 deletions
|
|
@ -59,6 +59,11 @@ export class ServiceContainerBuilder {
|
|||
}
|
||||
|
||||
async build(): Promise<AwilixContainer<ServiceDefinitions>> {
|
||||
return (await this.buildOwned()).container;
|
||||
}
|
||||
|
||||
/** Keep initialization and shutdown in one resource lifetime when the caller owns an app. */
|
||||
async buildOwned() {
|
||||
// Validate and prepare config
|
||||
const validatedConfig = this.prepareConfig();
|
||||
|
||||
|
|
@ -71,13 +76,24 @@ export class ServiceContainerBuilder {
|
|||
// Register services
|
||||
this.registerServices(container, validatedConfig);
|
||||
|
||||
const lifecycleManager = new ServiceLifecycleManager();
|
||||
// Initialize services if not skipped
|
||||
if (!this.options.skipInitialization) {
|
||||
const lifecycleManager = new ServiceLifecycleManager();
|
||||
await lifecycleManager.initializeServices(container, this.options.initializationTimeout);
|
||||
}
|
||||
|
||||
return container;
|
||||
let shutdown: Promise<void> | undefined;
|
||||
return {
|
||||
container,
|
||||
shutdown: () =>
|
||||
(shutdown ??= (async () => {
|
||||
try {
|
||||
await lifecycleManager.shutdownServices(container);
|
||||
} finally {
|
||||
await container.dispose();
|
||||
}
|
||||
})()),
|
||||
};
|
||||
}
|
||||
|
||||
private prepareConfig(): AppConfig {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue