Skip to content

Commit aec90b0

Browse files
authored
Fix lifecycle handling for signals (#57)
* 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.
1 parent b5fc4f7 commit aec90b0

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

src/WorkerPool.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -267,26 +267,19 @@ export default class WorkerPool {
267267
return !this.terminated;
268268
}
269269

270-
terminate(force) {
271-
if (!this.terminated) {
272-
this.terminated = true;
273-
274-
this.poolQueue.kill();
275-
this.disposeWorkers(force);
270+
terminate() {
271+
if (this.terminated) {
272+
return;
276273
}
274+
275+
this.terminated = true;
276+
this.poolQueue.kill();
277+
this.disposeWorkers(true);
277278
}
278279

279280
setupLifeCycle() {
280-
process.on('SIGTERM', () => {
281-
this.terminate(true);
282-
});
283-
284-
process.on('SIGINT', () => {
285-
this.terminate(true);
286-
});
287-
288281
process.on('exit', () => {
289-
this.terminate(true);
282+
this.terminate();
290283
});
291284
}
292285

@@ -338,15 +331,17 @@ export default class WorkerPool {
338331
}
339332
}
340333

341-
disposeWorkers(force) {
342-
if (this.activeJobs === 0 || force) {
334+
disposeWorkers(fromTerminate) {
335+
if (!this.options.poolRespawn && !fromTerminate) {
336+
this.terminate();
337+
return;
338+
}
339+
340+
if (this.activeJobs === 0 || fromTerminate) {
343341
for (const worker of this.workers) {
344342
worker.dispose();
345343
}
346344
this.workers.clear();
347345
}
348-
if (!this.options.poolRespawn) {
349-
this.terminate();
350-
}
351346
}
352347
}

test/workerPool.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('workerPool', () => {
3636
expect(workerPool.isAbleToRun()).toBe(true);
3737
});
3838

39-
it('should not be able to run if the worker pool was not terminated', () => {
39+
it('should not be able to run if the worker pool was terminated', () => {
4040
const workerPool = new WorkerPool({});
4141
workerPool.terminate();
4242
expect(workerPool.isAbleToRun()).toBe(false);

0 commit comments

Comments
 (0)