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