fix(core): Handle multiple termination signals correctly (#8046)

Prevent possible multiple termination signals initiating the shutdown
process multiple times.
This commit is contained in:
Tomi Turtiainen 2023-12-15 17:35:22 +02:00 committed by GitHub
parent e69707efd4
commit 67bd8ad698
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -38,12 +38,14 @@ export abstract class BaseCommand extends Command {
protected server?: AbstractServer;
protected isShuttingDown = false;
async init(): Promise<void> {
await initErrorHandling();
initExpressionEvaluator();
process.once('SIGTERM', async () => this.stopProcess());
process.once('SIGINT', async () => this.stopProcess());
process.once('SIGTERM', this.onTerminationSignal('SIGTERM'));
process.once('SIGINT', this.onTerminationSignal('SIGINT'));
// Make sure the settings exist
this.instanceSettings = Container.get(InstanceSettings);
@ -299,4 +301,17 @@ export abstract class BaseCommand extends Command {
const exitCode = error instanceof ExitError ? error.oclif.exit : error ? 1 : 0;
this.exit(exitCode);
}
private onTerminationSignal(signal: string) {
return async () => {
if (this.isShuttingDown) {
this.logger.info(`Received ${signal}. Already shutting down...`);
return;
}
this.logger.info(`Received ${signal}. Shutting down...`);
this.isShuttingDown = true;
await this.stopProcess();
};
}
}