2021-06-15 12:32:35 +02:00
|
|
|
import { Logger } from '../logger';
|
|
|
|
import { IUnleash } from '../types/core';
|
|
|
|
|
|
|
|
function registerGracefulShutdown(unleash: IUnleash, logger: Logger): void {
|
2021-06-17 20:33:34 +02:00
|
|
|
const unleashCloser = (signal: string) => async () => {
|
2021-06-15 12:32:35 +02:00
|
|
|
try {
|
2021-06-17 20:33:34 +02:00
|
|
|
logger.info(`Graceful shutdown signal (${signal}) received.`);
|
2021-06-15 12:32:35 +02:00
|
|
|
await unleash.stop();
|
2021-06-17 20:33:34 +02:00
|
|
|
logger.info('Unleash has been successfully stopped.');
|
2021-06-15 12:32:35 +02:00
|
|
|
process.exit(0);
|
|
|
|
} catch (e) {
|
2021-06-17 20:33:34 +02:00
|
|
|
logger.error('Unable to shutdown Unleash. Hard exit!');
|
2021-06-15 12:32:35 +02:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2021-06-17 20:33:34 +02:00
|
|
|
};
|
2021-06-15 12:32:35 +02:00
|
|
|
|
2021-09-15 20:28:10 +02:00
|
|
|
logger.debug('Registering graceful shutdown');
|
2021-06-17 20:33:34 +02:00
|
|
|
|
|
|
|
process.on('SIGINT', unleashCloser('SIGINT'));
|
|
|
|
process.on('SIGHUP', unleashCloser('SIGHUP'));
|
|
|
|
process.on('SIGTERM', unleashCloser('SIGTERM'));
|
2021-06-15 12:32:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default registerGracefulShutdown;
|