node-graceful
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -8,5 +8,5 @@ // +----------------------------------------------------------------------+ | ||
var Graceful = { | ||
exitCode: 0 | ||
exitOnDouble: true, | ||
timeout: 30000 | ||
}; | ||
var listeners = Object.create(null); | ||
@@ -16,2 +16,4 @@ listeners.exit = []; | ||
var isExiting = false; | ||
Graceful.on = function (signal, callback, deadly) { | ||
@@ -48,3 +50,5 @@ if (signal != "exit") { | ||
Graceful.exit = function (code, signal) { | ||
if (typeof code == "number") Graceful.exitCode = code; | ||
if (typeof code == "number") { | ||
process.exitCode = code; | ||
} | ||
@@ -70,3 +74,3 @@ handler(signal || DEADLY_EVENTS[0]); | ||
return process.nextTick(function () { | ||
return process.exit(Graceful.exitCode); | ||
return killProcess(); | ||
}); | ||
@@ -78,3 +82,3 @@ } | ||
if (count >= targetCount) { | ||
if (deadly) process.exit(Graceful.exitCode); | ||
if (deadly) killProcess(); | ||
} | ||
@@ -90,8 +94,22 @@ }; | ||
if (deadly) { | ||
exitListeners.forEach(function (listener) { | ||
return invokeListener(listener, quit, event, signal); | ||
}); | ||
if (isExiting) { | ||
if (Graceful.exitOnDouble) killProcess(true); | ||
} else { | ||
isExiting = true; | ||
if (parseInt(Graceful.timeout)) { | ||
setTimeout(function () { | ||
return killProcess(true); | ||
}, Graceful.timeout); | ||
} | ||
exitListeners.forEach(function (listener) { | ||
return invokeListener(listener, quit, event, signal); | ||
}); | ||
} | ||
} | ||
} | ||
function killProcess(force) { | ||
process.exit(process.exitCode || (force ? 1 : 0)); | ||
} | ||
function invokeListener(listener, quit, event, signal) { | ||
@@ -98,0 +116,0 @@ var invoked = false; |
@@ -8,5 +8,5 @@ // +----------------------------------------------------------------------+ | ||
const Graceful = { | ||
exitCode: 0 | ||
exitOnDouble: true, | ||
timeout: 30000 | ||
}; | ||
const listeners = Object.create(null); | ||
@@ -16,2 +16,4 @@ listeners.exit = []; | ||
let isExiting = false; | ||
Graceful.on = function (signal, callback, deadly) { | ||
@@ -48,3 +50,5 @@ if (signal != 'exit') { | ||
Graceful.exit = function (code, signal) { | ||
if (typeof code == 'number') Graceful.exitCode = code; | ||
if (typeof code == 'number') { | ||
process.exitCode = code; | ||
} | ||
@@ -67,3 +71,3 @@ handler(signal || DEADLY_EVENTS[0]) | ||
if (!targetCount) { | ||
return process.nextTick(() => process.exit(Graceful.exitCode)); | ||
return process.nextTick(() => killProcess()); | ||
} | ||
@@ -74,3 +78,3 @@ | ||
if (count >= targetCount) { | ||
if (deadly) process.exit(Graceful.exitCode); | ||
if (deadly) killProcess(); | ||
} | ||
@@ -84,6 +88,19 @@ }; | ||
if (deadly) { | ||
exitListeners.forEach(listener => invokeListener(listener, quit, event, signal)); | ||
if (isExiting) { | ||
if (Graceful.exitOnDouble) killProcess(true); | ||
} | ||
else { | ||
isExiting = true; | ||
if (parseInt(Graceful.timeout)) { | ||
setTimeout(() => killProcess(true), Graceful.timeout); | ||
} | ||
exitListeners.forEach(listener => invokeListener(listener, quit, event, signal)); | ||
} | ||
} | ||
} | ||
function killProcess(force) { | ||
process.exit(process.exitCode || (force ? 1 : 0)); | ||
} | ||
function invokeListener(listener, quit, event, signal) { | ||
@@ -90,0 +107,0 @@ let invoked = false; |
{ | ||
"name": "node-graceful", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Graceful process exit manager. allows waiting on multiple async services.", | ||
@@ -5,0 +5,0 @@ "main": "graceful.js", |
@@ -125,9 +125,9 @@ # node-graceful | ||
### Graceful.exit({Number} code, {String} \[signal]) | ||
### Graceful.exit({Number} \[code], {String} \[signal]) | ||
Trigger graceful process exit. | ||
This method is meant to be a substitute command for `process.exit()` | ||
to allow other modules to exit gracefully in case of error | ||
to allow other modules to exit gracefully in case of error. | ||
- `code` - (optional) exit code to be used. default - `0` | ||
- `code` - (optional) exit code to be used. default - `process.exitCode` | ||
- `signal` - (optional) signal to be simulating for listeners. default - `SIGTERM` | ||
@@ -152,1 +152,24 @@ | ||
``` | ||
## Options | ||
Options are global and shared, any change will override previous values. | ||
#### Graceful.exitOnDouble = true {Boolean} | ||
Whether to exit immediately when a second deadly event is received, | ||
For example when Ctrl-C is pressed twice etc.. | ||
When exiting due to double event, exit code will be `process.exitCode` or `1` (necessarily a non-zero) | ||
#### Graceful.timeout = 30000 {Number} | ||
Maximum time to wait for exit listeners in `ms`. | ||
After exceeding the time, the process will force exit | ||
and the exit code will be `process.exitCode` or `1` (necessarily a non-zero) | ||
Setting the timeout to `0` will disable timeout functionality (will wait indefinitely) | ||
#### exitCode | ||
Graceful will obey process.exitCode property value when exiting | ||
apart from forced exit cases where the exit code must be non-zero. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43121
211
173