Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
29021a7ffe | |||
ca473dcd2e | |||
b0b32fd730 | |||
924309ea8e | |||
18928310e7 | |||
1451da7aba | |||
47b7be41fc | |||
22db7c9f57 |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartexit",
|
"name": "@pushrocks/smartexit",
|
||||||
"version": "1.0.3",
|
"version": "1.0.7",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartexit",
|
"name": "@pushrocks/smartexit",
|
||||||
"version": "1.0.3",
|
"version": "1.0.7",
|
||||||
"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",
|
||||||
|
@ -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 => {
|
||||||
|
32
ts/index.ts
32
ts/index.ts
@ -1,17 +1,29 @@
|
|||||||
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() {
|
/**
|
||||||
|
* 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...');
|
console.log('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}`);
|
console.log(`killing process #${counter}`);
|
||||||
childProcessArg.kill('SIGINT');
|
childProcessArg.kill('SIGINT');
|
||||||
counter++;
|
counter++;
|
||||||
@ -20,21 +32,23 @@ export class SmartExit {
|
|||||||
console.log(`Everything looks clean. Ready to exit!`);
|
console.log(`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 () => {
|
||||||
await SmartExit.killAll();
|
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('Ctrl-C...');
|
||||||
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('Ctrl-C...');
|
||||||
await SmartExit.killAll();
|
await this.killAll();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user