import * as plugins from './smartexit.plugins.js'; 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 export class SmartExit { 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 public processesToEnd = new plugins.lik.ObjectMap(); public cleanupFunctions = new plugins.lik.ObjectMap<() => Promise>(); /** * adds a process to be exited * @param childProcessArg */ public addProcess(childProcessArg: plugins.childProcess.ChildProcess) { this.processesToEnd.add(childProcessArg); } public addCleanupFunction(cleanupFunctionArg: () => Promise) { this.cleanupFunctions.add(cleanupFunctionArg); } /** * removes a process to be exited */ public removeProcess(childProcessArg: plugins.childProcess.ChildProcess) { this.processesToEnd.remove(childProcessArg); } public async killAll() { console.log('Checking for remaining child processes before exit...'); if (this.processesToEnd.getArray().length > 0) { console.log('found remaining child processes'); let counter = 1; this.processesToEnd.forEach(async (childProcessArg) => { const pid = childProcessArg.pid; console.log(`killing process #${counter} with pid ${pid}`); plugins.smartdelay.delayFor(10000).then(() => { if (childProcessArg.killed) { return; } process.kill(pid, 'SIGKILL'); }); process.kill(pid, 'SIGINT'); counter++; }); } else { console.log(`ChildProcesses look clean.`); } if (this.cleanupFunctions.getArray.length > 0) { this.cleanupFunctions.forEach(async (cleanupFunction) => { await cleanupFunction(); }); } console.log(`Ready to exit!`); } constructor() { // do app specific cleaning before exiting process.on('exit', async (code) => { if (code === 0) { console.log('Process wants to exit'); await this.killAll(); console.log('Exited ok!'); } else { console.error('Exited NOT OK!'); } }); // catch ctrl+c event and exit normally process.on('SIGINT', async () => { console.log('Ctrl-C... or SIGINT signal received!'); await this.killAll(); process.exit(0); }); //catch uncaught exceptions, trace, then exit normally process.on('uncaughtException', async (err) => { console.log('SMARTEXIT: uncaught exception...'); console.log(err); await this.killAll(); process.exit(1); }); } }