From eed821873297383b22495a57cab7266549effb37 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 9 Jan 2019 20:32:10 +0000 Subject: [PATCH 1/6] fix(na): fix-lifecycle-handling-for-signals --- src/WorkerPool.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/WorkerPool.js b/src/WorkerPool.js index e6132bb..5ca8127 100644 --- a/src/WorkerPool.js +++ b/src/WorkerPool.js @@ -267,26 +267,30 @@ export default class WorkerPool { return !this.terminated; } - terminate(force) { - if (!this.terminated) { + terminate({ exit, clean }) { + if (exit) { this.terminated = true; + process.exit(0); + return; + } + if (clean) { this.poolQueue.kill(); - this.disposeWorkers(force); + this.disposeWorkers(true); } } setupLifeCycle() { process.on('SIGTERM', () => { - this.terminate(true); + this.terminate({ exit: true }); }); process.on('SIGINT', () => { - this.terminate(true); + this.terminate({ exit: true }); }); process.on('exit', () => { - this.terminate(true); + this.terminate({ clean: true }); }); } From 3170a16c53432d5106a4832ce992a0e2d61c9a24 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 9 Jan 2019 20:33:22 +0000 Subject: [PATCH 2/6] fix(na): allow disposeWorkers to be called from terminate function. --- src/WorkerPool.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/WorkerPool.js b/src/WorkerPool.js index 5ca8127..6fb0796 100644 --- a/src/WorkerPool.js +++ b/src/WorkerPool.js @@ -342,15 +342,17 @@ export default class WorkerPool { } } - disposeWorkers(force) { - if (this.activeJobs === 0 || force) { + disposeWorkers(fromTerminate) { + if (!this.options.poolRespawn && !fromTerminate) { + this.terminate({ exit: true }); + return; + } + + if (this.activeJobs === 0 || fromTerminate) { for (const worker of this.workers) { worker.dispose(); } this.workers.clear(); } - if (!this.options.poolRespawn) { - this.terminate(); - } } } From b5504921e95b8d6bd9d52d41595bacd6a4c406b0 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 9 Jan 2019 20:34:08 +0000 Subject: [PATCH 3/6] refact(na): rename terminated to exited on WorkerPool. --- src/WorkerPool.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/WorkerPool.js b/src/WorkerPool.js index 6fb0796..2178e7b 100644 --- a/src/WorkerPool.js +++ b/src/WorkerPool.js @@ -258,18 +258,18 @@ export default class WorkerPool { this.activeJobs = 0; this.timeout = null; this.poolQueue = asyncQueue(this.distributeJob.bind(this), options.poolParallelJobs); - this.terminated = false; + this.exited = false; this.setupLifeCycle(); } isAbleToRun() { - return !this.terminated; + return !this.exited; } terminate({ exit, clean }) { if (exit) { - this.terminated = true; + this.exited = true; process.exit(0); return; } From cdc0cba74fb2f8ea30f96355bc11c99b06bb4b4b Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 9 Jan 2019 20:43:21 +0000 Subject: [PATCH 4/6] test(na): fix test description. fix(na): apply default sigint handler. --- src/WorkerPool.js | 27 ++++++++------------------- test/workerPool.test.js | 2 +- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/WorkerPool.js b/src/WorkerPool.js index 2178e7b..e8b0d69 100644 --- a/src/WorkerPool.js +++ b/src/WorkerPool.js @@ -258,39 +258,28 @@ export default class WorkerPool { this.activeJobs = 0; this.timeout = null; this.poolQueue = asyncQueue(this.distributeJob.bind(this), options.poolParallelJobs); - this.exited = false; + this.terminated = false; this.setupLifeCycle(); } isAbleToRun() { - return !this.exited; + return !this.terminated; } - terminate({ exit, clean }) { - if (exit) { - this.exited = true; - process.exit(0); + terminate() { + if (this.terminated) { return; } - if (clean) { - this.poolQueue.kill(); - this.disposeWorkers(true); - } + this.terminated = true; + this.poolQueue.kill(); + this.disposeWorkers(true); } setupLifeCycle() { - process.on('SIGTERM', () => { - this.terminate({ exit: true }); - }); - - process.on('SIGINT', () => { - this.terminate({ exit: true }); - }); - process.on('exit', () => { - this.terminate({ clean: true }); + this.terminate(); }); } diff --git a/test/workerPool.test.js b/test/workerPool.test.js index 1715377..e27a564 100644 --- a/test/workerPool.test.js +++ b/test/workerPool.test.js @@ -36,7 +36,7 @@ describe('workerPool', () => { expect(workerPool.isAbleToRun()).toBe(true); }); - it('should not be able to run if the worker pool was not terminated', () => { + it('should not be able to run if the worker pool was terminated', () => { const workerPool = new WorkerPool({}); workerPool.terminate(); expect(workerPool.isAbleToRun()).toBe(false); From 399454c64c8d3623ebfd1164e436ebd15f5bb1e8 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 9 Jan 2019 21:23:29 +0000 Subject: [PATCH 5/6] fix(na): call to terminate with bad arguments on disposeWorkers. --- src/WorkerPool.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/WorkerPool.js b/src/WorkerPool.js index e8b0d69..4de6a7c 100644 --- a/src/WorkerPool.js +++ b/src/WorkerPool.js @@ -278,6 +278,14 @@ export default class WorkerPool { } setupLifeCycle() { + process.on('SIGTERM', () => { + process.exit(1); + }); + + process.on('SIGINT', () => { + process.exit(1); + }); + process.on('exit', () => { this.terminate(); }); @@ -333,7 +341,7 @@ export default class WorkerPool { disposeWorkers(fromTerminate) { if (!this.options.poolRespawn && !fromTerminate) { - this.terminate({ exit: true }); + this.terminate(); return; } From 2448a6c7450c9c27ee4d01093d23cb3f71deb8a3 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 9 Jan 2019 21:24:28 +0000 Subject: [PATCH 6/6] fix(na): remove overrides for sigint and sigterm. --- src/WorkerPool.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/WorkerPool.js b/src/WorkerPool.js index 4de6a7c..6e917eb 100644 --- a/src/WorkerPool.js +++ b/src/WorkerPool.js @@ -278,14 +278,6 @@ export default class WorkerPool { } setupLifeCycle() { - process.on('SIGTERM', () => { - process.exit(1); - }); - - process.on('SIGINT', () => { - process.exit(1); - }); - process.on('exit', () => { this.terminate(); });