From f2fdaa86fd9e6f3a5dc0b3146c065e3a7bfb44e0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 12 Feb 2021 21:45:45 +0100 Subject: [PATCH] Add protected removeWorker member function (#124) --- package-lock.json | 6 +++--- src/pools/abstract-pool.ts | 7 +++++++ src/pools/cluster/dynamic.ts | 5 +---- src/pools/cluster/fixed.ts | 1 + src/pools/thread/dynamic.ts | 5 +---- src/pools/thread/fixed.ts | 1 + src/worker/abstract-worker.ts | 2 +- src/worker/cluster-worker.ts | 2 +- src/worker/thread-worker.ts | 2 +- 9 files changed, 17 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index a48b2f51..92c73008 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1901,9 +1901,9 @@ "dev": true }, "caniuse-lite": { - "version": "1.0.30001185", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001185.tgz", - "integrity": "sha512-Fpi4kVNtNvJ15H0F6vwmXtb3tukv3Zg3qhKkOGUq7KJ1J6b9kf4dnNgtEAFXhRsJo0gNj9W60+wBvn0JcTvdTg==", + "version": "1.0.30001187", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001187.tgz", + "integrity": "sha512-w7/EP1JRZ9552CyrThUnay2RkZ1DXxKe/Q2swTC4+LElLh9RRYrL1Z+27LlakB8kzY0fSmHw9mc7XYDUKAKWMA==", "dev": true }, "caseless": { diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 6d7f5569..1ae188dd 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -102,6 +102,13 @@ export abstract class AbstractPool< } } + protected removeWorker (worker: Worker): void { + // Clean worker from data structure + const workerIndex = this.workers.indexOf(worker) + this.workers.splice(workerIndex, 1) + this.tasks.delete(worker) + } + /** * Execute the task specified into the constructor with the data parameter. * diff --git a/src/pools/cluster/dynamic.ts b/src/pools/cluster/dynamic.ts index 25306d34..25940ad8 100644 --- a/src/pools/cluster/dynamic.ts +++ b/src/pools/cluster/dynamic.ts @@ -54,10 +54,7 @@ export class DynamicClusterPool< if (message.kill) { this.sendToWorker(worker, { kill: 1 }) void this.destroyWorker(worker) - // clean workers from data structures - const workerIndex = this.workers.indexOf(worker) - this.workers.splice(workerIndex, 1) - this.tasks.delete(worker) + this.removeWorker(worker) } }) return worker diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index b4071407..65f6ce54 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -50,6 +50,7 @@ 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 51dca2f3..b63d14e2 100644 --- a/src/pools/thread/dynamic.ts +++ b/src/pools/thread/dynamic.ts @@ -54,10 +54,7 @@ export class DynamicThreadPool< if (message.kill) { this.sendToWorker(worker, { kill: 1 }) void this.destroyWorker(worker) - // clean workers from data structures - const workerIndex = this.workers.indexOf(worker) - this.workers.splice(workerIndex, 1) - this.tasks.delete(worker) + this.removeWorker(worker) } }) return worker diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index badca517..6cf8b526 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -38,6 +38,7 @@ 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/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 4b68d897..1ae7b65c 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -30,7 +30,7 @@ export abstract class AbstractWorker< this.maxInactiveTime = this.opts.maxInactiveTime ?? 1000 * 60 this.async = !!this.opts.async this.lastTask = Date.now() - if (!fn) throw new Error('Fn parameter is mandatory') + if (!fn) throw new Error('fn parameter is mandatory') // keep the worker active if (!isMain) { this.interval = setInterval( diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index 4ed74749..4deeb9ae 100644 --- a/src/worker/cluster-worker.ts +++ b/src/worker/cluster-worker.ts @@ -23,7 +23,7 @@ export class ClusterWorker< worker.on('message', (value: MessageValue) => { if (value?.data && value.id) { // here you will receive messages - // console.log('This is the main worker ' + isMain) + // console.log('This is the main worker ' + isMaster) if (this.async) { this.runInAsyncScope(this.runAsync.bind(this), this, fn, value) } else { diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index 80b06ee9..a6c9ec16 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -24,7 +24,7 @@ export class ThreadWorker< parentPort?.on('message', (value: MessageValue) => { if (value?.data && value.id) { // here you will receive messages - // console.log('This is the main worker ' + isMain) + // console.log('This is the main worker ' + isMainThread) if (this.async) { this.runInAsyncScope(this.runAsync.bind(this), this, fn, value) } else { -- 2.34.1