fix: clean up service configuration startup and development commands
This commit is contained in:
parent
4ddffa6b67
commit
805a9f8fd4
36 changed files with 627 additions and 596 deletions
|
|
@ -25,42 +25,72 @@ export class ServiceLifecycleManager {
|
|||
container: AwilixContainer<ServiceDefinitions>,
|
||||
timeout = 30000
|
||||
): Promise<void> {
|
||||
const initPromises: Promise<void>[] = [];
|
||||
|
||||
for (const { name, key } of this.services) {
|
||||
const service = container.cradle[key] as ServiceWithLifecycle | null;
|
||||
|
||||
if (service) {
|
||||
const initPromise = this.initializeService(name, service);
|
||||
initPromises.push(
|
||||
Promise.race([
|
||||
initPromise,
|
||||
this.createTimeoutPromise(
|
||||
timeout,
|
||||
`${name} initialization timed out after ${timeout}ms`
|
||||
),
|
||||
])
|
||||
);
|
||||
}
|
||||
const started: { name: string; service: ServiceWithLifecycle; settled: boolean }[] = [];
|
||||
const cancelTimers: (() => void)[] = [];
|
||||
let failed = false;
|
||||
try {
|
||||
const work = this.services.map(async ({ name, key }) => {
|
||||
const service = container.cradle[key] as ServiceWithLifecycle | null;
|
||||
if (!service) {return Promise.resolve();}
|
||||
const entry = { name, service, settled: false };
|
||||
started.push(entry);
|
||||
// A timeout cannot cancel connect(). Keep ownership until it settles,
|
||||
// including resources allocated by an initializer that later rejects.
|
||||
const initialization = this.initializeService(name, service).finally(async () => {
|
||||
entry.settled = true;
|
||||
if (failed) {await this.cleanupOnce(name, service);}
|
||||
});
|
||||
let timer: ReturnType<typeof setTimeout>;
|
||||
const expired = new Promise<never>((_, reject) => {
|
||||
timer = setTimeout(
|
||||
() => reject(new Error(`${name} initialization timed out after ${timeout}ms`)),
|
||||
timeout
|
||||
);
|
||||
});
|
||||
const clear = () => clearTimeout(timer);
|
||||
cancelTimers.push(clear);
|
||||
return Promise.race([initialization, expired]).finally(clear);
|
||||
});
|
||||
await Promise.all(work);
|
||||
this.logger.info('All services initialized successfully');
|
||||
} catch (error) {
|
||||
failed = true;
|
||||
// Only settled work can be released finally. Pending work retains the
|
||||
// cleanup continuation above, so startup need not wait on a hung connect.
|
||||
await Promise.allSettled(
|
||||
started
|
||||
.filter(entry => entry.settled)
|
||||
.reverse()
|
||||
.map(({ name, service }) => this.cleanupOnce(name, service))
|
||||
);
|
||||
throw error;
|
||||
} finally {
|
||||
for (const clear of cancelTimers) {clear();}
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(initPromises);
|
||||
this.logger.info('All services initialized successfully');
|
||||
private readonly cleanup = new WeakMap<ServiceWithLifecycle, Promise<void>>();
|
||||
|
||||
private cleanupOnce(name: string, service: ServiceWithLifecycle): Promise<void> {
|
||||
let work = this.cleanup.get(service);
|
||||
if (!work) {
|
||||
work = this.shutdownService(name, service);
|
||||
this.cleanup.set(service, work);
|
||||
}
|
||||
return work;
|
||||
}
|
||||
|
||||
async shutdownServices(container: AwilixContainer<ServiceDefinitions>): Promise<void> {
|
||||
const shutdownPromises: Promise<void>[] = [];
|
||||
|
||||
// Shutdown in reverse order
|
||||
// Resolve and clean independently: one broken resolver must not prevent
|
||||
// other, already connected resources from being released.
|
||||
for (const { name, key } of [...this.services].reverse()) {
|
||||
const service = container.cradle[key] as ServiceWithLifecycle | null;
|
||||
|
||||
if (service) {
|
||||
shutdownPromises.push(this.shutdownService(name, service));
|
||||
try {
|
||||
const service = container.cradle[key] as ServiceWithLifecycle | null;
|
||||
if (service) {await this.cleanupOnce(name, service);}
|
||||
} catch (error) {
|
||||
this.logger.error(`Error resolving ${name} during shutdown:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.allSettled(shutdownPromises);
|
||||
this.logger.info('All services shut down');
|
||||
}
|
||||
|
||||
|
|
@ -93,10 +123,4 @@ export class ServiceLifecycleManager {
|
|||
this.logger.error(`Error shutting down ${name}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
private createTimeoutPromise(timeout: number, message: string): Promise<never> {
|
||||
return new Promise((_, reject) => {
|
||||
setTimeout(() => reject(new Error(message)), timeout);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue