refactor: use pool side task functions list for hasTaskFunction()
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 13 Sep 2023 17:48:38 +0000 (19:48 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 13 Sep 2023 17:48:38 +0000 (19:48 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts
src/utility-types.ts
src/worker/abstract-worker.ts

index 90cede3a8227f7e21cd7a0f32ec84e4f8e6bdbf6..1b53065c54e18318f1295bd68e6340dc5cbc76c1 100644 (file)
@@ -737,11 +737,15 @@ export abstract class AbstractPool<
 
   /** @inheritDoc */
   public hasTaskFunction (name: string): boolean {
-    this.sendToWorkers({
-      taskFunctionOperation: 'has',
-      taskFunctionName: name
-    })
-    return true
+    for (const workerNode of this.workerNodes) {
+      if (
+        Array.isArray(workerNode.info.taskFunctionNames) &&
+        workerNode.info.taskFunctionNames.includes(name)
+      ) {
+        return true
+      }
+    }
+    return false
   }
 
   /** @inheritDoc */
index b2470bea17df1e283ee492a4346f46bf6944a13f..3d5673f93111d552a4cdce0364d7c036cf3e175e 100644 (file)
@@ -118,12 +118,14 @@ export interface MessageValue<Data = unknown, ErrorData = unknown>
   readonly taskPerformance?: TaskPerformance
   /**
    * Task function operation:
-   * - `'has'` - Check if a task function exists.
    * - `'add'` - Add a task function.
    * - `'delete'` - Delete a task function.
    * - `'default'` - Set a task function as default.
    */
-  readonly taskFunctionOperation?: 'has' | 'add' | 'remove' | 'default'
+  readonly taskFunctionOperation?: 'add' | 'remove' | 'default'
+  /**
+   * Whether the task function operation is successful or not.
+   */
   readonly taskFunctionOperationStatus?: boolean
   /**
    * Task function serialized to string.
index 1b64dc4f4cfff7c769ade8ef2bbd100efa1885ab..8cd370de2e3b7671c2f697c106acff3dc12e267f 100644 (file)
@@ -355,9 +355,7 @@ export abstract class AbstractWorker<
   ): void {
     const { taskFunctionOperation, taskFunction, taskFunctionName } = message
     let response!: TaskFunctionOperationReturnType
-    if (taskFunctionOperation === 'has') {
-      response = this.hasTaskFunction(taskFunctionName as string)
-    } else if (taskFunctionOperation === 'add') {
+    if (taskFunctionOperation === 'add') {
       response = this.addTaskFunction(
         taskFunctionName as string,
         // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func