Skip to content

Commit

Permalink
fix: exposes stuck services during shutdown timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Figedi committed Aug 19, 2023
1 parent 1a85e3a commit b2d9402
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/app/ApplicationBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,17 +607,31 @@ export class ApplicationBuilder<Config> {
);

try {
const shutdownServices = new Set<ServiceWithLifecycleHandlers>(this.servicesWithLifecycleHandlers);
await Promise.race([
sleep(this.appBuilderConfig.shutdownGracePeriodSeconds * 1000, true).then(() => {
const error = new Error("Timeout while graceful-shutdown, will exit now");
this.rootLogger.error({ reason }, error.message);
this.rootLogger.error(
{
remainingServicesWithLifecycleHandlers: Array.from(shutdownServices).map(
s => s.constructor.name,
),
reason,
},
error.message,
);
if (this.appBuilderConfig.exitAfterRun) {
process.exit(1);
} else {
throw error;
}
}),
Promise.all(this.servicesWithLifecycleHandlers.map(svc => svc.shutdown && svc.shutdown())),
Promise.all(
this.servicesWithLifecycleHandlers.map(async svc => {
await svc.shutdown?.();
shutdownServices.delete(svc);
}),
),
]);
debug("Ran all service shutdown handlers");
if (this.appBuilderConfig.exitAfterRun) {
Expand Down

0 comments on commit b2d9402

Please sign in to comment.