Commit | Line | Data |
---|---|---|
1a76932b | 1 | import type { ClusterSettings, Worker } from 'cluster' |
1d43bc4c | 2 | import cluster from 'cluster' |
deb85c12 | 3 | import type { MessageValue } from '../../utility-types' |
c97c7edb | 4 | import { AbstractPool } from '../abstract-pool' |
bdaf31cd | 5 | import type { PoolOptions } from '../pool' |
7c0ba920 | 6 | import { PoolType } from '../pool-internal' |
4ade5f1f | 7 | |
729c563d S |
8 | /** |
9 | * Options for a poolifier cluster pool. | |
10 | */ | |
c97c7edb | 11 | export interface ClusterPoolOptions extends PoolOptions<Worker> { |
325f50bc S |
12 | /** |
13 | * Key/value pairs to add to worker process environment. | |
14 | * | |
15 | * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env | |
16 | */ | |
17 | // eslint-disable-next-line @typescript-eslint/no-explicit-any | |
18 | env?: any | |
1a76932b JB |
19 | /** |
20 | * Cluster settings. | |
21 | * | |
22 | * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings | |
23 | */ | |
24 | settings?: ClusterSettings | |
4ade5f1f S |
25 | } |
26 | ||
27 | /** | |
729c563d S |
28 | * A cluster pool with a fixed number of workers. |
29 | * | |
30 | * It is possible to perform tasks in sync or asynchronous mode as you prefer. | |
4ade5f1f | 31 | * |
729c563d S |
32 | * This pool selects the workers in a round robin fashion. |
33 | * | |
91bfd799 JB |
34 | * @template DataType of data sent to the worker. This can only be serializable data. |
35 | * @template ResponseType of response of execution. This can only be serializable data. | |
325f50bc S |
36 | * @author [Christopher Quadflieg](https://github.com/Shinigami92) |
37 | * @since 2.0.0 | |
4ade5f1f | 38 | */ |
d3c8a1a8 | 39 | export class FixedClusterPool< |
deb85c12 JB |
40 | Data = unknown, |
41 | Response = unknown | |
d3c8a1a8 | 42 | > extends AbstractPool<Worker, Data, Response> { |
4ade5f1f | 43 | /** |
729c563d S |
44 | * Constructs a new poolifier fixed cluster pool. |
45 | * | |
5c5a1fb7 | 46 | * @param numberOfWorkers Number of workers for this pool. |
729c563d | 47 | * @param filePath Path to an implementation of a `ClusterWorker` file, which can be relative or absolute. |
ee6c0f81 | 48 | * @param opts Options for this fixed cluster pool. |
4ade5f1f S |
49 | */ |
50 | public constructor ( | |
5c5a1fb7 | 51 | numberOfWorkers: number, |
c97c7edb | 52 | filePath: string, |
1927ee67 | 53 | public readonly opts: ClusterPoolOptions = {} |
4ade5f1f | 54 | ) { |
5c5a1fb7 | 55 | super(numberOfWorkers, filePath, opts) |
c97c7edb | 56 | } |
4ade5f1f | 57 | |
a76fac14 | 58 | /** @inheritDoc */ |
c97c7edb | 59 | protected setupHook (): void { |
1a76932b | 60 | cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath }) |
c97c7edb | 61 | } |
325f50bc | 62 | |
a76fac14 | 63 | /** @inheritDoc */ |
c97c7edb | 64 | protected isMain (): boolean { |
7e0d447f | 65 | return cluster.isPrimary |
4ade5f1f S |
66 | } |
67 | ||
a76fac14 | 68 | /** @inheritDoc */ |
a35560ba | 69 | public destroyWorker (worker: Worker): void { |
cefac5ba | 70 | this.sendToWorker(worker, { kill: 1 }) |
c97c7edb | 71 | worker.kill() |
4ade5f1f S |
72 | } |
73 | ||
a76fac14 | 74 | /** @inheritDoc */ |
c97c7edb S |
75 | protected sendToWorker (worker: Worker, message: MessageValue<Data>): void { |
76 | worker.send(message) | |
4ade5f1f S |
77 | } |
78 | ||
a76fac14 | 79 | /** @inheritDoc */ |
a35560ba | 80 | public registerWorkerMessageListener<Message extends Data | Response> ( |
4f7fa42a S |
81 | worker: Worker, |
82 | listener: (message: MessageValue<Message>) => void | |
c97c7edb | 83 | ): void { |
4f7fa42a | 84 | worker.on('message', listener) |
c97c7edb S |
85 | } |
86 | ||
a76fac14 | 87 | /** @inheritDoc */ |
280c2a77 | 88 | protected createWorker (): Worker { |
7e0d447f | 89 | return cluster.fork(this.opts.env) |
4ade5f1f S |
90 | } |
91 | ||
a76fac14 | 92 | /** @inheritDoc */ |
280c2a77 | 93 | protected afterWorkerSetup (worker: Worker): void { |
a05c10de | 94 | // Listen to worker messages. |
be0676b3 | 95 | this.registerWorkerMessageListener(worker, super.workerListener()) |
4ade5f1f | 96 | } |
7c0ba920 | 97 | |
a76fac14 | 98 | /** @inheritDoc */ |
7c0ba920 JB |
99 | public get type (): PoolType { |
100 | return PoolType.FIXED | |
101 | } | |
102 | ||
a76fac14 | 103 | /** @inheritDoc */ |
7c0ba920 JB |
104 | public get busy (): boolean { |
105 | return this.internalGetBusyStatus() | |
106 | } | |
4ade5f1f | 107 | } |