fix: ensure task function ops sync worker choice strategies
[poolifier.git] / src / pools / worker-node.ts
index 308215176edb515851c2349ba550d3cc50e2611b..a2878d8980e4c300a7dc0712c7fbbbe971868f27 100644 (file)
@@ -1,9 +1,16 @@
-import { MessageChannel } from 'node:worker_threads'
 import { EventEmitter } from 'node:events'
+import { MessageChannel } from 'node:worker_threads'
+
 import { CircularArray } from '../circular-array.js'
+import { Deque } from '../deque.js'
 import type { Task } from '../utility-types.js'
 import { DEFAULT_TASK_NAME } from '../utils.js'
-import { Deque } from '../deque.js'
+import {
+  checkWorkerNodeArguments,
+  createWorker,
+  getWorkerId,
+  getWorkerType
+} from './utils.js'
 import {
   type EventHandler,
   type IWorker,
@@ -15,12 +22,6 @@ import {
   WorkerTypes,
   type WorkerUsage
 } from './worker.js'
-import {
-  checkWorkerNodeArguments,
-  createWorker,
-  getWorkerId,
-  getWorkerType
-} from './utils.js'
 
 /**
  * Worker node.
@@ -137,6 +138,7 @@ export class WorkerNode<Worker extends IWorker, Data = unknown>
     this.removeAllListeners()
     switch (this.info.type) {
       case WorkerTypes.thread:
+        this.worker.unref?.()
         await this.worker.terminate?.()
         break
       case WorkerTypes.cluster:
@@ -167,21 +169,21 @@ export class WorkerNode<Worker extends IWorker, Data = unknown>
 
   /** @inheritdoc */
   public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined {
-    if (!Array.isArray(this.info.taskFunctionNames)) {
+    if (!Array.isArray(this.info.taskFunctionsProperties)) {
       throw new Error(
-        `Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined`
+        `Cannot get task function worker usage for task function name '${name}' when task function properties list is not yet defined`
       )
     }
     if (
-      Array.isArray(this.info.taskFunctionNames) &&
-      this.info.taskFunctionNames.length < 3
+      Array.isArray(this.info.taskFunctionsProperties) &&
+      this.info.taskFunctionsProperties.length < 3
     ) {
       throw new Error(
-        `Cannot get task function worker usage for task function name '${name}' when task function names list has less than 3 elements`
+        `Cannot get task function worker usage for task function name '${name}' when task function properties list has less than 3 elements`
       )
     }
     if (name === DEFAULT_TASK_NAME) {
-      name = this.info.taskFunctionNames[1]
+      name = this.info.taskFunctionsProperties[1].name
     }
     if (!this.taskFunctionsUsage.has(name)) {
       this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name))
@@ -260,7 +262,7 @@ export class WorkerNode<Worker extends IWorker, Data = unknown>
         if (
           (task.name === DEFAULT_TASK_NAME &&
             // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-            name === this.info.taskFunctionNames![1]) ||
+            name === this.info.taskFunctionsProperties![1].name) ||
           (task.name !== DEFAULT_TASK_NAME && name === task.name)
         ) {
           ++taskFunctionQueueSize