smartexit/ts/index.ts

83 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-09-11 08:29:01 +00:00
import * as plugins from './smartexit.plugins.js';
2019-05-16 13:10:22 +00:00
2019-05-16 16:48:45 +00:00
export class SmartExit {
2021-07-27 11:42:13 +00:00
public processesToEnd = new plugins.lik.ObjectMap<plugins.childProcess.ChildProcess>();
public cleanupFunctions = new plugins.lik.ObjectMap<() => Promise<any>>();
2019-05-19 20:23:38 +00:00
2019-05-19 20:23:17 +00:00
/**
* adds a process to be exited
* @param childProcessArg
*/
public addProcess(childProcessArg: plugins.childProcess.ChildProcess) {
2019-05-19 20:12:21 +00:00
this.processesToEnd.add(childProcessArg);
2019-05-16 16:48:45 +00:00
}
2021-07-27 11:42:13 +00:00
public addCleanupFunction(cleanupFunctionArg: () => Promise<any>) {
this.cleanupFunctions.add(cleanupFunctionArg);
}
2019-05-19 20:23:17 +00:00
/**
* removes a process to be exited
*/
public removeProcess(childProcessArg: plugins.childProcess.ChildProcess) {
this.processesToEnd.remove(childProcessArg);
}
2019-05-19 20:12:21 +00:00
public async killAll() {
2021-07-27 12:56:02 +00:00
console.log('Checking for remaining child processes before exit...');
2019-05-16 16:48:45 +00:00
if (this.processesToEnd.getArray().length > 0) {
2021-07-27 12:56:02 +00:00
console.log('found remaining child processes');
2019-05-16 16:48:45 +00:00
let counter = 1;
2021-07-27 11:42:13 +00:00
this.processesToEnd.forEach(async (childProcessArg) => {
const pid = childProcessArg.pid;
2021-07-27 12:56:02 +00:00
console.log(`killing process #${counter} with pid ${pid}`);
plugins.smartdelay.delayFor(10000).then(() => {
if (childProcessArg.killed) {
return;
}
2019-05-28 08:28:56 +00:00
process.kill(pid, 'SIGKILL');
});
2019-05-28 08:28:56 +00:00
process.kill(pid, 'SIGINT');
2021-07-27 11:42:13 +00:00
2019-05-16 16:48:45 +00:00
counter++;
});
} else {
2021-07-27 12:56:02 +00:00
console.log(`ChildProcesses look clean.`);
2021-07-27 11:42:13 +00:00
}
if (this.cleanupFunctions.getArray.length > 0) {
2023-09-11 08:29:01 +00:00
this.cleanupFunctions.forEach(async (cleanupFunction) => {
2021-07-27 11:42:13 +00:00
await cleanupFunction();
2023-09-11 08:29:01 +00:00
});
2019-05-16 16:48:45 +00:00
}
2021-07-27 12:56:02 +00:00
console.log(`Ready to exit!`);
2019-05-16 16:48:45 +00:00
}
2019-05-19 20:12:21 +00:00
constructor() {
// do app specific cleaning before exiting
2019-05-27 13:16:38 +00:00
process.on('exit', async (code) => {
if (code === 0) {
2021-07-27 12:56:02 +00:00
console.log('Process wants to exit');
2019-05-27 13:16:38 +00:00
await this.killAll();
2021-07-27 12:56:02 +00:00
console.log('Exited ok!');
2019-05-28 08:18:10 +00:00
} else {
2021-07-27 12:56:02 +00:00
console.error('Exited NOT OK!');
2019-05-27 13:16:38 +00:00
}
2019-05-19 20:12:21 +00:00
});
2019-05-16 16:48:45 +00:00
2019-05-19 20:12:21 +00:00
// catch ctrl+c event and exit normally
process.on('SIGINT', async () => {
2021-07-27 12:56:02 +00:00
console.log('Ctrl-C... or SIGINT signal received!');
2019-05-19 20:12:21 +00:00
await this.killAll();
2019-05-28 08:34:57 +00:00
process.exit(0);
2019-05-19 20:12:21 +00:00
});
2019-05-16 16:48:45 +00:00
2019-05-19 20:12:21 +00:00
//catch uncaught exceptions, trace, then exit normally
2021-07-27 11:42:13 +00:00
process.on('uncaughtException', async (err) => {
2021-07-27 12:56:02 +00:00
console.log('SMARTEXIT: uncaught exception...');
2019-05-27 13:16:38 +00:00
console.log(err);
2019-05-19 20:12:21 +00:00
await this.killAll();
2019-05-28 08:28:56 +00:00
process.exit(1);
2019-05-19 20:12:21 +00:00
});
}
}