From 2ae75c06353cc5bd6f15de33b2002d66e7d84066 Mon Sep 17 00:00:00 2001 From: Nikita Savchenko Date: Sun, 6 Sep 2020 18:59:40 +0300 Subject: [PATCH] Add one more test, clarify readme --- package.json | 6 +++--- readme.md | 8 +++++--- test.js | 23 ++++++++++++++++++++++- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index e4e11c7..3f1a2cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-graceful-shutdown", - "version": "1.0.3", + "version": "1.0.4", "description": "Gracefully shutdown your modular NodeJS application", "main": "index.js", "engines": { @@ -37,7 +37,7 @@ }, "homepage": "https://github.com/ZitRos/node-graceful-shutdown#readme", "devDependencies": { - "esm": "^3.0.83", - "ava": "^0.25.0" + "ava": "^3.12.1", + "esm": "^3.2.25" } } diff --git a/readme.md b/readme.md index 8a93116..229e561 100644 --- a/readme.md +++ b/readme.md @@ -6,6 +6,8 @@ Gracefully handle your modular NodeJS application's shutdown (termination), using dependencies. +Process signals captured: `SIGINT`, `SIGTERM`, `SIGQUIT`. + Example ------- @@ -44,12 +46,12 @@ This library, along existing ones, allow your application to be **modular**. You in the same module, where initialization happens. In addition, it allows specifying the order Recommendations: -1. Please, do not use this module in libraries (modules, packages). Use for the end application only. +1. Please, **do not use this module in libraries** (modules, packages). It is intended for end applications only (see why in `5.`). 2. Once imported, `onShutdown` is application-wide (in terms of a single process), so the callbacks and their dependencies will see each other when imported from multiple files. 3. Circular shutdown handler dependencies will throw an error immediately once declared. 4. There's also an `onShutdownError` export which takes an error as an argument when any of assigned shutdown handlers throw an error (added for very-very prudent programmers only). -5. Importing this module deletes existing handlers (`SIGINT`, `SIGTERM`, `SIGQUIT`) if there are any (normally, there should be no other handlers). -6. You may also consider defining constants +5. Importing this module **deletes** existing handlers (`SIGINT`, `SIGTERM`, `SIGQUIT`) if there are any. This is intended as other custom handlers can exit the process at any time. +6. You may also consider defining constants in your application, instead of string arguments (names). Licence ------- diff --git a/test.js b/test.js index f59d1f9..86fb8bd 100644 --- a/test.js +++ b/test.js @@ -198,6 +198,27 @@ const tests = [ assert.equal(callTable["a"], 1); + }], + // Keep this test at the end. + ["Removes other shutdown handlers", async () => { + + process.addListener('SIGINT', () => { + console.log('\n\nSIGINT is handled by the custom code, but is expected to be handled by the library.\n'); + process.exit(101); + }); + + const pkg = await testModule(); + const callTable = { "a": 0 }; + + pkg.onShutdown("a", async function () { + callTable["a"] = 1; + }); + + process.emit("SIGINT"); + await delay(); + + assert.equal(callTable["a"], 1); + }] ]; @@ -229,7 +250,7 @@ async function test () { } test().then(() => { - console.log("It works indeed!"); + console.log("\nIt works indeed!"); }).catch((e) => { console.log("\n\nTEST FAILED"); console.error(e);