smartexit/ts/index.ts

133 lines
4.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
2024-04-18 11:26:02 +00:00
export type TProcessSignal =
| 'SIGHUP' // Hangup detected on controlling terminal or death of controlling process
| 'SIGINT' // Interrupt from keyboard
| 'SIGQUIT' // Quit from keyboard
| 'SIGILL' // Illegal Instruction
| 'SIGTRAP' // Trace/breakpoint trap
| 'SIGABRT' // Abort signal from abort(3)
| 'SIGIOT' // IOT trap. A synonym for SIGABRT
| 'SIGBUS' // Bus error (bad memory access)
| 'SIGFPE' // Floating-point exception
| 'SIGKILL' // Kill signal
| 'SIGUSR1' // User-defined signal 1
| 'SIGSEGV' // Invalid memory reference
| 'SIGUSR2' // User-defined signal 2
| 'SIGPIPE' // Broken pipe: write to pipe with no readers
| 'SIGALRM' // Timer signal from alarm(2)
| 'SIGTERM' // Termination signal
| 'SIGCHLD' // Child stopped or terminated
| 'SIGCONT' // Continue if stopped
| 'SIGSTOP' // Stop process
| 'SIGTSTP' // Stop typed at terminal
| 'SIGTTIN' // Terminal input for background process
| 'SIGTTOU' // Terminal output for background process
| 'SIGURG' // Urgent condition on socket
| 'SIGXCPU' // CPU time limit exceeded
| 'SIGXFSZ' // File size limit exceeded
| 'SIGVTALRM' // Virtual alarm clock
| 'SIGPROF' // Profiling timer expired
| 'SIGWINCH' // Window resize signal
| 'SIGPOLL' // Pollable event (Sys V). Synonym for SIGIO
| 'SIGIO' // I/O now possible (4.2BSD)
| 'SIGPWR' // Power failure (System V)
| 'SIGINFO' // Information request (some systems)
| 'SIGLOST' // Resource lost (unused on most UNIX systems)
| 'SIGSYS' // Bad system call (unused on most UNIX systems)
| 'SIGUNUSED'; // Synonym for SIGSYS
2019-05-16 16:48:45 +00:00
export class SmartExit {
2024-04-18 11:26:02 +00:00
public static async killTreeByPid(pidArg: number, signalArg: TProcessSignal = 'SIGKILL') {
const done = plugins.smartpromise.defer();
plugins.treeKill.default(pidArg, signalArg, (err) => {
if (err) {
done.reject(err);
} else {
done.resolve();
}
});
await done.promise;
}
// Instance
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
});
}
}