Merge dependabot/npm_and_yarn/examples/typescript/http-server-pool/fastify-hybrid...
[poolifier.git] / src / pools / cluster / fixed.ts
index f3fb54bbf4a08bd0ee6d28f623f3ac04f55630f1..4a8cb4f1eeeafeb59adf1e30c5d678328cb19b70 100644 (file)
@@ -1,8 +1,14 @@
 import cluster, { 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 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 type ClusterPoolOptions = PoolOptions<Worker>
 
 /**
  * A cluster pool with a fixed number of workers.
@@ -26,9 +32,10 @@ export class FixedClusterPool<
   public constructor (
     numberOfWorkers: number,
     filePath: string,
-    protected readonly opts: PoolOptions<Worker> = {}
+    opts: ClusterPoolOptions = {},
+    maximumNumberOfWorkers?: number
   ) {
-    super(numberOfWorkers, filePath, opts)
+    super(numberOfWorkers, filePath, opts, maximumNumberOfWorkers)
   }
 
   /** @inheritDoc */
@@ -41,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 waitWorkerExit = new Promise<void>(resolve => {
-      workerNode.registerOnceWorkerEventHandler('exit', () => {
-        resolve()
-      })
-    })
-    workerNode.registerOnceWorkerEventHandler('disconnect', () => {
-      workerNode.worker.kill()
-    })
-    await this.sendKillMessageToWorker(workerNodeKey)
-    workerNode.removeAllListeners()
-    workerNode.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 */
@@ -103,6 +90,16 @@ export class FixedClusterPool<
     this.workerNodes[workerNodeKey].worker.off('message', listener)
   }
 
+  /** @inheritDoc */
+  protected shallCreateDynamicWorker (): boolean {
+    return false
+  }
+
+  /** @inheritDoc */
+  protected checkAndEmitDynamicWorkerCreationEvents (): void {
+    /* noop */
+  }
+
   /** @inheritDoc */
   protected get type (): PoolType {
     return PoolTypes.fixed