fix(core): update
This commit is contained in:
parent
47b7be41fc
commit
1451da7aba
@ -1,8 +1,10 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as smartexit from '../ts/index';
|
||||
|
||||
let testSmartexit: smartexit.SmartExit;
|
||||
|
||||
tap.test('first test', async () => {
|
||||
smartexit;
|
||||
testSmartexit = new smartexit.SmartExit();
|
||||
});
|
||||
|
||||
tap.test('should end processes upon SIGINT', async tools => {
|
||||
|
20
ts/index.ts
20
ts/index.ts
@ -1,17 +1,17 @@
|
||||
import * as plugins from './smartexit.plugins';
|
||||
|
||||
export class SmartExit {
|
||||
public static processesToEnd = new plugins.lik.Objectmap<plugins.childProcess.ChildProcess>();
|
||||
public static async addProcess(childProcessArg: plugins.childProcess.ChildProcess) {
|
||||
SmartExit.processesToEnd.add(childProcessArg);
|
||||
public processesToEnd = new plugins.lik.Objectmap<plugins.childProcess.ChildProcess>();
|
||||
public async addProcess(childProcessArg: plugins.childProcess.ChildProcess) {
|
||||
this.processesToEnd.add(childProcessArg);
|
||||
}
|
||||
|
||||
public static async killAll() {
|
||||
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;
|
||||
SmartExit.processesToEnd.forEach(async childProcessArg => {
|
||||
this.processesToEnd.forEach(async childProcessArg => {
|
||||
console.log(`killing process #${counter}`);
|
||||
childProcessArg.kill('SIGINT');
|
||||
counter++;
|
||||
@ -20,21 +20,23 @@ export class SmartExit {
|
||||
console.log(`Everything looks clean. Ready to exit!`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
constructor() {
|
||||
// do app specific cleaning before exiting
|
||||
process.on('exit', async () => {
|
||||
await SmartExit.killAll();
|
||||
await this.killAll();
|
||||
});
|
||||
|
||||
// catch ctrl+c event and exit normally
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Ctrl-C...');
|
||||
await SmartExit.killAll();
|
||||
await this.killAll();
|
||||
});
|
||||
|
||||
//catch uncaught exceptions, trace, then exit normally
|
||||
process.on('uncaughtException', async err => {
|
||||
console.log('Ctrl-C...');
|
||||
await SmartExit.killAll();
|
||||
await this.killAll();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user