build(deps-dev): apply updates
[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,
5c4d16da 7 Task,
d715b7bc
JB
8 TaskPerformance,
9 WorkerStatistics
10} from '../utility-types'
ff128cc9
JB
11import {
12 DEFAULT_TASK_NAME,
13 EMPTY_FUNCTION,
14 isAsyncFunction,
15 isPlainObject
16} from '../utils'
241f23c2
JB
17import {
18 type KillBehavior,
19 KillBehaviors,
20 type WorkerOptions
21} from './worker-options'
b6b32453
JB
22import type {
23 TaskFunctions,
24 WorkerAsyncFunction,
25 WorkerFunction,
26 WorkerSyncFunction
27} from './worker-functions'
4c35177b 28
978aad6f 29const DEFAULT_MAX_INACTIVE_TIME = 60000
1a81f8af 30const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT
c97c7edb 31
729c563d 32/**
ea7a90d3 33 * Base class that implements some shared logic for all poolifier workers.
729c563d 34 *
38e795c1 35 * @typeParam MainWorker - Type of main worker.
e102732c
JB
36 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
37 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
729c563d 38 */
c97c7edb 39export abstract class AbstractWorker<
6677a3d3 40 MainWorker extends Worker | MessagePort,
d3c8a1a8
S
41 Data = unknown,
42 Response = unknown
c97c7edb 43> extends AsyncResource {
f59e1027 44 /**
83fa0a36 45 * Worker id.
f59e1027
JB
46 */
47 protected abstract id: number
a86b6df1
JB
48 /**
49 * Task function(s) processed by the worker when the pool's `execution` function is invoked.
50 */
51 protected taskFunctions!: Map<string, WorkerFunction<Data, Response>>
729c563d
S
52 /**
53 * Timestamp of the last task processed by this worker.
54 */
a9d9ea34 55 protected lastTaskTimestamp!: number
b6b32453 56 /**
8a970421 57 * Performance statistics computation requirements.
b6b32453
JB
58 */
59 protected statistics!: WorkerStatistics
729c563d 60 /**
b0a4db63 61 * Handler id of the `activeInterval` worker activity check.
729c563d 62 */
b0a4db63 63 protected activeInterval?: NodeJS.Timeout
c97c7edb 64 /**
729c563d 65 * Constructs a new poolifier worker.
c97c7edb 66 *
38e795c1
JB
67 * @param type - The type of async event.
68 * @param isMain - Whether this is the main worker or not.
82888165 69 * @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
70 * @param mainWorker - Reference to main worker.
71 * @param opts - Options for the worker.
c97c7edb
S
72 */
73 public constructor (
74 type: string,
c2ade475 75 protected readonly isMain: boolean,
a86b6df1
JB
76 taskFunctions:
77 | WorkerFunction<Data, Response>
78 | TaskFunctions<Data, Response>,
448ad581 79 protected readonly mainWorker: MainWorker,
d99ba5a8 80 protected readonly opts: WorkerOptions = {
e088a00c 81 /**
aee46736 82 * The kill behavior option on this worker or its default value.
e088a00c 83 */
1a81f8af 84 killBehavior: DEFAULT_KILL_BEHAVIOR,
e088a00c 85 /**
b0a4db63 86 * The maximum time to keep this worker active while idle.
e088a00c
JB
87 * The pool automatically checks and terminates this worker when the time expires.
88 */
1a81f8af 89 maxInactiveTime: DEFAULT_MAX_INACTIVE_TIME
4c35177b 90 }
c97c7edb
S
91 ) {
92 super(type)
e088a00c 93 this.checkWorkerOptions(this.opts)
a86b6df1 94 this.checkTaskFunctions(taskFunctions)
1f68cede 95 if (!this.isMain) {
3749facb 96 this.mainWorker?.on('message', this.messageListener.bind(this))
c97c7edb
S
97 }
98 }
99
41aa7dcd
JB
100 private checkWorkerOptions (opts: WorkerOptions): void {
101 this.opts.killBehavior = opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR
102 this.opts.maxInactiveTime =
103 opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME
571227f4 104 delete this.opts.async
41aa7dcd
JB
105 }
106
107 /**
a86b6df1 108 * Checks if the `taskFunctions` parameter is passed to the constructor.
41aa7dcd 109 *
82888165 110 * @param taskFunctions - The task function(s) parameter that should be checked.
41aa7dcd 111 */
a86b6df1
JB
112 private checkTaskFunctions (
113 taskFunctions:
114 | WorkerFunction<Data, Response>
115 | TaskFunctions<Data, Response>
116 ): void {
ec8fd331
JB
117 if (taskFunctions == null) {
118 throw new Error('taskFunctions parameter is mandatory')
119 }
a86b6df1 120 this.taskFunctions = new Map<string, WorkerFunction<Data, Response>>()
0d80593b 121 if (typeof taskFunctions === 'function') {
2a69b8c5
JB
122 const boundFn = taskFunctions.bind(this)
123 this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
124 this.taskFunctions.set(
125 typeof taskFunctions.name === 'string' &&
126 taskFunctions.name.trim().length > 0
127 ? taskFunctions.name
128 : 'fn1',
129 boundFn
130 )
0d80593b 131 } else if (isPlainObject(taskFunctions)) {
82888165 132 let firstEntry = true
a86b6df1 133 for (const [name, fn] of Object.entries(taskFunctions)) {
42e2b8a6
JB
134 if (typeof name !== 'string') {
135 throw new TypeError(
136 'A taskFunctions parameter object key is not a string'
137 )
138 }
a86b6df1 139 if (typeof fn !== 'function') {
0d80593b 140 throw new TypeError(
a86b6df1
JB
141 'A taskFunctions parameter object value is not a function'
142 )
143 }
2a69b8c5 144 const boundFn = fn.bind(this)
82888165 145 if (firstEntry) {
2a69b8c5 146 this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
82888165
JB
147 firstEntry = false
148 }
c50b93fb 149 this.taskFunctions.set(name, boundFn)
a86b6df1 150 }
630f0acf
JB
151 if (firstEntry) {
152 throw new Error('taskFunctions parameter object is empty')
153 }
a86b6df1 154 } else {
f34fdabe
JB
155 throw new TypeError(
156 'taskFunctions parameter is not a function or a plain object'
157 )
41aa7dcd
JB
158 }
159 }
160
968a2e8c
JB
161 /**
162 * Checks if the worker has a task function with the given name.
163 *
164 * @param name - The name of the task function to check.
165 * @returns Whether the worker has a task function with the given name or not.
13a332e6 166 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string.
968a2e8c
JB
167 */
168 public hasTaskFunction (name: string): boolean {
169 if (typeof name !== 'string') {
170 throw new TypeError('name parameter is not a string')
171 }
172 return this.taskFunctions.has(name)
173 }
174
175 /**
176 * Adds a task function to the worker.
177 * If a task function with the same name already exists, it is replaced.
178 *
179 * @param name - The name of the task function to add.
180 * @param fn - The task function to add.
181 * @returns Whether the task function was added or not.
13a332e6
JB
182 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string.
183 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
184 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `fn` parameter is not a function.
968a2e8c
JB
185 */
186 public addTaskFunction (
187 name: string,
188 fn: WorkerFunction<Data, Response>
189 ): boolean {
190 if (typeof name !== 'string') {
191 throw new TypeError('name parameter is not a string')
192 }
193 if (name === DEFAULT_TASK_NAME) {
194 throw new Error(
195 'Cannot add a task function with the default reserved name'
196 )
197 }
198 if (typeof fn !== 'function') {
199 throw new TypeError('fn parameter is not a function')
200 }
201 try {
646d040a 202 const boundFn = fn.bind(this)
968a2e8c
JB
203 if (
204 this.taskFunctions.get(name) ===
205 this.taskFunctions.get(DEFAULT_TASK_NAME)
206 ) {
2a69b8c5 207 this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
968a2e8c 208 }
2a69b8c5 209 this.taskFunctions.set(name, boundFn)
968a2e8c
JB
210 return true
211 } catch {
212 return false
213 }
214 }
215
216 /**
217 * Removes a task function from the worker.
218 *
219 * @param name - The name of the task function to remove.
220 * @returns Whether the task function existed and was removed or not.
13a332e6
JB
221 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string.
222 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
223 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the task function used as default task function.
968a2e8c
JB
224 */
225 public removeTaskFunction (name: string): boolean {
226 if (typeof name !== 'string') {
227 throw new TypeError('name parameter is not a string')
228 }
229 if (name === DEFAULT_TASK_NAME) {
230 throw new Error(
231 'Cannot remove the task function with the default reserved name'
232 )
233 }
234 if (
235 this.taskFunctions.get(name) === this.taskFunctions.get(DEFAULT_TASK_NAME)
236 ) {
237 throw new Error(
238 'Cannot remove the task function used as the default task function'
239 )
240 }
241 return this.taskFunctions.delete(name)
242 }
243
244 /**
c50b93fb
JB
245 * Lists the names of the worker's task functions.
246 *
247 * @returns The names of the worker's task functions.
248 */
249 public listTaskFunctions (): string[] {
250 return Array.from(this.taskFunctions.keys())
251 }
252
253 /**
254 * Sets the default task function to use in the worker.
968a2e8c
JB
255 *
256 * @param name - The name of the task function to use as default task function.
257 * @returns Whether the default task function was set or not.
13a332e6
JB
258 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string.
259 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
260 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is a non-existing task function.
968a2e8c
JB
261 */
262 public setDefaultTaskFunction (name: string): boolean {
263 if (typeof name !== 'string') {
264 throw new TypeError('name parameter is not a string')
265 }
266 if (name === DEFAULT_TASK_NAME) {
267 throw new Error(
268 'Cannot set the default task function reserved name as the default task function'
269 )
270 }
271 if (!this.taskFunctions.has(name)) {
272 throw new Error(
273 'Cannot set the default task function to a non-existing task function'
274 )
275 }
276 try {
277 this.taskFunctions.set(
278 DEFAULT_TASK_NAME,
2a69b8c5 279 this.taskFunctions.get(name) as WorkerFunction<Data, Response>
968a2e8c
JB
280 )
281 return true
282 } catch {
283 return false
284 }
285 }
286
aee46736
JB
287 /**
288 * Worker message listener.
289 *
6b813701 290 * @param message - The received message.
aee46736 291 */
6677a3d3 292 protected messageListener (message: MessageValue<Data, Data>): void {
826f42ee
JB
293 if (message.workerId === this.id) {
294 if (message.ready != null) {
295 // Startup message received
b0a4db63 296 this.sendReadyResponse()
826f42ee
JB
297 } else if (message.statistics != null) {
298 // Statistics message received
299 this.statistics = message.statistics
b0a4db63
JB
300 } else if (message.checkActive != null) {
301 // Check active message received
209d7d15
JB
302 !this.isMain && message.checkActive
303 ? this.startCheckActive()
304 : this.stopCheckActive()
826f42ee
JB
305 } else if (message.id != null && message.data != null) {
306 // Task message received
5c4d16da 307 this.run(message)
826f42ee
JB
308 } else if (message.kill === true) {
309 // Kill message received
209d7d15 310 !this.isMain && this.stopCheckActive()
826f42ee 311 this.emitDestroy()
cf597bc5 312 }
cf597bc5
JB
313 }
314 }
315
2431bdb4 316 /**
289cf7e3 317 * Sends the ready response to the main worker.
2431bdb4 318 */
b0a4db63 319 protected sendReadyResponse (): void {
2431bdb4
JB
320 !this.isMain && this.sendToMainWorker({ ready: true, workerId: this.id })
321 }
322
48487131 323 /**
b0a4db63 324 * Starts the worker check active interval.
48487131 325 */
b0a4db63 326 private startCheckActive (): void {
75d3401a 327 this.lastTaskTimestamp = performance.now()
b0a4db63
JB
328 this.activeInterval = setInterval(
329 this.checkActive.bind(this),
75d3401a 330 (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2
b55c265c 331 ).unref()
75d3401a
JB
332 }
333
48487131 334 /**
b0a4db63 335 * Stops the worker check active interval.
48487131 336 */
b0a4db63 337 private stopCheckActive (): void {
c3f498b5
JB
338 if (this.activeInterval != null) {
339 clearInterval(this.activeInterval)
340 delete this.activeInterval
341 }
48487131
JB
342 }
343
344 /**
345 * Checks if the worker should be terminated, because its living too long.
346 */
b0a4db63 347 private checkActive (): void {
48487131
JB
348 if (
349 performance.now() - this.lastTaskTimestamp >
350 (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME)
351 ) {
21f710aa 352 this.sendToMainWorker({ kill: this.opts.killBehavior, workerId: this.id })
48487131
JB
353 }
354 }
355
729c563d
S
356 /**
357 * Returns the main worker.
838898f1
S
358 *
359 * @returns Reference to the main worker.
729c563d 360 */
838898f1 361 protected getMainWorker (): MainWorker {
78cea37e 362 if (this.mainWorker == null) {
e102732c 363 throw new Error('Main worker not set')
838898f1
S
364 }
365 return this.mainWorker
366 }
c97c7edb 367
729c563d 368 /**
8accb8d5 369 * Sends a message to the main worker.
729c563d 370 *
38e795c1 371 * @param message - The response message.
729c563d 372 */
82f36766
JB
373 protected abstract sendToMainWorker (
374 message: MessageValue<Response, Data>
375 ): void
c97c7edb 376
729c563d 377 /**
8accb8d5 378 * Handles an error and convert it to a string so it can be sent back to the main worker.
729c563d 379 *
38e795c1 380 * @param e - The error raised by the worker.
ab80dc46 381 * @returns The error message.
729c563d 382 */
c97c7edb 383 protected handleError (e: Error | string): string {
985d0e79 384 return e instanceof Error ? e.message : e
c97c7edb
S
385 }
386
5c4d16da
JB
387 /**
388 * Runs the given task.
389 *
390 * @param task - The task to execute.
391 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found.
392 */
393 protected run (task: Task<Data>): void {
209d7d15
JB
394 if (this.isMain) {
395 throw new Error('Cannot run a task in the main worker')
396 }
5c4d16da
JB
397 const fn = this.getTaskFunction(task.name)
398 if (isAsyncFunction(fn)) {
399 this.runInAsyncScope(this.runAsync.bind(this), this, fn, task)
400 } else {
401 this.runInAsyncScope(this.runSync.bind(this), this, fn, task)
402 }
403 }
404
729c563d 405 /**
4dd93fcf 406 * Runs the given task function synchronously.
729c563d 407 *
5c4d16da
JB
408 * @param fn - Task function that will be executed.
409 * @param task - Input data for the task function.
729c563d 410 */
70a4f5ea 411 protected runSync (
48ef9107 412 fn: WorkerSyncFunction<Data, Response>,
5c4d16da 413 task: Task<Data>
c97c7edb
S
414 ): void {
415 try {
5c4d16da
JB
416 let taskPerformance = this.beginTaskPerformance(task.name)
417 const res = fn(task.data)
d715b7bc 418 taskPerformance = this.endTaskPerformance(taskPerformance)
3fafb1b2
JB
419 this.sendToMainWorker({
420 data: res,
d715b7bc 421 taskPerformance,
f59e1027 422 workerId: this.id,
5c4d16da 423 id: task.id
3fafb1b2 424 })
c97c7edb 425 } catch (e) {
985d0e79 426 const errorMessage = this.handleError(e as Error | string)
91ee39ed 427 this.sendToMainWorker({
82f36766 428 taskError: {
5c4d16da 429 name: task.name ?? DEFAULT_TASK_NAME,
985d0e79 430 message: errorMessage,
5c4d16da 431 data: task.data
82f36766 432 },
21f710aa 433 workerId: this.id,
5c4d16da 434 id: task.id
91ee39ed 435 })
6e9d10db 436 } finally {
c3f498b5 437 this.updateLastTaskTimestamp()
c97c7edb
S
438 }
439 }
440
729c563d 441 /**
4dd93fcf 442 * Runs the given task function asynchronously.
729c563d 443 *
5c4d16da
JB
444 * @param fn - Task function that will be executed.
445 * @param task - Input data for the task function.
729c563d 446 */
c97c7edb 447 protected runAsync (
48ef9107 448 fn: WorkerAsyncFunction<Data, Response>,
5c4d16da 449 task: Task<Data>
c97c7edb 450 ): void {
5c4d16da
JB
451 let taskPerformance = this.beginTaskPerformance(task.name)
452 fn(task.data)
c97c7edb 453 .then(res => {
d715b7bc 454 taskPerformance = this.endTaskPerformance(taskPerformance)
3fafb1b2
JB
455 this.sendToMainWorker({
456 data: res,
d715b7bc 457 taskPerformance,
f59e1027 458 workerId: this.id,
5c4d16da 459 id: task.id
3fafb1b2 460 })
c97c7edb
S
461 return null
462 })
463 .catch(e => {
985d0e79 464 const errorMessage = this.handleError(e as Error | string)
91ee39ed 465 this.sendToMainWorker({
82f36766 466 taskError: {
5c4d16da 467 name: task.name ?? DEFAULT_TASK_NAME,
985d0e79 468 message: errorMessage,
5c4d16da 469 data: task.data
82f36766 470 },
21f710aa 471 workerId: this.id,
5c4d16da 472 id: task.id
91ee39ed 473 })
6e9d10db
JB
474 })
475 .finally(() => {
c3f498b5 476 this.updateLastTaskTimestamp()
c97c7edb 477 })
6e9d10db 478 .catch(EMPTY_FUNCTION)
c97c7edb 479 }
ec8fd331 480
82888165 481 /**
5c4d16da 482 * Gets the task function with the given name.
82888165 483 *
ff128cc9 484 * @param name - Name of the task function that will be returned.
5c4d16da
JB
485 * @returns The task function.
486 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found.
82888165 487 */
ec8fd331 488 private getTaskFunction (name?: string): WorkerFunction<Data, Response> {
ff128cc9 489 name = name ?? DEFAULT_TASK_NAME
ec8fd331
JB
490 const fn = this.taskFunctions.get(name)
491 if (fn == null) {
ace229a1 492 throw new Error(`Task function '${name}' not found`)
ec8fd331
JB
493 }
494 return fn
495 }
62c15a68 496
197b4aa5 497 private beginTaskPerformance (name?: string): TaskPerformance {
8a970421 498 this.checkStatistics()
62c15a68 499 return {
ff128cc9 500 name: name ?? DEFAULT_TASK_NAME,
1c6fe997 501 timestamp: performance.now(),
b6b32453 502 ...(this.statistics.elu && { elu: performance.eventLoopUtilization() })
62c15a68
JB
503 }
504 }
505
d9d31201
JB
506 private endTaskPerformance (
507 taskPerformance: TaskPerformance
508 ): TaskPerformance {
8a970421 509 this.checkStatistics()
62c15a68
JB
510 return {
511 ...taskPerformance,
b6b32453
JB
512 ...(this.statistics.runTime && {
513 runTime: performance.now() - taskPerformance.timestamp
514 }),
515 ...(this.statistics.elu && {
62c15a68 516 elu: performance.eventLoopUtilization(taskPerformance.elu)
b6b32453 517 })
62c15a68
JB
518 }
519 }
8a970421
JB
520
521 private checkStatistics (): void {
522 if (this.statistics == null) {
523 throw new Error('Performance statistics computation requirements not set')
524 }
525 }
c3f498b5
JB
526
527 private updateLastTaskTimestamp (): void {
528 if (!this.isMain && this.activeInterval != null) {
529 this.lastTaskTimestamp = performance.now()
530 }
531 }
c97c7edb 532}