Bump @typescript-eslint/eslint-plugin from 4.18.0 to 4.19.0 (#290)
[poolifier.git] / src / pools / cluster / fixed.ts
index 616523cb2885964ef2fee6e4165aaf5dad979000..e1caf252332f7d0103a77a9470b1526a62613e78 100644 (file)
@@ -2,6 +2,7 @@ import { fork, isMaster, setupMaster, Worker } from 'cluster'
 import type { MessageValue } from '../../utility-types'
 import type { PoolOptions } from '../abstract-pool'
 import { AbstractPool } from '../abstract-pool'
+import { PoolType } from '../pool-internal'
 
 /**
  * Options for a poolifier cluster pool.
@@ -38,56 +39,65 @@ export class FixedClusterPool<
    *
    * @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. Default: `{ maxTasks: 1000 }`
+   * @param opts Options for this fixed cluster pool. Default: `{}`
    */
   public constructor (
     numberOfWorkers: number,
     filePath: string,
-    public readonly opts: ClusterPoolOptions = { maxTasks: 1000 }
+    public readonly opts: ClusterPoolOptions = {}
   ) {
     super(numberOfWorkers, filePath, opts)
   }
 
+  /** @inheritdoc */
   protected setupHook (): void {
     setupMaster({
       exec: this.filePath
     })
   }
 
+  /** @inheritdoc */
   protected isMain (): boolean {
     return isMaster
   }
 
-  protected destroyWorker (worker: Worker): void {
+  /** @inheritdoc */
+  public destroyWorker (worker: Worker): void {
     this.sendToWorker(worker, { kill: 1 })
     worker.kill()
   }
 
+  /** @inheritdoc */
   protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
     worker.send(message)
   }
 
-  protected registerWorkerMessageListener<Message extends Data | Response> (
+  /** @inheritdoc */
+  public registerWorkerMessageListener<Message extends Data | Response> (
     worker: Worker,
     listener: (message: MessageValue<Message>) => void
   ): void {
     worker.on('message', listener)
   }
 
-  protected unregisterWorkerMessageListener<Message extends Data | Response> (
-    worker: Worker,
-    listener: (message: MessageValue<Message>) => void
-  ): void {
-    worker.removeListener('message', listener)
-  }
-
+  /** @inheritdoc */
   protected createWorker (): Worker {
     return fork(this.opts.env)
   }
 
+  /** @inheritdoc */
   protected afterWorkerSetup (worker: Worker): void {
-    // we will attach a listener for every task,
-    // when task is completed the listener will be removed but to avoid warnings we are increasing the max listeners size
-    worker.setMaxListeners(this.opts.maxTasks ?? 1000)
+    // Listen worker messages.
+    this.registerWorkerMessageListener(worker, super.workerListener())
+  }
+
+  /** @inheritdoc */
+  public get type (): PoolType {
+    return PoolType.FIXED
+  }
+
+  /** @inheritdoc */
+  public get busy (): boolean {
+    return this.internalGetBusyStatus()
   }
 }