14 Commits

Author SHA1 Message Date
96c5967a35 1.0.11 2019-05-27 15:16:39 +02:00
c21c10d7f2 fix(core): update 2019-05-27 15:16:38 +02:00
eb137c3871 1.0.10 2019-05-23 17:54:42 +02:00
334a38c71e fix(core): update 2019-05-23 17:54:41 +02:00
0d0404d00d 1.0.9 2019-05-23 17:53:17 +02:00
44414ab1b4 fix(core): now killing groups of processes with negative pid 2019-05-23 17:53:16 +02:00
bcf33c718c 1.0.8 2019-05-22 12:47:38 +02:00
7a8ab962b4 fix(core): add sigkill with 10 second delay 2019-05-22 12:47:38 +02:00
29021a7ffe 1.0.7 2019-05-19 22:23:38 +02:00
ca473dcd2e fix(core): update 2019-05-19 22:23:38 +02:00
b0b32fd730 1.0.6 2019-05-19 22:23:18 +02:00
924309ea8e fix(core): update 2019-05-19 22:23:17 +02:00
18928310e7 1.0.5 2019-05-19 22:12:21 +02:00
1451da7aba fix(core): update 2019-05-19 22:12:21 +02:00
5 changed files with 62 additions and 31 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartexit", "name": "@pushrocks/smartexit",
"version": "1.0.4", "version": "1.0.11",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartexit", "name": "@pushrocks/smartexit",
"version": "1.0.4", "version": "1.0.11",
"private": false, "private": false,
"description": "do things before one exists a process", "description": "do things before one exists a process",
"main": "dist/index.js", "main": "dist/index.js",
@ -21,6 +21,7 @@
"tslint-config-prettier": "^1.15.0" "tslint-config-prettier": "^1.15.0"
}, },
"dependencies": { "dependencies": {
"@pushrocks/lik": "^3.0.5" "@pushrocks/lik": "^3.0.5",
"@pushrocks/smartdelay": "^2.0.3"
} }
} }

View File

@ -1,8 +1,10 @@
import { expect, tap } from '@pushrocks/tapbundle'; import { expect, tap } from '@pushrocks/tapbundle';
import * as smartexit from '../ts/index'; import * as smartexit from '../ts/index';
let testSmartexit: smartexit.SmartExit;
tap.test('first test', async () => { tap.test('first test', async () => {
smartexit; testSmartexit = new smartexit.SmartExit();
}); });
tap.test('should end processes upon SIGINT', async tools => { tap.test('should end processes upon SIGINT', async tools => {

View File

@ -1,40 +1,67 @@
import * as plugins from './smartexit.plugins'; import * as plugins from './smartexit.plugins';
export class SmartExit { export class SmartExit {
public static processesToEnd = new plugins.lik.Objectmap<plugins.childProcess.ChildProcess>(); public processesToEnd = new plugins.lik.Objectmap<plugins.childProcess.ChildProcess>();
public static async addProcess(childProcessArg: plugins.childProcess.ChildProcess) {
SmartExit.processesToEnd.add(childProcessArg); /**
* 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) { if (this.processesToEnd.getArray().length > 0) {
console.log('found remaining child processes'); console.log('found remaining child processes');
let counter = 1; let counter = 1;
SmartExit.processesToEnd.forEach(async childProcessArg => { this.processesToEnd.forEach(async childProcessArg => {
console.log(`killing process #${counter}`); const pid = childProcessArg.pid;
childProcessArg.kill('SIGINT'); 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++; counter++;
}); });
} else { } 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 // do app specific cleaning before exiting
process.on('exit', async () => { process.on('exit', async (code) => {
await SmartExit.killAll(); if (code === 0) {
console.log('SMARTEXIT: Process wants to exit');
await this.killAll();
}
}); });
// catch ctrl+c event and exit normally // catch ctrl+c event and exit normally
process.on('SIGINT', async () => { process.on('SIGINT', async () => {
console.log('Ctrl-C...'); console.log('SMARTEXIT: Ctrl-C... or SIGINT signal received!');
await SmartExit.killAll(); await this.killAll();
}); });
//catch uncaught exceptions, trace, then exit normally //catch uncaught exceptions, trace, then exit normally
process.on('uncaughtException', async err => { process.on('uncaughtException', async err => {
console.log('Ctrl-C...'); console.log('SMARTEXIT: uncaught exception...');
await SmartExit.killAll(); console.log(err);
await this.killAll();
process.exit(1);
}); });
}
}

View File

@ -5,5 +5,6 @@ export { childProcess };
// pushrocks scope // pushrocks scope
import * as lik from '@pushrocks/lik'; import * as lik from '@pushrocks/lik';
import * as smartdelay from '@pushrocks/smartdelay';
export { lik }; export { lik, smartdelay };