build(ci): refine autofix GH action
[poolifier.git] / src / pools / cluster / fixed.ts
index 17ef7e9e57adcb91967b30be12cef158d1fc37fd..bf61cd477b153af111d835a0371d193d92ceb786 100644 (file)
@@ -1,30 +1,17 @@
-import cluster, { type ClusterSettings, type Worker } from 'node:cluster'
-import type { MessageValue } from '../../utility-types'
-import { AbstractPool } from '../abstract-pool'
-import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
-import { type WorkerType, WorkerTypes } from '../worker'
+import cluster, { type Worker } from 'node:cluster'
+
+import type { MessageValue } from '../../utility-types.js'
+import { AbstractPool } from '../abstract-pool.js'
+import { type PoolOptions, type PoolType, PoolTypes } from '../pool.js'
+import { type WorkerType, WorkerTypes } from '../worker.js'
 
 /**
  * Options for a poolifier cluster pool.
  */
-export interface ClusterPoolOptions extends PoolOptions<Worker> {
-  /**
-   * Key/value pairs to add to worker process environment.
-   *
-   * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
-   */
-  env?: Record<string, unknown>
-  /**
-   * Cluster settings.
-   *
-   * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
-   */
-  settings?: ClusterSettings
-}
+export type ClusterPoolOptions = PoolOptions<Worker>
 
 /**
  * A cluster pool with a fixed number of workers.
- *
  * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
  * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
  * @author [Christopher Quadflieg](https://github.com/Shinigami92)
@@ -36,17 +23,18 @@ export class FixedClusterPool<
 > extends AbstractPool<Worker, Data, Response> {
   /**
    * Constructs a new poolifier fixed cluster pool.
-   *
    * @param numberOfWorkers - Number of workers for this pool.
    * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
    * @param opts - Options for this fixed cluster pool.
+   * @param maximumNumberOfWorkers - The maximum number of workers for this pool.
    */
   public constructor (
     numberOfWorkers: number,
     filePath: string,
-    protected readonly opts: ClusterPoolOptions = {}
+    opts: ClusterPoolOptions = {},
+    maximumNumberOfWorkers?: number
   ) {
-    super(numberOfWorkers, filePath, opts)
+    super(numberOfWorkers, filePath, opts, maximumNumberOfWorkers)
   }
 
   /** @inheritDoc */
@@ -59,41 +47,21 @@ export class FixedClusterPool<
     return cluster.isPrimary
   }
 
-  /** @inheritDoc */
-  protected async destroyWorkerNode (workerNodeKey: number): Promise<void> {
-    this.flushTasksQueue(workerNodeKey)
-    // FIXME: wait for tasks to be finished
-    const workerNode = this.workerNodes[workerNodeKey]
-    const worker = workerNode.worker
-    const waitWorkerExit = new Promise<void>(resolve => {
-      worker.on('exit', () => {
-        resolve()
-      })
-    })
-    worker.on('disconnect', () => {
-      worker.kill()
-    })
-    await this.sendKillMessageToWorker(
-      workerNodeKey,
-      workerNode.info.id as number
-    )
-    worker.disconnect()
-    await waitWorkerExit
-  }
-
   /** @inheritDoc */
   protected sendToWorker (
     workerNodeKey: number,
     message: MessageValue<Data>
   ): void {
-    this.workerNodes[workerNodeKey].worker.send(message)
+    this.workerNodes[workerNodeKey]?.worker.send({
+      ...message,
+      workerId: this.getWorkerInfo(workerNodeKey)?.id,
+    } satisfies MessageValue<Data>)
   }
 
   /** @inheritDoc */
   protected sendStartupMessageToWorker (workerNodeKey: number): void {
     this.sendToWorker(workerNodeKey, {
       ready: false,
-      workerId: this.workerNodes[workerNodeKey].info.id as number
     })
   }
 
@@ -106,8 +74,29 @@ export class FixedClusterPool<
   }
 
   /** @inheritDoc */
-  protected createWorker (): Worker {
-    return cluster.fork(this.opts.env)
+  protected registerOnceWorkerMessageListener<Message extends Data | Response>(
+    workerNodeKey: number,
+    listener: (message: MessageValue<Message>) => void
+  ): void {
+    this.workerNodes[workerNodeKey].worker.once('message', listener)
+  }
+
+  /** @inheritDoc */
+  protected deregisterWorkerMessageListener<Message extends Data | Response>(
+    workerNodeKey: number,
+    listener: (message: MessageValue<Message>) => void
+  ): void {
+    this.workerNodes[workerNodeKey].worker.off('message', listener)
+  }
+
+  /** @inheritDoc */
+  protected shallCreateDynamicWorker (): boolean {
+    return false
+  }
+
+  /** @inheritDoc */
+  protected checkAndEmitDynamicWorkerCreationEvents (): void {
+    /* noop */
   }
 
   /** @inheritDoc */