fix: register message handler on worker only
[poolifier.git] / src / worker / abstract-worker.ts
CommitLineData
fc3e6586 1import { AsyncResource } from 'node:async_hooks'
6677a3d3 2import type { Worker } from 'node:cluster'
fc3e6586 3import type { MessagePort } from 'node:worker_threads'
d715b7bc
JB
4import { performance } from 'node:perf_hooks'
5import type {
6 MessageValue,
7 TaskPerformance,
8 WorkerStatistics
9} from '../utility-types'
0d80593b 10import { EMPTY_FUNCTION, isPlainObject } from '../utils'
241f23c2
JB
11import {
12 type KillBehavior,
13 KillBehaviors,
14 type WorkerOptions
15} from './worker-options'
b6b32453
JB
16import type {
17 TaskFunctions,
18 WorkerAsyncFunction,
19 WorkerFunction,
20 WorkerSyncFunction
21} from './worker-functions'
4c35177b 22
ec8fd331 23const DEFAULT_FUNCTION_NAME = 'default'
978aad6f 24const DEFAULT_MAX_INACTIVE_TIME = 60000
1a81f8af 25const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT
c97c7edb 26
729c563d 27/**
ea7a90d3 28 * Base class that implements some shared logic for all poolifier workers.
729c563d 29 *
38e795c1 30 * @typeParam MainWorker - Type of main worker.
e102732c
JB
31 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
32 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
729c563d 33 */
c97c7edb 34export abstract class AbstractWorker<
6677a3d3 35 MainWorker extends Worker | MessagePort,
d3c8a1a8
S
36 Data = unknown,
37 Response = unknown
c97c7edb 38> extends AsyncResource {
a86b6df1
JB
39 /**
40 * Task function(s) processed by the worker when the pool's `execution` function is invoked.
41 */
42 protected taskFunctions!: Map<string, WorkerFunction<Data, Response>>
729c563d
S
43 /**
44 * Timestamp of the last task processed by this worker.
45 */
a9d9ea34 46 protected lastTaskTimestamp!: number
b6b32453 47 /**
8a970421 48 * Performance statistics computation requirements.
b6b32453
JB
49 */
50 protected statistics!: WorkerStatistics
729c563d 51 /**
aee46736 52 * Handler id of the `aliveInterval` worker alive check.
729c563d 53 */
e088a00c 54 protected readonly aliveInterval?: NodeJS.Timeout
c97c7edb 55 /**
729c563d 56 * Constructs a new poolifier worker.
c97c7edb 57 *
38e795c1
JB
58 * @param type - The type of async event.
59 * @param isMain - Whether this is the main worker or not.
82888165 60 * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function.
38e795c1
JB
61 * @param mainWorker - Reference to main worker.
62 * @param opts - Options for the worker.
c97c7edb
S
63 */
64 public constructor (
65 type: string,
c2ade475 66 protected readonly isMain: boolean,
a86b6df1
JB
67 taskFunctions:
68 | WorkerFunction<Data, Response>
69 | TaskFunctions<Data, Response>,
448ad581 70 protected readonly mainWorker: MainWorker,
d99ba5a8 71 protected readonly opts: WorkerOptions = {
e088a00c 72 /**
aee46736 73 * The kill behavior option on this worker or its default value.
e088a00c 74 */
1a81f8af 75 killBehavior: DEFAULT_KILL_BEHAVIOR,
e088a00c
JB
76 /**
77 * The maximum time to keep this worker alive while idle.
78 * The pool automatically checks and terminates this worker when the time expires.
79 */
1a81f8af 80 maxInactiveTime: DEFAULT_MAX_INACTIVE_TIME
4c35177b 81 }
c97c7edb
S
82 ) {
83 super(type)
e088a00c 84 this.checkWorkerOptions(this.opts)
a86b6df1 85 this.checkTaskFunctions(taskFunctions)
1f68cede 86 if (!this.isMain) {
3fafb1b2 87 this.lastTaskTimestamp = performance.now()
e088a00c 88 this.aliveInterval = setInterval(
c97c7edb 89 this.checkAlive.bind(this),
e088a00c 90 (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2
c97c7edb
S
91 )
92 this.checkAlive.bind(this)()
3749facb 93 this.mainWorker?.on('message', this.messageListener.bind(this))
c97c7edb
S
94 }
95 }
96
41aa7dcd
JB
97 private checkWorkerOptions (opts: WorkerOptions): void {
98 this.opts.killBehavior = opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR
99 this.opts.maxInactiveTime =
100 opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME
571227f4 101 delete this.opts.async
41aa7dcd
JB
102 }
103
104 /**
a86b6df1 105 * Checks if the `taskFunctions` parameter is passed to the constructor.
41aa7dcd 106 *
82888165 107 * @param taskFunctions - The task function(s) parameter that should be checked.
41aa7dcd 108 */
a86b6df1
JB
109 private checkTaskFunctions (
110 taskFunctions:
111 | WorkerFunction<Data, Response>
112 | TaskFunctions<Data, Response>
113 ): void {
ec8fd331
JB
114 if (taskFunctions == null) {
115 throw new Error('taskFunctions parameter is mandatory')
116 }
a86b6df1 117 this.taskFunctions = new Map<string, WorkerFunction<Data, Response>>()
0d80593b
JB
118 if (typeof taskFunctions === 'function') {
119 this.taskFunctions.set(DEFAULT_FUNCTION_NAME, taskFunctions.bind(this))
120 } else if (isPlainObject(taskFunctions)) {
82888165 121 let firstEntry = true
a86b6df1
JB
122 for (const [name, fn] of Object.entries(taskFunctions)) {
123 if (typeof fn !== 'function') {
0d80593b 124 throw new TypeError(
a86b6df1
JB
125 'A taskFunctions parameter object value is not a function'
126 )
127 }
128 this.taskFunctions.set(name, fn.bind(this))
82888165
JB
129 if (firstEntry) {
130 this.taskFunctions.set(DEFAULT_FUNCTION_NAME, fn.bind(this))
131 firstEntry = false
132 }
a86b6df1 133 }
630f0acf
JB
134 if (firstEntry) {
135 throw new Error('taskFunctions parameter object is empty')
136 }
a86b6df1 137 } else {
f34fdabe
JB
138 throw new TypeError(
139 'taskFunctions parameter is not a function or a plain object'
140 )
41aa7dcd
JB
141 }
142 }
143
aee46736
JB
144 /**
145 * Worker message listener.
146 *
147 * @param message - Message received.
aee46736 148 */
6677a3d3 149 protected messageListener (message: MessageValue<Data, Data>): void {
a3f5f781 150 if (message.id != null && message.data != null) {
aee46736 151 // Task message received
c7c04698 152 const fn = this.getTaskFunction(message.name)
a86b6df1 153 if (fn?.constructor.name === 'AsyncFunction') {
aee46736 154 this.runInAsyncScope(this.runAsync.bind(this), this, fn, message)
cf597bc5 155 } else {
70a4f5ea 156 this.runInAsyncScope(this.runSync.bind(this), this, fn, message)
cf597bc5 157 }
b9907d18
JB
158 } else if (message.statistics != null) {
159 // Statistics message received
160 this.statistics = message.statistics
aee46736
JB
161 } else if (message.kill != null) {
162 // Kill message received
73cff87e 163 this.aliveInterval != null && clearInterval(this.aliveInterval)
cf597bc5
JB
164 this.emitDestroy()
165 }
166 }
167
729c563d
S
168 /**
169 * Returns the main worker.
838898f1
S
170 *
171 * @returns Reference to the main worker.
729c563d 172 */
838898f1 173 protected getMainWorker (): MainWorker {
78cea37e 174 if (this.mainWorker == null) {
e102732c 175 throw new Error('Main worker not set')
838898f1
S
176 }
177 return this.mainWorker
178 }
c97c7edb 179
729c563d 180 /**
8accb8d5 181 * Sends a message to the main worker.
729c563d 182 *
38e795c1 183 * @param message - The response message.
729c563d 184 */
82f36766
JB
185 protected abstract sendToMainWorker (
186 message: MessageValue<Response, Data>
187 ): void
c97c7edb 188
729c563d 189 /**
a05c10de 190 * Checks if the worker should be terminated, because its living too long.
729c563d 191 */
c97c7edb 192 protected checkAlive (): void {
e088a00c 193 if (
3fafb1b2 194 performance.now() - this.lastTaskTimestamp >
e088a00c
JB
195 (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME)
196 ) {
197 this.sendToMainWorker({ kill: this.opts.killBehavior })
c97c7edb
S
198 }
199 }
200
729c563d 201 /**
8accb8d5 202 * Handles an error and convert it to a string so it can be sent back to the main worker.
729c563d 203 *
38e795c1 204 * @param e - The error raised by the worker.
50eceb07 205 * @returns Message of the error.
729c563d 206 */
c97c7edb 207 protected handleError (e: Error | string): string {
4a34343d 208 return e as string
c97c7edb
S
209 }
210
729c563d 211 /**
8accb8d5 212 * Runs the given function synchronously.
729c563d 213 *
38e795c1 214 * @param fn - Function that will be executed.
aee46736 215 * @param message - Input data for the given function.
729c563d 216 */
70a4f5ea 217 protected runSync (
48ef9107 218 fn: WorkerSyncFunction<Data, Response>,
aee46736 219 message: MessageValue<Data>
c97c7edb
S
220 ): void {
221 try {
1c6fe997 222 let taskPerformance = this.beginTaskPerformance()
aee46736 223 const res = fn(message.data)
d715b7bc 224 taskPerformance = this.endTaskPerformance(taskPerformance)
3fafb1b2
JB
225 this.sendToMainWorker({
226 data: res,
d715b7bc 227 taskPerformance,
91ee39ed 228 id: message.id
3fafb1b2 229 })
c97c7edb 230 } catch (e) {
0a23f635 231 const err = this.handleError(e as Error)
91ee39ed 232 this.sendToMainWorker({
82f36766
JB
233 taskError: {
234 message: err,
235 data: message.data
236 },
91ee39ed
JB
237 id: message.id
238 })
6e9d10db 239 } finally {
3fafb1b2 240 !this.isMain && (this.lastTaskTimestamp = performance.now())
c97c7edb
S
241 }
242 }
243
729c563d 244 /**
8accb8d5 245 * Runs the given function asynchronously.
729c563d 246 *
38e795c1 247 * @param fn - Function that will be executed.
aee46736 248 * @param message - Input data for the given function.
729c563d 249 */
c97c7edb 250 protected runAsync (
48ef9107 251 fn: WorkerAsyncFunction<Data, Response>,
aee46736 252 message: MessageValue<Data>
c97c7edb 253 ): void {
1c6fe997 254 let taskPerformance = this.beginTaskPerformance()
aee46736 255 fn(message.data)
c97c7edb 256 .then(res => {
d715b7bc 257 taskPerformance = this.endTaskPerformance(taskPerformance)
3fafb1b2
JB
258 this.sendToMainWorker({
259 data: res,
d715b7bc 260 taskPerformance,
91ee39ed 261 id: message.id
3fafb1b2 262 })
c97c7edb
S
263 return null
264 })
265 .catch(e => {
f3636726 266 const err = this.handleError(e as Error)
91ee39ed 267 this.sendToMainWorker({
82f36766
JB
268 taskError: {
269 message: err,
270 data: message.data
271 },
91ee39ed
JB
272 id: message.id
273 })
6e9d10db
JB
274 })
275 .finally(() => {
3fafb1b2 276 !this.isMain && (this.lastTaskTimestamp = performance.now())
c97c7edb 277 })
6e9d10db 278 .catch(EMPTY_FUNCTION)
c97c7edb 279 }
ec8fd331 280
82888165
JB
281 /**
282 * Gets the task function in the given scope.
283 *
284 * @param name - Name of the function that will be returned.
285 */
ec8fd331
JB
286 private getTaskFunction (name?: string): WorkerFunction<Data, Response> {
287 name = name ?? DEFAULT_FUNCTION_NAME
288 const fn = this.taskFunctions.get(name)
289 if (fn == null) {
ace229a1 290 throw new Error(`Task function '${name}' not found`)
ec8fd331
JB
291 }
292 return fn
293 }
62c15a68 294
1c6fe997 295 private beginTaskPerformance (): TaskPerformance {
8a970421 296 this.checkStatistics()
62c15a68 297 return {
1c6fe997 298 timestamp: performance.now(),
b6b32453 299 ...(this.statistics.elu && { elu: performance.eventLoopUtilization() })
62c15a68
JB
300 }
301 }
302
d9d31201
JB
303 private endTaskPerformance (
304 taskPerformance: TaskPerformance
305 ): TaskPerformance {
8a970421 306 this.checkStatistics()
62c15a68
JB
307 return {
308 ...taskPerformance,
b6b32453
JB
309 ...(this.statistics.runTime && {
310 runTime: performance.now() - taskPerformance.timestamp
311 }),
312 ...(this.statistics.elu && {
62c15a68 313 elu: performance.eventLoopUtilization(taskPerformance.elu)
b6b32453 314 })
62c15a68
JB
315 }
316 }
8a970421
JB
317
318 private checkStatistics (): void {
319 if (this.statistics == null) {
320 throw new Error('Performance statistics computation requirements not set')
321 }
322 }
c97c7edb 323}