Add eslint-plugin-spellcheck (#139)
[poolifier.git] / src / pools / cluster / fixed.ts
CommitLineData
325f50bc 1import { fork, isMaster, setupMaster, Worker } from 'cluster'
d3c8a1a8 2import type { JSONValue, MessageValue } from '../../utility-types'
c97c7edb
S
3import type { PoolOptions } from '../abstract-pool'
4import { AbstractPool } from '../abstract-pool'
4ade5f1f 5
729c563d
S
6/**
7 * Options for a poolifier cluster pool.
8 */
c97c7edb 9export interface ClusterPoolOptions extends PoolOptions<Worker> {
325f50bc
S
10 /**
11 * Key/value pairs to add to worker process environment.
12 *
13 * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
14 */
15 // eslint-disable-next-line @typescript-eslint/no-explicit-any
16 env?: any
4ade5f1f
S
17}
18
19/**
729c563d
S
20 * A cluster pool with a fixed number of workers.
21 *
22 * It is possible to perform tasks in sync or asynchronous mode as you prefer.
4ade5f1f 23 *
729c563d
S
24 * This pool selects the workers in a round robin fashion.
25 *
26 * @template Data Type of data sent to the worker.
27 * @template Response Type of response of execution.
4ade5f1f 28 *
325f50bc
S
29 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
30 * @since 2.0.0
4ade5f1f 31 */
d3c8a1a8
S
32export class FixedClusterPool<
33 Data extends JSONValue = JSONValue,
34 Response extends JSONValue = JSONValue
35> extends AbstractPool<Worker, Data, Response> {
4ade5f1f 36 /**
729c563d
S
37 * Constructs a new poolifier fixed cluster pool.
38 *
5c5a1fb7 39 * @param numberOfWorkers Number of workers for this pool.
729c563d
S
40 * @param filePath Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
41 * @param opts Options for this fixed cluster pool. Default: `{ maxTasks: 1000 }`
4ade5f1f
S
42 */
43 public constructor (
5c5a1fb7 44 numberOfWorkers: number,
c97c7edb
S
45 filePath: string,
46 public readonly opts: ClusterPoolOptions = { maxTasks: 1000 }
4ade5f1f 47 ) {
5c5a1fb7 48 super(numberOfWorkers, filePath, opts)
c97c7edb 49 }
4ade5f1f 50
c97c7edb 51 protected setupHook (): void {
325f50bc
S
52 setupMaster({
53 exec: this.filePath
54 })
c97c7edb 55 }
325f50bc 56
c97c7edb
S
57 protected isMain (): boolean {
58 return isMaster
4ade5f1f
S
59 }
60
c97c7edb
S
61 protected destroyWorker (worker: Worker): void {
62 worker.kill()
f2fdaa86 63 // FIXME: The tests are currently failing, so these must be changed first
4ade5f1f
S
64 }
65
c97c7edb
S
66 protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
67 worker.send(message)
4ade5f1f
S
68 }
69
c97c7edb
S
70 protected registerWorkerMessageListener (
71 port: Worker,
72 listener: (message: MessageValue<Response>) => void
73 ): void {
74 port.on('message', listener)
75 }
76
77 protected unregisterWorkerMessageListener (
78 port: Worker,
79 listener: (message: MessageValue<Response>) => void
80 ): void {
81 port.removeListener('message', listener)
4ade5f1f
S
82 }
83
c97c7edb
S
84 protected newWorker (): Worker {
85 return fork(this.opts.env)
4ade5f1f
S
86 }
87
c97c7edb 88 protected afterNewWorkerPushed (worker: Worker): void {
4ade5f1f
S
89 // we will attach a listener for every task,
90 // when task is completed the listener will be removed but to avoid warnings we are increasing the max listeners size
325f50bc 91 worker.setMaxListeners(this.opts.maxTasks ?? 1000)
4ade5f1f
S
92 }
93}