close-with-grace
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -19,3 +19,3 @@ declare namespace closeWithGrace { | ||
type Signals = "SIGTERM" | "SIGINT" | ||
type Signals = 'SIGHUP' | 'SIGINT' | 'SIGQUIT' | 'SIGILL' | 'SIGTRAP' | 'SIGABRT' | 'SIGBUS' | 'SIGFPE' | 'SIGSEGV' | 'SIGUSR2' | 'SIGTERM' | ||
interface CloseWithGraceCallback { | ||
@@ -22,0 +22,0 @@ ( |
38
index.js
'use strict' | ||
const { signalEvents } = require('./events/signal.events') | ||
const { errorEvents } = require('./events/error.events') | ||
const { exitEvents } = require('./events/exit.events') | ||
const { promisify } = require('util') | ||
@@ -26,7 +30,5 @@ const sleep = promisify(setTimeout) | ||
process.once('SIGTERM', onSignal) | ||
process.once('SIGINT', onSignal) | ||
process.once('uncaughtException', onError) | ||
process.once('unhandledRejection', onError) | ||
process.once('beforeExit', onNormalExit) | ||
signalEvents.forEach((event) => process.once(event, onSignal)) | ||
errorEvents.forEach((event) => process.once(event, onError)) | ||
exitEvents.forEach((event) => process.once(event, onNormalExit)) | ||
@@ -36,14 +38,12 @@ const sleeped = Symbol('sleeped') | ||
return { | ||
close () { | ||
run({ manual: true }) | ||
}, | ||
uninstall () { | ||
process.removeListener('SIGTERM', onSignal) | ||
process.removeListener('SIGINT', onSignal) | ||
process.removeListener('uncaughtException', onError) | ||
process.removeListener('unhandledRejection', onError) | ||
process.removeListener('beforeExit', onNormalExit) | ||
} | ||
close: () => run({ manual: true }), | ||
uninstall: cleanup | ||
} | ||
function cleanup () { | ||
signalEvents.forEach((event) => process.removeListener(event, onSignal)) | ||
errorEvents.forEach((event) => process.removeListener(event, onError)) | ||
exitEvents.forEach((event) => process.removeListener(event, onNormalExit)) | ||
} | ||
function onSignal (signal) { | ||
@@ -106,7 +106,7 @@ run({ signal }) | ||
async function run (out) { | ||
process.on('SIGTERM', afterFirstSignal) | ||
process.on('SIGINT', afterFirstSignal) | ||
process.on('uncaughtException', afterFirstError) | ||
process.on('unhandledRejection', afterFirstError) | ||
cleanup() | ||
signalEvents.forEach((event) => process.on(event, afterFirstSignal)) | ||
errorEvents.forEach((event) => process.on(event, afterFirstError)) | ||
closeWithGrace.closing = true | ||
@@ -113,0 +113,0 @@ |
{ | ||
"name": "close-with-grace", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Exit your process, gracefully (if possible)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -80,6 +80,16 @@ # close-with-grace | ||
* `process.once('SIGHUP')` | ||
* `process.once('SIGINT')` | ||
* `process.once('SIGQUIT')` | ||
* `process.once('SIGILL')` | ||
* `process.once('SIGTRAP')` | ||
* `process.once('SIGABRT')` | ||
* `process.once('SIGBUS')` | ||
* `process.once('SIGFPE')` | ||
* `process.once('SIGSEGV')` | ||
* `process.once('SIGUSR2')` | ||
* `process.once('SIGTERM')` | ||
* `process.once('SIGINT')` | ||
* `process.once('uncaughtException')` | ||
* `process.once('unhandledRejection')` | ||
* `process.once('beforeExit')` | ||
@@ -86,0 +96,0 @@ In case one of them is emitted, it will call the given function. |
'use strict' | ||
const { signalEvents } = require('../events/signal.events') | ||
const { errorEvents } = require('../events/error.events') | ||
const test = require('tape') | ||
@@ -40,3 +42,3 @@ const { fork } = require('child_process') | ||
for (const signal of ['SIGTERM', 'SIGINT']) { | ||
for (const signal of signalEvents) { | ||
test(`close gracefully (${signal}) async/await`, async (t) => { | ||
@@ -219,3 +221,3 @@ const child = fork(join(__dirname, 'simple.js'), { | ||
for (const event of ['uncaughtException', 'unhandledRejection']) { | ||
for (const event of errorEvents) { | ||
test(`close gracefully (${event})`, async (t) => { | ||
@@ -222,0 +224,0 @@ const child = fork(join(__dirname, event + '.js'), { |
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
26170
142285
25
674
128