Encapsulate logic of cluster and thread worker/pool (#116)
[poolifier.git] / src / pools / thread / fixed.ts
index 36eddace5ec149c41b8daafbd4ada0b91babb32d..bed8f3a7dba8ae87ed6085df040723a93eb0e739 100644 (file)
@@ -1,28 +1,9 @@
 import { isMainThread, MessageChannel, SHARE_ENV, Worker } from 'worker_threads'
 import type { Draft, MessageValue } from '../../utility-types'
+import type { PoolOptions } from '../abstract-pool'
+import { AbstractPool } from '../abstract-pool'
 
-export type WorkerWithMessageChannel = Worker & Draft<MessageChannel>
-
-export interface FixedThreadPoolOptions {
-  /**
-   * A function that will listen for error event on each worker thread.
-   */
-  errorHandler?: (this: Worker, e: Error) => void
-  /**
-   * A function that will listen for online event on each worker thread.
-   */
-  onlineHandler?: (this: Worker) => void
-  /**
-   * A function that will listen for exit event on each worker thread.
-   */
-  exitHandler?: (this: Worker, code: number) => void
-  /**
-   * This is just to avoid not useful warnings message, is used to set `maxListeners` on event emitters (workers are event emitters).
-   *
-   * @default 1000
-   */
-  maxTasks?: number
-}
+export type ThreadWorkerWithMessageChannel = Worker & Draft<MessageChannel>
 
 /**
  * A thread pool with a static number of threads, is possible to execute tasks in sync or async mode as you prefer.
@@ -33,109 +14,64 @@ export interface FixedThreadPoolOptions {
  * @since 0.0.1
  */
 // eslint-disable-next-line @typescript-eslint/no-explicit-any
-export class FixedThreadPool<Data = any, Response = any> {
-  public readonly workers: WorkerWithMessageChannel[] = []
-  public nextWorker: number = 0
-
-  // threadId as key and an integer value
-  public readonly tasks: Map<WorkerWithMessageChannel, number> = new Map<
-    WorkerWithMessageChannel,
-    number
-  >()
-
-  protected id: number = 0
-
+export class FixedThreadPool<Data = any, Response = any> extends AbstractPool<
+  ThreadWorkerWithMessageChannel,
+  Data,
+  Response
+> {
   /**
    * @param numThreads Num of threads for this worker pool.
    * @param filePath A file path with implementation of `ThreadWorker` class, relative path is fine.
    * @param opts An object with possible options for example `errorHandler`, `onlineHandler`. Default: `{ maxTasks: 1000 }`
    */
   public constructor (
-    public readonly numThreads: number,
-    public readonly filePath: string,
-    public readonly opts: FixedThreadPoolOptions = { maxTasks: 1000 }
+    numThreads: number,
+    filePath: string,
+    opts: PoolOptions<ThreadWorkerWithMessageChannel> = { maxTasks: 1000 }
   ) {
-    if (!isMainThread) {
-      throw new Error('Cannot start a thread pool from a worker thread !!!')
-    }
-    // TODO christopher 2021-02-07: Improve this check e.g. with a pattern or blank check
-    if (!this.filePath) {
-      throw new Error('Please specify a file with a worker implementation')
-    }
+    super(numThreads, filePath, opts)
+  }
 
-    for (let i = 1; i <= this.numThreads; i++) {
-      this.newWorker()
-    }
+  protected isMain (): boolean {
+    return isMainThread
   }
 
-  public async destroy (): Promise<void> {
-    for (const worker of this.workers) {
-      await worker.terminate()
-    }
+  protected async destroyWorker (
+    worker: ThreadWorkerWithMessageChannel
+  ): Promise<void> {
+    await worker.terminate()
   }
 
-  /**
-   * Execute the task specified into the constructor with the data parameter.
-   *
-   * @param data The input for the task specified.
-   * @returns Promise that is resolved when the task is done.
-   */
-  public execute (data: Data): Promise<Response> {
-    // configure worker to handle message with the specified task
-    const worker = this.chooseWorker()
-    const previousWorkerIndex = this.tasks.get(worker)
-    if (previousWorkerIndex !== undefined) {
-      this.tasks.set(worker, previousWorkerIndex + 1)
-    } else {
-      throw Error('Worker could not be found in tasks map')
-    }
-    const id = ++this.id
-    const res = this.internalExecute(worker, id)
-    worker.postMessage({ data: data || {}, id: id })
-    return res
+  protected sendToWorker (
+    worker: ThreadWorkerWithMessageChannel,
+    message: MessageValue<Data>
+  ): void {
+    worker.postMessage(message)
   }
 
-  protected internalExecute (
-    worker: WorkerWithMessageChannel,
-    id: number
-  ): Promise<Response> {
-    return new Promise((resolve, reject) => {
-      const listener: (message: MessageValue<Response>) => void = message => {
-        if (message.id === id) {
-          worker.port2?.removeListener('message', listener)
-          const previousWorkerIndex = this.tasks.get(worker)
-          if (previousWorkerIndex !== undefined) {
-            this.tasks.set(worker, previousWorkerIndex + 1)
-          } else {
-            throw Error('Worker could not be found in tasks map')
-          }
-          if (message.error) reject(message.error)
-          else resolve(message.data as Response)
-        }
-      }
-      worker.port2?.on('message', listener)
-    })
+  protected registerWorkerMessageListener (
+    port: ThreadWorkerWithMessageChannel,
+    listener: (message: MessageValue<Response>) => void
+  ): void {
+    port.port2?.on('message', listener)
   }
 
-  protected chooseWorker (): WorkerWithMessageChannel {
-    if (this.workers.length - 1 === this.nextWorker) {
-      this.nextWorker = 0
-      return this.workers[this.nextWorker]
-    } else {
-      this.nextWorker++
-      return this.workers[this.nextWorker]
-    }
+  protected unregisterWorkerMessageListener (
+    port: ThreadWorkerWithMessageChannel,
+    listener: (message: MessageValue<Response>) => void
+  ): void {
+    port.port2?.removeListener('message', listener)
   }
 
-  protected newWorker (): WorkerWithMessageChannel {
-    const worker: WorkerWithMessageChannel = new Worker(this.filePath, {
+  protected newWorker (): ThreadWorkerWithMessageChannel {
+    return new Worker(this.filePath, {
       env: SHARE_ENV
     })
-    worker.on('error', this.opts.errorHandler ?? (() => {}))
-    worker.on('online', this.opts.onlineHandler ?? (() => {}))
-    // TODO handle properly when a thread exit
-    worker.on('exit', this.opts.exitHandler ?? (() => {}))
-    this.workers.push(worker)
+  }
+
+  protected afterNewWorkerPushed (
+    worker: ThreadWorkerWithMessageChannel
+  ): void {
     const { port1, port2 } = new MessageChannel()
     worker.postMessage({ parent: port1 }, [port1])
     worker.port1 = port1
@@ -143,8 +79,5 @@ export class FixedThreadPool<Data = any, Response = any> {
     // 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.port2.setMaxListeners(this.opts.maxTasks ?? 1000)
-    // init tasks map
-    this.tasks.set(worker, 0)
-    return worker
   }
 }