diff --git a/src/WorkerPool.js b/src/WorkerPool.js index e6132bb..6e917eb 100644 --- a/src/WorkerPool.js +++ b/src/WorkerPool.js @@ -267,26 +267,19 @@ export default class WorkerPool { return !this.terminated; } - terminate(force) { - if (!this.terminated) { - this.terminated = true; - - this.poolQueue.kill(); - this.disposeWorkers(force); + terminate() { + if (this.terminated) { + return; } + + this.terminated = true; + this.poolQueue.kill(); + this.disposeWorkers(true); } setupLifeCycle() { - process.on('SIGTERM', () => { - this.terminate(true); - }); - - process.on('SIGINT', () => { - this.terminate(true); - }); - process.on('exit', () => { - this.terminate(true); + this.terminate(); }); } @@ -338,15 +331,17 @@ export default class WorkerPool { } } - disposeWorkers(force) { - if (this.activeJobs === 0 || force) { + disposeWorkers(fromTerminate) { + if (!this.options.poolRespawn && !fromTerminate) { + this.terminate(); + return; + } + + if (this.activeJobs === 0 || fromTerminate) { for (const worker of this.workers) { worker.dispose(); } this.workers.clear(); } - if (!this.options.poolRespawn) { - 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);