Files
smartexit/ts/index.ts

160 lines
5.5 KiB
TypeScript

import * as plugins from './smartexit.plugins.js';
export interface ISmartExitOptions {
silent?: boolean; // Completely disable logging
}
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<plugins.childProcess.ChildProcess>();
public cleanupFunctions = new plugins.lik.ObjectMap<() => Promise<any>>();
private options: ISmartExitOptions;
/**
* Internal logging helper that respects silent option
*/
private log(message: string, isError = false): void {
if (this.options.silent) {
return;
}
const prefix = '[smartexit]';
if (isError) {
console.error(`${prefix} ${message}`);
} else {
console.log(`${prefix} ${message}`);
}
}
/**
* adds a process to be exited
* @param childProcessArg
*/
public addProcess(childProcessArg: plugins.childProcess.ChildProcess) {
this.processesToEnd.add(childProcessArg);
}
public addCleanupFunction(cleanupFunctionArg: () => Promise<any>) {
this.cleanupFunctions.add(cleanupFunctionArg);
}
/**
* removes a process to be exited
*/
public removeProcess(childProcessArg: plugins.childProcess.ChildProcess) {
this.processesToEnd.remove(childProcessArg);
}
public async killAll(): Promise<{ processesKilled: number; cleanupFunctionsRan: number }> {
const processes = this.processesToEnd.getArray();
const cleanupFuncs = this.cleanupFunctions.getArray();
let processesKilled = 0;
let cleanupFunctionsRan = 0;
// Kill child processes
if (processes.length > 0) {
for (const childProcessArg of processes) {
const pid = childProcessArg.pid;
if (pid) {
plugins.smartdelay.delayFor(10000).then(() => {
if (childProcessArg.killed) {
return;
}
process.kill(pid, 'SIGKILL');
});
process.kill(pid, 'SIGINT');
processesKilled++;
}
}
}
// Run cleanup functions
if (cleanupFuncs.length > 0) {
for (const cleanupFunction of cleanupFuncs) {
await cleanupFunction();
cleanupFunctionsRan++;
}
}
return { processesKilled, cleanupFunctionsRan };
}
constructor(optionsArg: ISmartExitOptions = {}) {
this.options = optionsArg;
// do app specific cleaning before exiting
process.on('exit', async (code) => {
if (code === 0) {
const { processesKilled, cleanupFunctionsRan } = await this.killAll();
this.log(`Shutdown complete: killed ${processesKilled} child processes, ran ${cleanupFunctionsRan} cleanup functions`);
} else {
const { processesKilled, cleanupFunctionsRan } = await this.killAll();
this.log(`Shutdown complete: killed ${processesKilled} child processes, ran ${cleanupFunctionsRan} cleanup functions (exit code: ${code})`, true);
}
});
// catch ctrl+c event and exit normally
process.on('SIGINT', async () => {
const { processesKilled, cleanupFunctionsRan } = await this.killAll();
this.log(`Shutdown complete: killed ${processesKilled} child processes, ran ${cleanupFunctionsRan} cleanup functions`);
process.exit(0);
});
// catch uncaught exceptions, trace, then exit normally
process.on('uncaughtException', async (err) => {
this.log(`Uncaught exception: ${err.message}`, true);
const { processesKilled, cleanupFunctionsRan } = await this.killAll();
this.log(`Shutdown complete: killed ${processesKilled} child processes, ran ${cleanupFunctionsRan} cleanup functions (exit code: 1)`, true);
process.exit(1);
});
}
}