From ef3891a3674f862e07006d0e1961feb12c41d1d8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 15 Aug 2023 18:53:24 +0200 Subject: [PATCH] feat: add destroy event to pool API MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 6 +++++- src/pools/abstract-pool.ts | 1 + src/pools/pool.ts | 6 ++++-- tests/pools/cluster/dynamic.test.js | 3 +++ tests/pools/cluster/fixed.test.js | 3 +++ tests/pools/thread/dynamic.test.js | 3 +++ tests/pools/thread/fixed.test.js | 3 +++ 7 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de0d866f..6f63be84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,12 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `destroy` event to pool API. + ## [2.6.26] - 2023-08-15 ### Added - Add kill handler to worker options allowing to execute custom code when worker is killed. -- Add listTaskFunctions() method to pool API. +- Add `listTaskFunctions()` method to pool API. - SMTP server pool example: nodemailer. ## [2.6.25] - 2023-08-13 diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index d36e8a47..8fb25b64 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -721,6 +721,7 @@ export abstract class AbstractPool< await this.destroyWorkerNode(workerNodeKey) }) ) + this.emitter?.emit(PoolEvents.destroy) } protected async sendKillMessageToWorker ( diff --git a/src/pools/pool.ts b/src/pools/pool.ts index 0bde23be..24b506ca 100644 --- a/src/pools/pool.ts +++ b/src/pools/pool.ts @@ -42,9 +42,10 @@ export class PoolEmitter extends EventEmitter {} * Enumeration of pool events. */ export const PoolEvents = Object.freeze({ - full: 'full', ready: 'ready', busy: 'busy', + full: 'full', + destroy: 'destroy', error: 'error', taskError: 'taskError' } as const) @@ -183,9 +184,10 @@ export interface IPool< * * Events that can currently be listened to: * - * - `'full'`: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected. * - `'ready'`: Emitted when the number of workers created in the pool has reached the minimum size expected and are ready. * - `'busy'`: Emitted when the number of workers created in the pool has reached the maximum size expected and are executing at least one task. + * - `'full'`: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected. + * - '`destroy`': Emitted when the pool is destroyed. * - `'error'`: Emitted when an uncaught error occurs. * - `'taskError'`: Emitted when an error occurs while executing a task. */ diff --git a/tests/pools/cluster/dynamic.test.js b/tests/pools/cluster/dynamic.test.js index 724c0c91..c91447ee 100644 --- a/tests/pools/cluster/dynamic.test.js +++ b/tests/pools/cluster/dynamic.test.js @@ -59,9 +59,12 @@ describe('Dynamic cluster pool test suite', () => { it('Shutdown test', async () => { const exitPromise = waitWorkerEvents(pool, 'exit', min) + let poolDestroy = 0 + pool.emitter.on(PoolEvents.destroy, () => ++poolDestroy) await pool.destroy() const numberOfExitEvents = await exitPromise expect(numberOfExitEvents).toBe(min) + expect(poolDestroy).toBe(1) }) it('Validation of inputs test', () => { diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index 30dbc363..749cacaa 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -220,9 +220,12 @@ describe('Fixed cluster pool test suite', () => { it('Shutdown test', async () => { const exitPromise = waitWorkerEvents(pool, 'exit', numberOfWorkers) + let poolDestroy = 0 + pool.emitter.on(PoolEvents.destroy, () => ++poolDestroy) await pool.destroy() const numberOfExitEvents = await exitPromise expect(numberOfExitEvents).toBe(numberOfWorkers) + expect(poolDestroy).toBe(1) }) it('Verify that cluster pool options are checked', async () => { diff --git a/tests/pools/thread/dynamic.test.js b/tests/pools/thread/dynamic.test.js index 6e6142b7..f85d44ff 100644 --- a/tests/pools/thread/dynamic.test.js +++ b/tests/pools/thread/dynamic.test.js @@ -59,9 +59,12 @@ describe('Dynamic thread pool test suite', () => { it('Shutdown test', async () => { const exitPromise = waitWorkerEvents(pool, 'exit', min) + let poolDestroy = 0 + pool.emitter.on(PoolEvents.destroy, () => ++poolDestroy) await pool.destroy() const numberOfExitEvents = await exitPromise expect(numberOfExitEvents).toBe(min) + expect(poolDestroy).toBe(1) }) it('Validation of inputs test', () => { diff --git a/tests/pools/thread/fixed.test.js b/tests/pools/thread/fixed.test.js index 26546d5e..9ccbf67c 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -250,9 +250,12 @@ describe('Fixed thread pool test suite', () => { it('Shutdown test', async () => { const exitPromise = waitWorkerEvents(pool, 'exit', numberOfThreads) + let poolDestroy = 0 + pool.emitter.on(PoolEvents.destroy, () => ++poolDestroy) await pool.destroy() const numberOfExitEvents = await exitPromise expect(numberOfExitEvents).toBe(numberOfThreads) + expect(poolDestroy).toBe(1) }) it('Verify that thread pool options are checked', async () => { -- 2.34.1