-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
fixed.ts
127 lines (115 loc) · 3.49 KB
/
fixed.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import cluster, { type ClusterSettings, type Worker } from 'node:cluster'
import type { MessageValue } from '../../utility-types'
import { AbstractPool } from '../abstract-pool'
import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
import { type WorkerType, WorkerTypes } from '../worker'
/**
* Options for a poolifier cluster pool.
*/
export interface ClusterPoolOptions extends PoolOptions<Worker> {
/**
* Key/value pairs to add to worker process environment.
*
* @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
*/
env?: Record<string, unknown>
/**
* Cluster settings.
*
* @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
*/
settings?: ClusterSettings
}
/**
* A cluster pool with a fixed number of workers.
*
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
* @since 2.0.0
*/
export class FixedClusterPool<
Data = unknown,
Response = unknown
> extends AbstractPool<Worker, Data, Response> {
/**
* Constructs a new poolifier fixed cluster pool.
*
* @param numberOfWorkers - Number of workers for this pool.
* @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
* @param opts - Options for this fixed cluster pool.
*/
public constructor (
numberOfWorkers: number,
filePath: string,
protected readonly opts: ClusterPoolOptions = {}
) {
super(numberOfWorkers, filePath, opts)
}
/** @inheritDoc */
protected setupHook (): void {
cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
}
/** @inheritDoc */
protected isMain (): boolean {
return cluster.isPrimary
}
/** @inheritDoc */
protected async destroyWorkerNode (workerNodeKey: number): Promise<void> {
this.flushTasksQueue(workerNodeKey)
// FIXME: wait for tasks to be finished
const workerNode = this.workerNodes[workerNodeKey]
const worker = workerNode.worker
const waitWorkerExit = new Promise<void>((resolve) => {
worker.on('exit', () => {
resolve()
})
})
worker.on('disconnect', () => {
worker.kill()
})
await this.sendKillMessageToWorker(
workerNodeKey,
workerNode.info.id as number
)
worker.disconnect()
await waitWorkerExit
}
/** @inheritDoc */
protected sendToWorker (
workerNodeKey: number,
message: MessageValue<Data>
): void {
this.workerNodes[workerNodeKey].worker.send(message)
}
/** @inheritDoc */
protected sendStartupMessageToWorker (workerNodeKey: number): void {
this.sendToWorker(workerNodeKey, {
ready: false,
workerId: this.workerNodes[workerNodeKey].info.id as number
})
}
/** @inheritDoc */
protected registerWorkerMessageListener<Message extends Data | Response>(
workerNodeKey: number,
listener: (message: MessageValue<Message>) => void
): void {
this.workerNodes[workerNodeKey].worker.on('message', listener)
}
/** @inheritDoc */
protected createWorker (): Worker {
return cluster.fork(this.opts.env)
}
/** @inheritDoc */
protected get type (): PoolType {
return PoolTypes.fixed
}
/** @inheritDoc */
protected get worker (): WorkerType {
return WorkerTypes.cluster
}
/** @inheritDoc */
protected get busy (): boolean {
return this.internalBusy()
}
}