perf: optimize task(s) stealing
[poolifier.git] / src / worker / abstract-worker.ts
index ae95dcf825c40640b019cbc8027123ae9b2257b0..bb6ac5666e0252195b4f1cafbda38471ec7fcf07 100644 (file)
@@ -1,31 +1,35 @@
 import type { Worker } from 'node:cluster'
-import type { MessagePort } from 'node:worker_threads'
 import { performance } from 'node:perf_hooks'
+import type { MessagePort } from 'node:worker_threads'
+
 import type {
   MessageValue,
   Task,
+  TaskFunctionProperties,
   TaskPerformance,
   WorkerStatistics
-} from '../utility-types'
+} from '../utility-types.js'
 import {
+  buildTaskFunctionProperties,
   DEFAULT_TASK_NAME,
   EMPTY_FUNCTION,
   isAsyncFunction,
   isPlainObject
-} from '../utils'
-import { KillBehaviors, type WorkerOptions } from './worker-options'
+} from '../utils.js'
 import type {
   TaskAsyncFunction,
   TaskFunction,
+  TaskFunctionObject,
   TaskFunctionOperationResult,
   TaskFunctions,
   TaskSyncFunction
-} from './task-functions'
+} from './task-functions.js'
 import {
   checkTaskFunctionName,
-  checkValidTaskFunctionEntry,
+  checkValidTaskFunctionObjectEntry,
   checkValidWorkerOptions
-} from './utils'
+} from './utils.js'
+import { KillBehaviors, type WorkerOptions } from './worker-options.js'
 
 const DEFAULT_MAX_INACTIVE_TIME = 60000
 const DEFAULT_WORKER_OPTIONS: WorkerOptions = {
@@ -61,9 +65,9 @@ export abstract class AbstractWorker<
    */
   protected abstract id: number
   /**
-   * Task function(s) processed by the worker when the pool's `execution` function is invoked.
+   * Task function object(s) processed by the worker when the pool's `execution` function is invoked.
    */
-  protected taskFunctions!: Map<string, TaskFunction<Data, Response>>
+  protected taskFunctions!: Map<string, TaskFunctionObject<Data, Response>>
   /**
    * Timestamp of the last task processed by this worker.
    */
@@ -71,11 +75,12 @@ export abstract class AbstractWorker<
   /**
    * Performance statistics computation requirements.
    */
-  protected statistics!: WorkerStatistics
+  protected statistics?: WorkerStatistics
   /**
    * Handler id of the `activeInterval` worker activity check.
    */
   protected activeInterval?: NodeJS.Timeout
+
   /**
    * Constructs a new poolifier worker.
    *
@@ -85,8 +90,8 @@ export abstract class AbstractWorker<
    * @param opts - Options for the worker.
    */
   public constructor (
-    protected readonly isMain: boolean,
-    private readonly mainWorker: MainWorker,
+    protected readonly isMain: boolean | undefined,
+    private readonly mainWorker: MainWorker | undefined | null,
     taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>,
     protected opts: WorkerOptions = DEFAULT_WORKER_OPTIONS
   ) {
@@ -112,32 +117,41 @@ export abstract class AbstractWorker<
    * @param taskFunctions - The task function(s) parameter that should be checked.
    */
   private checkTaskFunctions (
-    taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>
+    taskFunctions:
+    | TaskFunction<Data, Response>
+    | TaskFunctions<Data, Response>
+    | undefined
   ): void {
     if (taskFunctions == null) {
       throw new Error('taskFunctions parameter is mandatory')
     }
-    this.taskFunctions = new Map<string, TaskFunction<Data, Response>>()
+    this.taskFunctions = new Map<string, TaskFunctionObject<Data, Response>>()
     if (typeof taskFunctions === 'function') {
-      const boundFn = taskFunctions.bind(this)
-      this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
+      const fnObj = { taskFunction: taskFunctions.bind(this) }
+      this.taskFunctions.set(DEFAULT_TASK_NAME, fnObj)
       this.taskFunctions.set(
         typeof taskFunctions.name === 'string' &&
-        taskFunctions.name.trim().length > 0
+          taskFunctions.name.trim().length > 0
           ? taskFunctions.name
           : 'fn1',
-        boundFn
+        fnObj
       )
     } else if (isPlainObject(taskFunctions)) {
       let firstEntry = true
-      for (const [name, fn] of Object.entries(taskFunctions)) {
-        checkValidTaskFunctionEntry<Data, Response>(name, fn)
-        const boundFn = fn.bind(this)
+      for (let [name, fnObj] of Object.entries(taskFunctions)) {
+        if (typeof fnObj === 'function') {
+          fnObj = { taskFunction: fnObj } satisfies TaskFunctionObject<
+          Data,
+          Response
+          >
+        }
+        checkValidTaskFunctionObjectEntry<Data, Response>(name, fnObj)
+        fnObj.taskFunction = fnObj.taskFunction.bind(this)
         if (firstEntry) {
-          this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
+          this.taskFunctions.set(DEFAULT_TASK_NAME, fnObj)
           firstEntry = false
         }
-        this.taskFunctions.set(name, boundFn)
+        this.taskFunctions.set(name, fnObj)
       }
       if (firstEntry) {
         throw new Error('taskFunctions parameter object is empty')
@@ -174,7 +188,7 @@ export abstract class AbstractWorker<
    */
   public addTaskFunction (
     name: string,
-    fn: TaskFunction<Data, Response>
+    fn: TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>
   ): TaskFunctionOperationResult {
     try {
       checkTaskFunctionName(name)
@@ -183,18 +197,19 @@ export abstract class AbstractWorker<
           'Cannot add a task function with the default reserved name'
         )
       }
-      if (typeof fn !== 'function') {
-        throw new TypeError('fn parameter is not a function')
+      if (typeof fn === 'function') {
+        fn = { taskFunction: fn } satisfies TaskFunctionObject<Data, Response>
       }
-      const boundFn = fn.bind(this)
+      checkValidTaskFunctionObjectEntry<Data, Response>(name, fn)
+      fn.taskFunction = fn.taskFunction.bind(this)
       if (
         this.taskFunctions.get(name) ===
         this.taskFunctions.get(DEFAULT_TASK_NAME)
       ) {
-        this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
+        this.taskFunctions.set(DEFAULT_TASK_NAME, fn)
       }
-      this.taskFunctions.set(name, boundFn)
-      this.sendTaskFunctionNamesToMainWorker()
+      this.taskFunctions.set(name, fn)
+      this.sendTaskFunctionsPropertiesToMainWorker()
       return { status: true }
     } catch (error) {
       return { status: false, error: error as Error }
@@ -224,7 +239,7 @@ export abstract class AbstractWorker<
         )
       }
       const deleteStatus = this.taskFunctions.delete(name)
-      this.sendTaskFunctionNamesToMainWorker()
+      this.sendTaskFunctionsPropertiesToMainWorker()
       return { status: deleteStatus }
     } catch (error) {
       return { status: false, error: error as Error }
@@ -232,28 +247,38 @@ export abstract class AbstractWorker<
   }
 
   /**
-   * Lists the names of the worker's task functions.
+   * Lists the properties of the worker's task functions.
    *
-   * @returns The names of the worker's task functions.
+   * @returns The properties of the worker's task functions.
    */
-  public listTaskFunctionNames (): string[] {
-    const names: string[] = [...this.taskFunctions.keys()]
-    let defaultTaskFunctionName: string = DEFAULT_TASK_NAME
-    for (const [name, fn] of this.taskFunctions) {
+  public listTaskFunctionsProperties (): TaskFunctionProperties[] {
+    let defaultTaskFunctionName = DEFAULT_TASK_NAME
+    for (const [name, fnObj] of this.taskFunctions) {
       if (
         name !== DEFAULT_TASK_NAME &&
-        fn === this.taskFunctions.get(DEFAULT_TASK_NAME)
+        fnObj === this.taskFunctions.get(DEFAULT_TASK_NAME)
       ) {
         defaultTaskFunctionName = name
         break
       }
     }
+    const taskFunctionsProperties: TaskFunctionProperties[] = []
+    for (const [name, fnObj] of this.taskFunctions) {
+      if (name === DEFAULT_TASK_NAME || name === defaultTaskFunctionName) {
+        continue
+      }
+      taskFunctionsProperties.push(buildTaskFunctionProperties(name, fnObj))
+    }
     return [
-      names[names.indexOf(DEFAULT_TASK_NAME)],
-      defaultTaskFunctionName,
-      ...names.filter(
-        name => name !== DEFAULT_TASK_NAME && name !== defaultTaskFunctionName
-      )
+      buildTaskFunctionProperties(
+        DEFAULT_TASK_NAME,
+        this.taskFunctions.get(DEFAULT_TASK_NAME)
+      ),
+      buildTaskFunctionProperties(
+        defaultTaskFunctionName,
+        this.taskFunctions.get(defaultTaskFunctionName)
+      ),
+      ...taskFunctionsProperties
     ]
   }
 
@@ -276,11 +301,9 @@ export abstract class AbstractWorker<
           'Cannot set the default task function to a non-existing task function'
         )
       }
-      this.taskFunctions.set(
-        DEFAULT_TASK_NAME,
-        this.taskFunctions.get(name) as TaskFunction<Data, Response>
-      )
-      this.sendTaskFunctionNamesToMainWorker()
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      this.taskFunctions.set(DEFAULT_TASK_NAME, this.taskFunctions.get(name)!)
+      this.sendTaskFunctionsPropertiesToMainWorker()
       return { status: true }
     } catch (error) {
       return { status: false, error: error as Error }
@@ -301,19 +324,27 @@ export abstract class AbstractWorker<
    */
   protected messageListener (message: MessageValue<Data>): void {
     this.checkMessageWorkerId(message)
-    if (message.statistics != null) {
+    const {
+      statistics,
+      checkActive,
+      taskFunctionOperation,
+      taskId,
+      data,
+      kill
+    } = message
+    if (statistics != null) {
       // Statistics message received
-      this.statistics = message.statistics
-    } else if (message.checkActive != null) {
+      this.statistics = statistics
+    } else if (checkActive != null) {
       // Check active message received
-      message.checkActive ? this.startCheckActive() : this.stopCheckActive()
-    } else if (message.taskFunctionOperation != null) {
+      checkActive ? this.startCheckActive() : this.stopCheckActive()
+    } else if (taskFunctionOperation != null) {
       // Task function operation message received
       this.handleTaskFunctionOperationMessage(message)
-    } else if (message.taskId != null && message.data != null) {
+    } else if (taskId != null && data != null) {
       // Task message received
       this.run(message)
-    } else if (message.kill === true) {
+    } else if (kill === true) {
       // Kill message received
       this.handleKillMessage(message)
     }
@@ -322,24 +353,34 @@ export abstract class AbstractWorker<
   protected handleTaskFunctionOperationMessage (
     message: MessageValue<Data>
   ): void {
-    const { taskFunctionOperation, taskFunctionName, taskFunction } = message
-    let response!: TaskFunctionOperationResult
+    const { taskFunctionOperation, taskFunctionProperties, taskFunction } =
+      message
+    if (taskFunctionProperties == null) {
+      throw new Error(
+        'Cannot handle task function operation message without task function properties'
+      )
+    }
+    let response: TaskFunctionOperationResult
     switch (taskFunctionOperation) {
       case 'add':
-        response = this.addTaskFunction(
-          taskFunctionName as string,
+        response = this.addTaskFunction(taskFunctionProperties.name, {
           // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
-          new Function(`return ${taskFunction as string}`)() as TaskFunction<
-          Data,
-          Response
-          >
-        )
+          taskFunction: new Function(
+            `return ${taskFunction}`
+          )() as TaskFunction<Data, Response>,
+          ...(taskFunctionProperties.priority != null && {
+            priority: taskFunctionProperties.priority
+          }),
+          ...(taskFunctionProperties.strategy != null && {
+            strategy: taskFunctionProperties.strategy
+          })
+        })
         break
       case 'remove':
-        response = this.removeTaskFunction(taskFunctionName as string)
+        response = this.removeTaskFunction(taskFunctionProperties.name)
         break
       case 'default':
-        response = this.setDefaultTaskFunction(taskFunctionName as string)
+        response = this.setDefaultTaskFunction(taskFunctionProperties.name)
         break
       default:
         response = { status: false, error: new Error('Unknown task operation') }
@@ -348,11 +389,11 @@ export abstract class AbstractWorker<
     this.sendToMainWorker({
       taskFunctionOperation,
       taskFunctionOperationStatus: response.status,
-      taskFunctionName,
+      taskFunctionProperties,
       ...(!response.status &&
-        response?.error != null && {
+        response.error != null && {
         workerError: {
-          name: taskFunctionName as string,
+          name: taskFunctionProperties.name,
           message: this.handleError(response.error as Error | string)
         }
       })
@@ -367,7 +408,7 @@ export abstract class AbstractWorker<
   protected handleKillMessage (_message: MessageValue<Data>): void {
     this.stopCheckActive()
     if (isAsyncFunction(this.opts.killHandler)) {
-      (this.opts.killHandler?.() as Promise<void>)
+      (this.opts.killHandler() as Promise<void>)
         .then(() => {
           this.sendToMainWorker({ kill: 'success' })
           return undefined
@@ -458,11 +499,11 @@ export abstract class AbstractWorker<
   ): void
 
   /**
-   * Sends task function names to the main worker.
+   * Sends task functions properties to the main worker.
    */
-  protected sendTaskFunctionNamesToMainWorker (): void {
+  protected sendTaskFunctionsPropertiesToMainWorker (): void {
     this.sendToMainWorker({
-      taskFunctionNames: this.listTaskFunctionNames()
+      taskFunctionsProperties: this.listTaskFunctionsProperties()
     })
   }
 
@@ -481,21 +522,22 @@ export abstract class AbstractWorker<
    *
    * @param task - The task to execute.
    */
-  protected run (task: Task<Data>): void {
+  protected readonly run = (task: Task<Data>): void => {
     const { name, taskId, data } = task
     const taskFunctionName = name ?? DEFAULT_TASK_NAME
     if (!this.taskFunctions.has(taskFunctionName)) {
       this.sendToMainWorker({
         workerError: {
-          name: name as string,
-          message: `Task function '${name as string}' not found`,
+          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+          name: name!,
+          message: `Task function '${name}' not found`,
           data
         },
         taskId
       })
       return
     }
-    const fn = this.taskFunctions.get(taskFunctionName)
+    const fn = this.taskFunctions.get(taskFunctionName)?.taskFunction
     if (isAsyncFunction(fn)) {
       this.runAsync(fn as TaskAsyncFunction<Data, Response>, task)
     } else {
@@ -509,10 +551,10 @@ export abstract class AbstractWorker<
    * @param fn - Task function that will be executed.
    * @param task - Input data for the task function.
    */
-  protected runSync (
+  protected readonly runSync = (
     fn: TaskSyncFunction<Data, Response>,
     task: Task<Data>
-  ): void {
+  ): void => {
     const { name, taskId, data } = task
     try {
       let taskPerformance = this.beginTaskPerformance(name)
@@ -526,7 +568,8 @@ export abstract class AbstractWorker<
     } catch (error) {
       this.sendToMainWorker({
         workerError: {
-          name: name as string,
+          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+          name: name!,
           message: this.handleError(error as Error | string),
           data
         },
@@ -543,10 +586,10 @@ export abstract class AbstractWorker<
    * @param fn - Task function that will be executed.
    * @param task - Input data for the task function.
    */
-  protected runAsync (
+  protected readonly runAsync = (
     fn: TaskAsyncFunction<Data, Response>,
     task: Task<Data>
-  ): void {
+  ): void => {
     const { name, taskId, data } = task
     let taskPerformance = this.beginTaskPerformance(name)
     fn(data)
@@ -559,10 +602,11 @@ export abstract class AbstractWorker<
         })
         return undefined
       })
-      .catch(error => {
+      .catch((error: unknown) => {
         this.sendToMainWorker({
           workerError: {
-            name: name as string,
+            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+            name: name!,
             message: this.handleError(error as Error | string),
             data
           },
@@ -576,18 +620,24 @@ export abstract class AbstractWorker<
   }
 
   private beginTaskPerformance (name?: string): TaskPerformance {
-    this.checkStatistics()
+    if (this.statistics == null) {
+      throw new Error('Performance statistics computation requirements not set')
+    }
     return {
       name: name ?? DEFAULT_TASK_NAME,
       timestamp: performance.now(),
-      ...(this.statistics.elu && { elu: performance.eventLoopUtilization() })
+      ...(this.statistics.elu && {
+        elu: performance.eventLoopUtilization()
+      })
     }
   }
 
   private endTaskPerformance (
     taskPerformance: TaskPerformance
   ): TaskPerformance {
-    this.checkStatistics()
+    if (this.statistics == null) {
+      throw new Error('Performance statistics computation requirements not set')
+    }
     return {
       ...taskPerformance,
       ...(this.statistics.runTime && {
@@ -599,12 +649,6 @@ export abstract class AbstractWorker<
     }
   }
 
-  private checkStatistics (): void {
-    if (this.statistics == null) {
-      throw new Error('Performance statistics computation requirements not set')
-    }
-  }
-
   private updateLastTaskTimestamp (): void {
     if (this.activeInterval != null) {
       this.lastTaskTimestamp = performance.now()