Commit | Line | Data |
---|---|---|
6677a3d3 | 1 | import cluster, { type Worker } from 'node:cluster' |
b6b32453 | 2 | import type { MessageValue } from '../utility-types' |
c97c7edb | 3 | import { AbstractWorker } from './abstract-worker' |
325f50bc | 4 | import type { WorkerOptions } from './worker-options' |
82ea6492 | 5 | import type { TaskFunction, TaskFunctions } from './task-functions' |
325f50bc S |
6 | |
7 | /** | |
729c563d | 8 | * A cluster worker used by a poolifier `ClusterPool`. |
325f50bc | 9 | * |
729c563d S |
10 | * When this worker is inactive for more than the given `maxInactiveTime`, |
11 | * it will send a termination request to its main worker. | |
12 | * | |
13 | * If you use a `DynamicClusterPool` the extra workers that were created will be terminated, | |
14 | * but the minimum number of workers will be guaranteed. | |
15 | * | |
e102732c JB |
16 | * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data. |
17 | * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data. | |
325f50bc S |
18 | * @author [Christopher Quadflieg](https://github.com/Shinigami92) |
19 | * @since 2.0.0 | |
20 | */ | |
d3c8a1a8 | 21 | export class ClusterWorker< |
deb85c12 JB |
22 | Data = unknown, |
23 | Response = unknown | |
6677a3d3 | 24 | > extends AbstractWorker<Worker, Data, Response> { |
729c563d S |
25 | /** |
26 | * Constructs a new poolifier cluster worker. | |
27 | * | |
a86b6df1 | 28 | * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. |
38e795c1 | 29 | * @param opts - Options for the worker. |
729c563d | 30 | */ |
d4aeae5a | 31 | public constructor ( |
82ea6492 | 32 | taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, |
d4aeae5a JB |
33 | opts: WorkerOptions = {} |
34 | ) { | |
04212af7 | 35 | super(cluster.isPrimary, cluster.worker as Worker, taskFunctions, opts) |
85aeb3f3 JB |
36 | } |
37 | ||
38 | /** @inheritDoc */ | |
39 | protected handleReadyMessage (message: MessageValue<Data>): void { | |
f05ed93c JB |
40 | if (message.workerId === this.id && message.ready === false) { |
41 | try { | |
9d2d0da1 | 42 | this.getMainWorker().on('message', this.messageListener.bind(this)) |
a5d15204 JB |
43 | this.sendToMainWorker({ |
44 | ready: true, | |
895b5341 | 45 | taskFunctionNames: this.listTaskFunctionNames() |
a5d15204 | 46 | }) |
f05ed93c | 47 | } catch { |
a5d15204 JB |
48 | this.sendToMainWorker({ |
49 | ready: false, | |
895b5341 | 50 | taskFunctionNames: this.listTaskFunctionNames() |
a5d15204 | 51 | }) |
f05ed93c | 52 | } |
85aeb3f3 | 53 | } |
f59e1027 JB |
54 | } |
55 | ||
56 | /** @inheritDoc */ | |
57 | protected get id (): number { | |
58 | return this.getMainWorker().id | |
325f50bc S |
59 | } |
60 | ||
afc003b2 | 61 | /** @inheritDoc */ |
c97c7edb | 62 | protected sendToMainWorker (message: MessageValue<Response>): void { |
895b5341 | 63 | this.getMainWorker().send({ ...message, workerId: this.id }) |
325f50bc | 64 | } |
325f50bc | 65 | } |