Skip to content

Commit

Permalink
Fix lifecycle handling for signals (#57)
Browse files Browse the repository at this point in the history
* fix(na): fix-lifecycle-handling-for-signals

* fix(na): allow disposeWorkers to be called from terminate function.

* refact(na): rename terminated to exited on WorkerPool.

* test(na): fix test description. fix(na): apply default sigint handler.

* fix(na): call to terminate with bad arguments on disposeWorkers.

* fix(na): remove overrides for sigint and sigterm.
  • Loading branch information
mistic committed Jan 18, 2019
1 parent b5fc4f7 commit aec90b0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
35 changes: 15 additions & 20 deletions src/WorkerPool.js
Expand Up @@ -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();
});
}

Expand Down Expand Up @@ -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();
}
}
}
2 changes: 1 addition & 1 deletion test/workerPool.test.js
Expand Up @@ -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);
Expand Down

0 comments on commit aec90b0

Please sign in to comment.