Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
96c5967a35 | |||
c21c10d7f2 | |||
eb137c3871 | |||
334a38c71e | |||
0d0404d00d | |||
44414ab1b4 | |||
bcf33c718c | |||
7a8ab962b4 | |||
29021a7ffe | |||
ca473dcd2e | |||
b0b32fd730 | |||
924309ea8e | |||
18928310e7 | |||
1451da7aba |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartexit",
|
||||
"version": "1.0.4",
|
||||
"version": "1.0.11",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartexit",
|
||||
"version": "1.0.4",
|
||||
"version": "1.0.11",
|
||||
"private": false,
|
||||
"description": "do things before one exists a process",
|
||||
"main": "dist/index.js",
|
||||
@ -21,6 +21,7 @@
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/lik": "^3.0.5"
|
||||
"@pushrocks/lik": "^3.0.5",
|
||||
"@pushrocks/smartdelay": "^2.0.3"
|
||||
}
|
||||
}
|
||||
|
@ -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 => {
|
||||
|
59
ts/index.ts
59
ts/index.ts
@ -1,40 +1,67 @@
|
||||
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>();
|
||||
|
||||
/**
|
||||
* adds a process to be exited
|
||||
* @param childProcessArg
|
||||
*/
|
||||
public addProcess(childProcessArg: plugins.childProcess.ChildProcess) {
|
||||
this.processesToEnd.add(childProcessArg);
|
||||
}
|
||||
|
||||
public static async killAll() {
|
||||
console.log('Checking for remaining child processes before exit...');
|
||||
/**
|
||||
* removes a process to be exited
|
||||
*/
|
||||
public removeProcess(childProcessArg: plugins.childProcess.ChildProcess) {
|
||||
this.processesToEnd.remove(childProcessArg);
|
||||
}
|
||||
|
||||
public async killAll() {
|
||||
console.log('SMARTEXIT: 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 => {
|
||||
console.log(`killing process #${counter}`);
|
||||
childProcessArg.kill('SIGINT');
|
||||
this.processesToEnd.forEach(async childProcessArg => {
|
||||
const pid = childProcessArg.pid;
|
||||
console.log(`SMARTEXIT: 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(`Everything looks clean. Ready to exit!`);
|
||||
}
|
||||
console.log(`SMARTEXIT: Everything looks clean. Ready to exit!`);
|
||||
}
|
||||
}
|
||||
|
||||
constructor() {
|
||||
// do app specific cleaning before exiting
|
||||
process.on('exit', async () => {
|
||||
await SmartExit.killAll();
|
||||
process.on('exit', async (code) => {
|
||||
if (code === 0) {
|
||||
console.log('SMARTEXIT: Process wants to exit');
|
||||
await this.killAll();
|
||||
}
|
||||
});
|
||||
|
||||
// catch ctrl+c event and exit normally
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Ctrl-C...');
|
||||
await SmartExit.killAll();
|
||||
console.log('SMARTEXIT: Ctrl-C... or SIGINT signal received!');
|
||||
await this.killAll();
|
||||
});
|
||||
|
||||
//catch uncaught exceptions, trace, then exit normally
|
||||
process.on('uncaughtException', async err => {
|
||||
console.log('Ctrl-C...');
|
||||
await SmartExit.killAll();
|
||||
console.log('SMARTEXIT: uncaught exception...');
|
||||
console.log(err);
|
||||
await this.killAll();
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -5,5 +5,6 @@ export { childProcess };
|
||||
|
||||
// pushrocks scope
|
||||
import * as lik from '@pushrocks/lik';
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
|
||||
export { lik };
|
||||
export { lik, smartdelay };
|
||||
|
Reference in New Issue
Block a user