refactor: use pool side task functions list for hasTaskFunction()
[poolifier.git] / src / pools / abstract-pool.ts
index 534cb49080a7127f10a366cf9b99d6187f18fb61..1b53065c54e18318f1295bd68e6340dc5cbc76c1 100644 (file)
@@ -14,11 +14,14 @@ import {
   average,
   isKillBehavior,
   isPlainObject,
+  max,
   median,
+  min,
   round,
   updateMeasurementStatistics
 } from '../utils'
 import { KillBehaviors } from '../worker/worker-options'
+import type { TaskFunction } from '../worker/task-functions'
 import {
   type IPool,
   PoolEmitter,
@@ -298,7 +301,7 @@ export abstract class AbstractPool<
     }
     if (
       tasksQueueOptions?.concurrency != null &&
-      !Number.isSafeInteger(tasksQueueOptions.concurrency)
+      !Number.isSafeInteger(tasksQueueOptions?.concurrency)
     ) {
       throw new TypeError(
         'Invalid worker node tasks concurrency: must be an integer'
@@ -306,10 +309,10 @@ export abstract class AbstractPool<
     }
     if (
       tasksQueueOptions?.concurrency != null &&
-      tasksQueueOptions.concurrency <= 0
+      tasksQueueOptions?.concurrency <= 0
     ) {
       throw new RangeError(
-        `Invalid worker node tasks concurrency: ${tasksQueueOptions.concurrency} is a negative integer or zero`
+        `Invalid worker node tasks concurrency: ${tasksQueueOptions?.concurrency} is a negative integer or zero`
       )
     }
     if (tasksQueueOptions?.queueMaxSize != null) {
@@ -319,15 +322,15 @@ export abstract class AbstractPool<
     }
     if (
       tasksQueueOptions?.size != null &&
-      !Number.isSafeInteger(tasksQueueOptions.size)
+      !Number.isSafeInteger(tasksQueueOptions?.size)
     ) {
       throw new TypeError(
         'Invalid worker node tasks queue size: must be an integer'
       )
     }
-    if (tasksQueueOptions?.size != null && tasksQueueOptions.size <= 0) {
+    if (tasksQueueOptions?.size != null && tasksQueueOptions?.size <= 0) {
       throw new RangeError(
-        `Invalid worker node tasks queue size: ${tasksQueueOptions.size} is a negative integer or zero`
+        `Invalid worker node tasks queue size: ${tasksQueueOptions?.size} is a negative integer or zero`
       )
     }
   }
@@ -414,16 +417,16 @@ export abstract class AbstractPool<
         .runTime.aggregate && {
         runTime: {
           minimum: round(
-            Math.min(
+            min(
               ...this.workerNodes.map(
-                (workerNode) => workerNode.usage.runTime?.minimum ?? Infinity
+                workerNode => workerNode.usage.runTime?.minimum ?? Infinity
               )
             )
           ),
           maximum: round(
-            Math.max(
+            max(
               ...this.workerNodes.map(
-                (workerNode) => workerNode.usage.runTime?.maximum ?? -Infinity
+                workerNode => workerNode.usage.runTime?.maximum ?? -Infinity
               )
             )
           ),
@@ -457,16 +460,16 @@ export abstract class AbstractPool<
         .waitTime.aggregate && {
         waitTime: {
           minimum: round(
-            Math.min(
+            min(
               ...this.workerNodes.map(
-                (workerNode) => workerNode.usage.waitTime?.minimum ?? Infinity
+                workerNode => workerNode.usage.waitTime?.minimum ?? Infinity
               )
             )
           ),
           maximum: round(
-            Math.max(
+            max(
               ...this.workerNodes.map(
-                (workerNode) => workerNode.usage.waitTime?.maximum ?? -Infinity
+                workerNode => workerNode.usage.waitTime?.maximum ?? -Infinity
               )
             )
           ),
@@ -588,7 +591,7 @@ export abstract class AbstractPool<
    */
   private getWorkerNodeKeyByWorker (worker: Worker): number {
     return this.workerNodes.findIndex(
-      (workerNode) => workerNode.worker === worker
+      workerNode => workerNode.worker === worker
     )
   }
 
@@ -600,7 +603,7 @@ export abstract class AbstractPool<
    */
   private getWorkerNodeKeyByWorkerId (workerId: number): number {
     return this.workerNodes.findIndex(
-      (workerNode) => workerNode.info.id === workerId
+      workerNode => workerNode.info.id === workerId
     )
   }
 
@@ -704,7 +707,7 @@ export abstract class AbstractPool<
     if (this.opts.enableTasksQueue === true) {
       return (
         this.workerNodes.findIndex(
-          (workerNode) =>
+          workerNode =>
             workerNode.info.ready &&
             workerNode.usage.tasks.executing <
               (this.opts.tasksQueueOptions?.concurrency as number)
@@ -713,26 +716,79 @@ export abstract class AbstractPool<
     } else {
       return (
         this.workerNodes.findIndex(
-          (workerNode) =>
+          workerNode =>
             workerNode.info.ready && workerNode.usage.tasks.executing === 0
         ) === -1
       )
     }
   }
 
+  private sendToWorkers (message: Omit<MessageValue<Data>, 'workerId'>): number {
+    let messagesCount = 0
+    for (const [workerNodeKey] of this.workerNodes.entries()) {
+      this.sendToWorker(workerNodeKey, {
+        ...message,
+        workerId: this.getWorkerInfo(workerNodeKey).id as number
+      })
+      ++messagesCount
+    }
+    return messagesCount
+  }
+
+  /** @inheritDoc */
+  public hasTaskFunction (name: string): boolean {
+    for (const workerNode of this.workerNodes) {
+      if (
+        Array.isArray(workerNode.info.taskFunctionNames) &&
+        workerNode.info.taskFunctionNames.includes(name)
+      ) {
+        return true
+      }
+    }
+    return false
+  }
+
   /** @inheritDoc */
-  public listTaskFunctions (): string[] {
+  public addTaskFunction (name: string, taskFunction: TaskFunction): boolean {
+    this.sendToWorkers({
+      taskFunctionOperation: 'add',
+      taskFunctionName: name,
+      taskFunction: taskFunction.toString()
+    })
+    return true
+  }
+
+  /** @inheritDoc */
+  public removeTaskFunction (name: string): boolean {
+    this.sendToWorkers({
+      taskFunctionOperation: 'remove',
+      taskFunctionName: name
+    })
+    return true
+  }
+
+  /** @inheritDoc */
+  public listTaskFunctionNames (): string[] {
     for (const workerNode of this.workerNodes) {
       if (
-        Array.isArray(workerNode.info.taskFunctions) &&
-        workerNode.info.taskFunctions.length > 0
+        Array.isArray(workerNode.info.taskFunctionNames) &&
+        workerNode.info.taskFunctionNames.length > 0
       ) {
-        return workerNode.info.taskFunctions
+        return workerNode.info.taskFunctionNames
       }
     }
     return []
   }
 
+  /** @inheritDoc */
+  public setDefaultTaskFunction (name: string): boolean {
+    this.sendToWorkers({
+      taskFunctionOperation: 'default',
+      taskFunctionName: name
+    })
+    return true
+  }
+
   private shallExecuteTask (workerNodeKey: number): boolean {
     return (
       this.tasksQueueSize(workerNodeKey) === 0 &&
@@ -770,14 +826,13 @@ export abstract class AbstractPool<
       }
       const timestamp = performance.now()
       const workerNodeKey = this.chooseWorkerNode()
-      const workerInfo = this.getWorkerInfo(workerNodeKey) as WorkerInfo
       const task: Task<Data> = {
         name: name ?? DEFAULT_TASK_NAME,
         // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
         data: data ?? ({} as Data),
         transferList,
         timestamp,
-        workerId: workerInfo.id as number,
+        workerId: this.getWorkerInfo(workerNodeKey).id as number,
         taskId: randomUUID()
       }
       this.promiseResponseMap.set(task.taskId as string, {
@@ -813,7 +868,7 @@ export abstract class AbstractPool<
     workerId: number
   ): Promise<void> {
     await new Promise<void>((resolve, reject) => {
-      this.registerWorkerMessageListener(workerNodeKey, (message) => {
+      this.registerWorkerMessageListener(workerNodeKey, message => {
         if (message.kill === 'success') {
           resolve()
         } else if (message.kill === 'failure') {
@@ -920,8 +975,8 @@ export abstract class AbstractPool<
     const workerInfo = this.getWorkerInfo(workerNodeKey)
     return (
       workerInfo != null &&
-      Array.isArray(workerInfo.taskFunctions) &&
-      workerInfo.taskFunctions.length > 2
+      Array.isArray(workerInfo.taskFunctionNames) &&
+      workerInfo.taskFunctionNames.length > 2
     )
   }
 
@@ -936,7 +991,7 @@ export abstract class AbstractPool<
     ) {
       --workerTaskStatistics.executing
     }
-    if (message.taskError == null) {
+    if (message.workerError == null) {
       ++workerTaskStatistics.executed
     } else {
       ++workerTaskStatistics.failed
@@ -947,7 +1002,7 @@ export abstract class AbstractPool<
     workerUsage: WorkerUsage,
     message: MessageValue<Response>
   ): void {
-    if (message.taskError != null) {
+    if (message.workerError != null) {
       return
     }
     updateMeasurementStatistics(
@@ -974,7 +1029,7 @@ export abstract class AbstractPool<
     workerUsage: WorkerUsage,
     message: MessageValue<Response>
   ): void {
-    if (message.taskError != null) {
+    if (message.workerError != null) {
       return
     }
     const eluTaskStatisticsRequirements: MeasurementStatisticsRequirements =
@@ -1062,16 +1117,16 @@ export abstract class AbstractPool<
     worker.on('online', this.opts.onlineHandler ?? EMPTY_FUNCTION)
     worker.on('message', this.opts.messageHandler ?? EMPTY_FUNCTION)
     worker.on('error', this.opts.errorHandler ?? EMPTY_FUNCTION)
-    worker.on('error', (error) => {
+    worker.on('error', error => {
       const workerNodeKey = this.getWorkerNodeKeyByWorker(worker)
-      const workerInfo = this.getWorkerInfo(workerNodeKey) as WorkerInfo
+      const workerInfo = this.getWorkerInfo(workerNodeKey)
       workerInfo.ready = false
       this.workerNodes[workerNodeKey].closeChannel()
       this.emitter?.emit(PoolEvents.error, error)
       if (
         this.opts.restartWorkerOnError === true &&
-        !this.starting &&
-        this.started
+        this.started &&
+        !this.starting
       ) {
         if (workerInfo.dynamic) {
           this.createAndSetupDynamicWorkerNode()
@@ -1102,7 +1157,7 @@ export abstract class AbstractPool<
    */
   protected createAndSetupDynamicWorkerNode (): number {
     const workerNodeKey = this.createAndSetupWorkerNode()
-    this.registerWorkerMessageListener(workerNodeKey, (message) => {
+    this.registerWorkerMessageListener(workerNodeKey, message => {
       const localWorkerNodeKey = this.getWorkerNodeKeyByWorkerId(
         message.workerId
       )
@@ -1117,12 +1172,12 @@ export abstract class AbstractPool<
               workerUsage.tasks.executing === 0 &&
               this.tasksQueueSize(localWorkerNodeKey) === 0)))
       ) {
-        this.destroyWorkerNode(localWorkerNodeKey).catch((error) => {
+        this.destroyWorkerNode(localWorkerNodeKey).catch(error => {
           this.emitter?.emit(PoolEvents.error, error)
         })
       }
     })
-    const workerInfo = this.getWorkerInfo(workerNodeKey) as WorkerInfo
+    const workerInfo = this.getWorkerInfo(workerNodeKey)
     this.sendToWorker(workerNodeKey, {
       checkActive: true,
       workerId: workerInfo.id as number
@@ -1193,37 +1248,31 @@ export abstract class AbstractPool<
         elu: this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
           .elu.aggregate
       },
-      workerId: (this.getWorkerInfo(workerNodeKey) as WorkerInfo).id as number
+      workerId: this.getWorkerInfo(workerNodeKey).id as number
     })
   }
 
   private redistributeQueuedTasks (workerNodeKey: number): void {
     while (this.tasksQueueSize(workerNodeKey) > 0) {
-      let destinationWorkerNodeKey!: number
-      let minQueuedTasks = Infinity
-      for (const [workerNodeId, workerNode] of this.workerNodes.entries()) {
-        if (workerNode.info.ready && workerNodeId !== workerNodeKey) {
-          if (workerNode.usage.tasks.queued === 0) {
-            destinationWorkerNodeKey = workerNodeId
-            break
-          }
-          if (workerNode.usage.tasks.queued < minQueuedTasks) {
-            minQueuedTasks = workerNode.usage.tasks.queued
-            destinationWorkerNodeKey = workerNodeId
-          }
-        }
+      const destinationWorkerNodeKey = this.workerNodes.reduce(
+        (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
+          return workerNode.info.ready &&
+            workerNode.usage.tasks.queued <
+              workerNodes[minWorkerNodeKey].usage.tasks.queued
+            ? workerNodeKey
+            : minWorkerNodeKey
+        },
+        0
+      )
+      const destinationWorkerNode = this.workerNodes[destinationWorkerNodeKey]
+      const task = {
+        ...(this.dequeueTask(workerNodeKey) as Task<Data>),
+        workerId: destinationWorkerNode.info.id as number
       }
-      if (destinationWorkerNodeKey != null) {
-        const destinationWorkerNode = this.workerNodes[destinationWorkerNodeKey]
-        const task = {
-          ...(this.dequeueTask(workerNodeKey) as Task<Data>),
-          workerId: destinationWorkerNode.info.id as number
-        }
-        if (this.shallExecuteTask(destinationWorkerNodeKey)) {
-          this.executeTask(destinationWorkerNodeKey, task)
-        } else {
-          this.enqueueTask(destinationWorkerNodeKey, task)
-        }
+      if (this.shallExecuteTask(destinationWorkerNodeKey)) {
+        this.executeTask(destinationWorkerNodeKey, task)
+      } else {
+        this.enqueueTask(destinationWorkerNodeKey, task)
       }
     }
   }
@@ -1256,30 +1305,26 @@ export abstract class AbstractPool<
         (workerNodeA, workerNodeB) =>
           workerNodeB.usage.tasks.queued - workerNodeA.usage.tasks.queued
       )
-    for (const sourceWorkerNode of workerNodes) {
-      if (sourceWorkerNode.usage.tasks.queued === 0) {
-        break
+    const sourceWorkerNode = workerNodes.find(
+      workerNode =>
+        workerNode.info.ready &&
+        workerNode.info.id !== workerId &&
+        workerNode.usage.tasks.queued > 0
+    )
+    if (sourceWorkerNode != null) {
+      const task = {
+        ...(sourceWorkerNode.popTask() as Task<Data>),
+        workerId: destinationWorkerNode.info.id as number
       }
-      if (
-        sourceWorkerNode.info.ready &&
-        sourceWorkerNode.info.id !== workerId &&
-        sourceWorkerNode.usage.tasks.queued > 0
-      ) {
-        const task = {
-          ...(sourceWorkerNode.popTask() as Task<Data>),
-          workerId: destinationWorkerNode.info.id as number
-        }
-        if (this.shallExecuteTask(destinationWorkerNodeKey)) {
-          this.executeTask(destinationWorkerNodeKey, task)
-        } else {
-          this.enqueueTask(destinationWorkerNodeKey, task)
-        }
-        this.updateTaskStolenStatisticsWorkerUsage(
-          destinationWorkerNodeKey,
-          task.name as string
-        )
-        break
+      if (this.shallExecuteTask(destinationWorkerNodeKey)) {
+        this.executeTask(destinationWorkerNodeKey, task)
+      } else {
+        this.enqueueTask(destinationWorkerNodeKey, task)
       }
+      this.updateTaskStolenStatisticsWorkerUsage(
+        destinationWorkerNodeKey,
+        task.name as string
+      )
     }
   }
 
@@ -1327,21 +1372,21 @@ export abstract class AbstractPool<
    * @returns The listener function to execute when a message is received from a worker.
    */
   protected workerListener (): (message: MessageValue<Response>) => void {
-    return (message) => {
+    return message => {
       this.checkMessageWorkerId(message)
-      if (message.ready != null && message.taskFunctions != null) {
+      if (message.ready != null && message.taskFunctionNames != null) {
         // Worker ready response received from worker
         this.handleWorkerReadyResponse(message)
       } else if (message.taskId != null) {
         // Task execution response received from worker
         this.handleTaskExecutionResponse(message)
-      } else if (message.taskFunctions != null) {
-        // Task functions message received from worker
-        (
-          this.getWorkerInfo(
-            this.getWorkerNodeKeyByWorkerId(message.workerId)
-          ) as WorkerInfo
-        ).taskFunctions = message.taskFunctions
+      } else if (message.taskFunctionNames != null) {
+        // Task function names message received from worker
+        this.getWorkerInfo(
+          this.getWorkerNodeKeyByWorkerId(message.workerId)
+        ).taskFunctionNames = message.taskFunctionNames
+      } else if (message.taskFunctionOperation != null) {
+        // Task function operation response received from worker
       }
     }
   }
@@ -1352,21 +1397,21 @@ export abstract class AbstractPool<
     }
     const workerInfo = this.getWorkerInfo(
       this.getWorkerNodeKeyByWorkerId(message.workerId)
-    ) as WorkerInfo
+    )
     workerInfo.ready = message.ready as boolean
-    workerInfo.taskFunctions = message.taskFunctions
+    workerInfo.taskFunctionNames = message.taskFunctionNames
     if (this.emitter != null && this.ready) {
       this.emitter.emit(PoolEvents.ready, this.info)
     }
   }
 
   private handleTaskExecutionResponse (message: MessageValue<Response>): void {
-    const { taskId, taskError, data } = message
+    const { taskId, workerError, data } = message
     const promiseResponse = this.promiseResponseMap.get(taskId as string)
     if (promiseResponse != null) {
-      if (taskError != null) {
-        this.emitter?.emit(PoolEvents.taskError, taskError)
-        promiseResponse.reject(taskError.message)
+      if (workerError != null) {
+        this.emitter?.emit(PoolEvents.taskError, workerError)
+        promiseResponse.reject(workerError.message)
       } else {
         promiseResponse.resolve(data as Response)
       }
@@ -1414,8 +1459,8 @@ export abstract class AbstractPool<
    * @param workerNodeKey - The worker node key.
    * @returns The worker information.
    */
-  protected getWorkerInfo (workerNodeKey: number): WorkerInfo | undefined {
-    return this.workerNodes[workerNodeKey]?.info
+  protected getWorkerInfo (workerNodeKey: number): WorkerInfo {
+    return this.workerNodes[workerNodeKey].info
   }
 
   /**
@@ -1467,7 +1512,7 @@ export abstract class AbstractPool<
     return (
       this.opts.enableTasksQueue === true &&
       this.workerNodes.findIndex(
-        (workerNode) => !workerNode.hasBackPressure()
+        workerNode => !workerNode.hasBackPressure()
       ) === -1
     )
   }