From: Shinigami Date: Sat, 13 Feb 2021 23:05:23 +0000 (+0100) Subject: Clean worker from pool after it was destroyed (#146) X-Git-Tag: v2.0.0-beta.2~31 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;ds=sidebyside;h=45dbbb14328a173cad05ddcf21b5acf7f6460bb8;hp=280c2a7728fbeb53612d8bc115a295d0255dd991;p=poolifier.git Clean worker from pool after it was destroyed (#146) Co-authored-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 6e4465ed..f6ed280c 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -24,6 +24,7 @@ export interface IWorker { on(event: 'error', handler: ErrorHandler): void on(event: 'online', handler: OnlineHandler): void on(event: 'exit', handler: ExitHandler): void + once(event: 'exit', handler: ExitHandler): void } /** @@ -161,9 +162,7 @@ export abstract class AbstractPool< } public async destroy (): Promise { - for (const worker of this.workers) { - await this.destroyWorker(worker) - } + await Promise.all(this.workers.map(worker => this.destroyWorker(worker))) } /** @@ -288,8 +287,8 @@ export abstract class AbstractPool< worker.on('error', this.opts.errorHandler ?? (() => {})) worker.on('online', this.opts.onlineHandler ?? (() => {})) - // TODO handle properly when a worker exit worker.on('exit', this.opts.exitHandler ?? (() => {})) + worker.once('exit', () => this.removeWorker(worker)) this.workers.push(worker) diff --git a/src/pools/cluster/dynamic.ts b/src/pools/cluster/dynamic.ts index 6d665cef..432feb3a 100644 --- a/src/pools/cluster/dynamic.ts +++ b/src/pools/cluster/dynamic.ts @@ -68,7 +68,6 @@ export class DynamicClusterPool< if (message.kill) { this.sendToWorker(worker, { kill: 1 }) void this.destroyWorker(worker) - this.removeWorker(worker) } }) return worker diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 0d8021eb..6620d5f3 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -60,7 +60,6 @@ export class FixedClusterPool< protected destroyWorker (worker: Worker): void { worker.kill() - // FIXME: The tests are currently failing, so these must be changed first } protected sendToWorker (worker: Worker, message: MessageValue): void { diff --git a/src/pools/thread/dynamic.ts b/src/pools/thread/dynamic.ts index c1033315..9ab6bf30 100644 --- a/src/pools/thread/dynamic.ts +++ b/src/pools/thread/dynamic.ts @@ -68,7 +68,6 @@ export class DynamicThreadPool< if (message.kill) { this.sendToWorker(worker, { kill: 1 }) void this.destroyWorker(worker) - this.removeWorker(worker) } }) return worker diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index 74c14ff2..bfd6f016 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -48,7 +48,6 @@ export class FixedThreadPool< worker: ThreadWorkerWithMessageChannel ): Promise { await worker.terminate() - // FIXME: The tests are currently failing, so these must be changed first } protected sendToWorker ( diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index aae9127f..d22f697a 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -120,7 +120,7 @@ describe('Fixed cluster pool test suite ', () => { closedWorkers++ }) }) - pool.destroy() + await pool.destroy() await new Promise(resolve => setTimeout(resolve, 200)) expect(closedWorkers).toBe(numberOfWorkers) })