Add protected removeWorker member function (#124)
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 12 Feb 2021 20:45:45 +0000 (21:45 +0100)
committerGitHub <noreply@github.com>
Fri, 12 Feb 2021 20:45:45 +0000 (21:45 +0100)
package-lock.json
src/pools/abstract-pool.ts
src/pools/cluster/dynamic.ts
src/pools/cluster/fixed.ts
src/pools/thread/dynamic.ts
src/pools/thread/fixed.ts
src/worker/abstract-worker.ts
src/worker/cluster-worker.ts
src/worker/thread-worker.ts

index a48b2f51a365c2943c03c6bd4974ba617f8e99ae..92c730085a5d9de7a97d31cf76d5168603e2a4ee 100644 (file)
       "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": {
index 6d7f5569f75f9027cae94136eccc9f1dcb023c6a..1ae188dd53cb2f194e586a0c82087db7f9ed1dc8 100644 (file)
@@ -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.
    *
index 25306d34215b396bb5f06e2ab740dd11c9379dd3..25940ad84340f43f3e7b0abf5cec7ddf3edbc8d4 100644 (file)
@@ -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
index b4071407f6e23864f2f34e4ea4da5d5d042333fc..65f6ce54c738e1b857a7c1d49f68811ca57f5697 100644 (file)
@@ -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<Data>): void {
index 51dca2f33698aede373eb2b95871f6d0dbc5ca84..b63d14e289df735a2683413c3b019aa0b12efadb 100644 (file)
@@ -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
index badca517585d346072debabc0c306253a513c08c..6cf8b526baf51d479a73c82a1bff9444718d9ffa 100644 (file)
@@ -38,6 +38,7 @@ export class FixedThreadPool<
     worker: ThreadWorkerWithMessageChannel
   ): Promise<void> {
     await worker.terminate()
+    // FIXME: The tests are currently failing, so these must be changed first
   }
 
   protected sendToWorker (
index 4b68d8975a3cc21306a1d4ff19ab9592b5c48297..1ae7b65c7efbe67c6e9efafedd44301c5a80f15c 100644 (file)
@@ -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(
index 4ed74749997209ec6202659a7499771d30ea7513..4deeb9ae6ebebb2cb960ebf4b8fd489c0aa3f258 100644 (file)
@@ -23,7 +23,7 @@ export class ClusterWorker<
     worker.on('message', (value: MessageValue<Data>) => {
       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 {
index 80b06ee9108f1b923a8a5af880e6803f678f649d..a6c9ec16b64760e8f4a79feeebef0339634ff660 100644 (file)
@@ -24,7 +24,7 @@ export class ThreadWorker<
     parentPort?.on('message', (value: MessageValue<Data>) => {
       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 {