Commit | Line | Data |
---|---|---|
325f50bc S |
1 | import { fork, isMaster, setupMaster, Worker } from 'cluster' |
2 | import type { MessageValue } from '../../utility-types' | |
c97c7edb S |
3 | import type { PoolOptions } from '../abstract-pool' |
4 | import { AbstractPool } from '../abstract-pool' | |
4ade5f1f | 5 | |
c97c7edb | 6 | export interface ClusterPoolOptions extends PoolOptions<Worker> { |
325f50bc S |
7 | /** |
8 | * Key/value pairs to add to worker process environment. | |
9 | * | |
10 | * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env | |
11 | */ | |
12 | // eslint-disable-next-line @typescript-eslint/no-explicit-any | |
13 | env?: any | |
4ade5f1f S |
14 | } |
15 | ||
16 | /** | |
325f50bc | 17 | * A cluster pool with a static number of workers, is possible to execute tasks in sync or async mode as you prefer. |
4ade5f1f | 18 | * |
325f50bc | 19 | * This pool will select the worker in a round robin fashion. |
4ade5f1f | 20 | * |
325f50bc S |
21 | * @author [Christopher Quadflieg](https://github.com/Shinigami92) |
22 | * @since 2.0.0 | |
4ade5f1f | 23 | */ |
777b7824 | 24 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
c97c7edb S |
25 | export class FixedClusterPool<Data = any, Response = any> extends AbstractPool< |
26 | Worker, | |
27 | Data, | |
28 | Response | |
29 | > { | |
4ade5f1f | 30 | /** |
325f50bc S |
31 | * @param numWorkers Number of workers for this pool. |
32 | * @param filePath A file path with implementation of `ClusterWorker` class, relative path is fine. | |
4ade5f1f S |
33 | * @param opts An object with possible options for example `errorHandler`, `onlineHandler`. Default: `{ maxTasks: 1000 }` |
34 | */ | |
35 | public constructor ( | |
c97c7edb S |
36 | numWorkers: number, |
37 | filePath: string, | |
38 | public readonly opts: ClusterPoolOptions = { maxTasks: 1000 } | |
4ade5f1f | 39 | ) { |
c97c7edb S |
40 | super(numWorkers, filePath, opts) |
41 | } | |
4ade5f1f | 42 | |
c97c7edb | 43 | protected setupHook (): void { |
325f50bc S |
44 | setupMaster({ |
45 | exec: this.filePath | |
46 | }) | |
c97c7edb | 47 | } |
325f50bc | 48 | |
c97c7edb S |
49 | protected isMain (): boolean { |
50 | return isMaster | |
4ade5f1f S |
51 | } |
52 | ||
c97c7edb S |
53 | protected destroyWorker (worker: Worker): void { |
54 | worker.kill() | |
4ade5f1f S |
55 | } |
56 | ||
c97c7edb S |
57 | protected sendToWorker (worker: Worker, message: MessageValue<Data>): void { |
58 | worker.send(message) | |
4ade5f1f S |
59 | } |
60 | ||
c97c7edb S |
61 | protected registerWorkerMessageListener ( |
62 | port: Worker, | |
63 | listener: (message: MessageValue<Response>) => void | |
64 | ): void { | |
65 | port.on('message', listener) | |
66 | } | |
67 | ||
68 | protected unregisterWorkerMessageListener ( | |
69 | port: Worker, | |
70 | listener: (message: MessageValue<Response>) => void | |
71 | ): void { | |
72 | port.removeListener('message', listener) | |
4ade5f1f S |
73 | } |
74 | ||
c97c7edb S |
75 | protected newWorker (): Worker { |
76 | return fork(this.opts.env) | |
4ade5f1f S |
77 | } |
78 | ||
c97c7edb | 79 | protected afterNewWorkerPushed (worker: Worker): void { |
4ade5f1f S |
80 | // we will attach a listener for every task, |
81 | // when task is completed the listener will be removed but to avoid warnings we are increasing the max listeners size | |
325f50bc | 82 | worker.setMaxListeners(this.opts.maxTasks ?? 1000) |
4ade5f1f S |
83 | } |
84 | } |