Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions src/WorkerPool.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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