Commit | Line | Data |
---|---|---|
325f50bc | 1 | import { fork, isMaster, setupMaster, Worker } from 'cluster' |
d3c8a1a8 | 2 | import type { JSONValue, 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 | */ |
d3c8a1a8 S |
24 | export class FixedClusterPool< |
25 | Data extends JSONValue = JSONValue, | |
26 | Response extends JSONValue = JSONValue | |
27 | > extends AbstractPool<Worker, Data, Response> { | |
4ade5f1f | 28 | /** |
325f50bc S |
29 | * @param numWorkers Number of workers for this pool. |
30 | * @param filePath A file path with implementation of `ClusterWorker` class, relative path is fine. | |
4ade5f1f S |
31 | * @param opts An object with possible options for example `errorHandler`, `onlineHandler`. Default: `{ maxTasks: 1000 }` |
32 | */ | |
33 | public constructor ( | |
c97c7edb S |
34 | numWorkers: number, |
35 | filePath: string, | |
36 | public readonly opts: ClusterPoolOptions = { maxTasks: 1000 } | |
4ade5f1f | 37 | ) { |
c97c7edb S |
38 | super(numWorkers, filePath, opts) |
39 | } | |
4ade5f1f | 40 | |
c97c7edb | 41 | protected setupHook (): void { |
325f50bc S |
42 | setupMaster({ |
43 | exec: this.filePath | |
44 | }) | |
c97c7edb | 45 | } |
325f50bc | 46 | |
c97c7edb S |
47 | protected isMain (): boolean { |
48 | return isMaster | |
4ade5f1f S |
49 | } |
50 | ||
c97c7edb S |
51 | protected destroyWorker (worker: Worker): void { |
52 | worker.kill() | |
4ade5f1f S |
53 | } |
54 | ||
c97c7edb S |
55 | protected sendToWorker (worker: Worker, message: MessageValue<Data>): void { |
56 | worker.send(message) | |
4ade5f1f S |
57 | } |
58 | ||
c97c7edb S |
59 | protected registerWorkerMessageListener ( |
60 | port: Worker, | |
61 | listener: (message: MessageValue<Response>) => void | |
62 | ): void { | |
63 | port.on('message', listener) | |
64 | } | |
65 | ||
66 | protected unregisterWorkerMessageListener ( | |
67 | port: Worker, | |
68 | listener: (message: MessageValue<Response>) => void | |
69 | ): void { | |
70 | port.removeListener('message', listener) | |
4ade5f1f S |
71 | } |
72 | ||
c97c7edb S |
73 | protected newWorker (): Worker { |
74 | return fork(this.opts.env) | |
4ade5f1f S |
75 | } |
76 | ||
c97c7edb | 77 | protected afterNewWorkerPushed (worker: Worker): void { |
4ade5f1f S |
78 | // we will attach a listener for every task, |
79 | // when task is completed the listener will be removed but to avoid warnings we are increasing the max listeners size | |
325f50bc | 80 | worker.setMaxListeners(this.opts.maxTasks ?? 1000) |
4ade5f1f S |
81 | } |
82 | } |