build: bump volta pnpm version
[poolifier.git] / src / pools / cluster / fixed.ts
index 5f067bd7fb4a716f20297f2c907c7632a9607a1b..4a8cb4f1eeeafeb59adf1e30c5d678328cb19b70 100644 (file)
@@ -1,26 +1,14 @@
-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.
@@ -44,9 +32,10 @@ export class FixedClusterPool<
   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,35 +48,15 @@ export class FixedClusterPool<
     return cluster.isPrimary
   }
 
-  /** @inheritDoc */
-  protected async destroyWorkerNode (workerNodeKey: number): Promise<void> {
-    this.flagWorkerNodeAsNotReady(workerNodeKey)
-    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.once('exit', () => {
-        resolve()
-      })
-    })
-    worker.once('disconnect', () => {
-      worker.kill()
-    })
-    await this.sendKillMessageToWorker(workerNodeKey)
-    worker.disconnect()
-    await waitWorkerExit
-  }
-
   /** @inheritDoc */
   protected sendToWorker (
     workerNodeKey: number,
     message: MessageValue<Data>
   ): void {
-    this.workerNodes[workerNodeKey].worker.send({
+    this.workerNodes[workerNodeKey]?.worker.send({
       ...message,
-      workerId: this.getWorkerInfo(workerNodeKey).id as number
-    })
+      workerId: this.getWorkerInfo(workerNodeKey)?.id
+    } satisfies MessageValue<Data>)
   }
 
   /** @inheritDoc */
@@ -122,8 +91,13 @@ export class FixedClusterPool<
   }
 
   /** @inheritDoc */
-  protected createWorker (): Worker {
-    return cluster.fork(this.opts.env)
+  protected shallCreateDynamicWorker (): boolean {
+    return false
+  }
+
+  /** @inheritDoc */
+  protected checkAndEmitDynamicWorkerCreationEvents (): void {
+    /* noop */
   }
 
   /** @inheritDoc */