]> Piment Noir Git Repositories - poolifier.git/blob - src/worker/abstract-worker.ts
ba8b00aa01f3e8b1c8d50428ab5d3d2a2c02c505
[poolifier.git] / src / worker / abstract-worker.ts
1 import type { Worker } from 'node:cluster'
2 import type { MessagePort } from 'node:worker_threads'
3
4 import { performance } from 'node:perf_hooks'
5
6 import type {
7 MessageValue,
8 Task,
9 TaskFunctionProperties,
10 TaskPerformance,
11 WorkerStatistics,
12 } from '../utility-types.js'
13 import type {
14 TaskAsyncFunction,
15 TaskFunction,
16 TaskFunctionObject,
17 TaskFunctionOperationResult,
18 TaskFunctions,
19 TaskSyncFunction,
20 } from './task-functions.js'
21
22 import {
23 buildTaskFunctionProperties,
24 DEFAULT_TASK_NAME,
25 EMPTY_FUNCTION,
26 isAsyncFunction,
27 isPlainObject,
28 } from '../utils.js'
29 import { AbortError } from './abort-error.js'
30 import {
31 checkTaskFunctionName,
32 checkValidTaskFunctionObjectEntry,
33 checkValidWorkerOptions,
34 } from './utils.js'
35 import { KillBehaviors, type WorkerOptions } from './worker-options.js'
36
37 const DEFAULT_MAX_INACTIVE_TIME = 60000
38 const DEFAULT_WORKER_OPTIONS: Readonly<WorkerOptions> = Object.freeze({
39 /**
40 * The kill behavior option on this worker or its default value.
41 */
42 killBehavior: KillBehaviors.SOFT,
43 /**
44 * The function to call when the worker is killed.
45 */
46 killHandler: EMPTY_FUNCTION,
47 /**
48 * The maximum time to keep this worker active while idle.
49 * The pool automatically checks and terminates this worker when the time expires.
50 */
51 maxInactiveTime: DEFAULT_MAX_INACTIVE_TIME,
52 })
53
54 /**
55 * Base class that implements some shared logic for all poolifier workers.
56 * @typeParam MainWorker - Type of main worker.
57 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
58 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
59 */
60 export abstract class AbstractWorker<
61 MainWorker extends MessagePort | Worker,
62 Data = unknown,
63 Response = unknown
64 > {
65 /**
66 * Handler id of the `activeInterval` worker activity check.
67 */
68 protected activeInterval?: NodeJS.Timeout
69 /**
70 * Worker id.
71 */
72 protected abstract readonly id: number
73 /**
74 * Timestamp of the last task processed by this worker.
75 */
76 protected lastTaskTimestamp!: number
77
78 /**
79 * Performance statistics computation requirements.
80 */
81 protected statistics?: WorkerStatistics
82
83 /**
84 * Task abort functions processed by the worker when task operation 'abort' is received.
85 */
86 protected taskAbortFunctions: Map<
87 `${string}-${string}-${string}-${string}-${string}`,
88 () => void
89 >
90
91 /**
92 * Task function object(s) processed by the worker when the pool's `execute` method is invoked.
93 */
94 protected taskFunctions!: Map<string, TaskFunctionObject<Data, Response>>
95
96 /**
97 * Constructs a new poolifier worker.
98 * @param isMain - Whether this is the main worker or not.
99 * @param mainWorker - Reference to main worker.
100 * @param taskFunctions - Task function(s) processed by the worker when the pool's `execute` method is invoked. The first function is the default function.
101 * @param opts - Options for the worker.
102 */
103 public constructor (
104 protected readonly isMain: boolean | undefined,
105 private readonly mainWorker: MainWorker | null | undefined,
106 taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>,
107 protected opts: WorkerOptions = DEFAULT_WORKER_OPTIONS
108 ) {
109 if (this.isMain == null) {
110 throw new Error('isMain parameter is mandatory')
111 }
112 this.checkTaskFunctions(taskFunctions)
113 this.taskAbortFunctions = new Map<
114 `${string}-${string}-${string}-${string}-${string}`,
115 () => void
116 >()
117 this.checkWorkerOptions(this.opts)
118 if (!this.isMain) {
119 this.getMainWorker().once('message', this.handleReadyMessage.bind(this))
120 }
121 }
122
123 /**
124 * Adds a task function to the worker.
125 * If a task function with the same name already exists, it is replaced.
126 * @param name - The name of the task function to add.
127 * @param fn - The task function to add.
128 * @returns Whether the task function was added or not.
129 */
130 public addTaskFunction (
131 name: string,
132 fn: TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>
133 ): TaskFunctionOperationResult {
134 try {
135 checkTaskFunctionName(name)
136 if (name === DEFAULT_TASK_NAME) {
137 throw new Error(
138 'Cannot add a task function with the default reserved name'
139 )
140 }
141 if (typeof fn === 'function') {
142 fn = { taskFunction: fn } satisfies TaskFunctionObject<Data, Response>
143 }
144 checkValidTaskFunctionObjectEntry<Data, Response>(name, fn)
145 fn.taskFunction = fn.taskFunction.bind(this)
146 if (
147 this.taskFunctions.get(name) ===
148 this.taskFunctions.get(DEFAULT_TASK_NAME)
149 ) {
150 this.taskFunctions.set(DEFAULT_TASK_NAME, fn)
151 }
152 this.taskFunctions.set(name, fn)
153 this.sendTaskFunctionsPropertiesToMainWorker()
154 return { status: true }
155 } catch (error) {
156 return { error: error as Error, status: false }
157 }
158 }
159
160 /**
161 * Checks if the worker has a task function with the given name.
162 * @param name - The name of the task function to check.
163 * @returns Whether the worker has a task function with the given name or not.
164 */
165 public hasTaskFunction (name: string): TaskFunctionOperationResult {
166 try {
167 checkTaskFunctionName(name)
168 } catch (error) {
169 return { error: error as Error, status: false }
170 }
171 return { status: this.taskFunctions.has(name) }
172 }
173
174 /**
175 * Lists the properties of the worker's task functions.
176 * @returns The properties of the worker's task functions.
177 */
178 public listTaskFunctionsProperties (): TaskFunctionProperties[] {
179 let defaultTaskFunctionName = DEFAULT_TASK_NAME
180 for (const [name, fnObj] of this.taskFunctions) {
181 if (
182 name !== DEFAULT_TASK_NAME &&
183 fnObj === this.taskFunctions.get(DEFAULT_TASK_NAME)
184 ) {
185 defaultTaskFunctionName = name
186 break
187 }
188 }
189 const taskFunctionsProperties: TaskFunctionProperties[] = []
190 for (const [name, fnObj] of this.taskFunctions) {
191 if (name === DEFAULT_TASK_NAME || name === defaultTaskFunctionName) {
192 continue
193 }
194 taskFunctionsProperties.push(buildTaskFunctionProperties(name, fnObj))
195 }
196 return [
197 buildTaskFunctionProperties(
198 DEFAULT_TASK_NAME,
199 this.taskFunctions.get(DEFAULT_TASK_NAME)
200 ),
201 buildTaskFunctionProperties(
202 defaultTaskFunctionName,
203 this.taskFunctions.get(defaultTaskFunctionName)
204 ),
205 ...taskFunctionsProperties,
206 ]
207 }
208
209 /**
210 * Removes a task function from the worker.
211 * @param name - The name of the task function to remove.
212 * @returns Whether the task function existed and was removed or not.
213 */
214 public removeTaskFunction (name: string): TaskFunctionOperationResult {
215 try {
216 checkTaskFunctionName(name)
217 if (name === DEFAULT_TASK_NAME) {
218 throw new Error(
219 'Cannot remove the task function with the default reserved name'
220 )
221 }
222 if (
223 this.taskFunctions.get(name) ===
224 this.taskFunctions.get(DEFAULT_TASK_NAME)
225 ) {
226 throw new Error(
227 'Cannot remove the task function used as the default task function'
228 )
229 }
230 const deleteStatus = this.taskFunctions.delete(name)
231 this.sendTaskFunctionsPropertiesToMainWorker()
232 return { status: deleteStatus }
233 } catch (error) {
234 return { error: error as Error, status: false }
235 }
236 }
237
238 /**
239 * Sets the default task function to use in the worker.
240 * @param name - The name of the task function to use as default task function.
241 * @returns Whether the default task function was set or not.
242 */
243 public setDefaultTaskFunction (name: string): TaskFunctionOperationResult {
244 try {
245 checkTaskFunctionName(name)
246 if (name === DEFAULT_TASK_NAME) {
247 throw new Error(
248 'Cannot set the default task function reserved name as the default task function'
249 )
250 }
251 if (!this.taskFunctions.has(name)) {
252 throw new Error(
253 'Cannot set the default task function to a non-existing task function'
254 )
255 }
256 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
257 this.taskFunctions.set(DEFAULT_TASK_NAME, this.taskFunctions.get(name)!)
258 this.sendTaskFunctionsPropertiesToMainWorker()
259 return { status: true }
260 } catch (error) {
261 return { error: error as Error, status: false }
262 }
263 }
264
265 /**
266 * Returns the main worker.
267 * @returns Reference to the main worker.
268 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the main worker is not set.
269 */
270 protected getMainWorker (): MainWorker {
271 if (this.mainWorker == null) {
272 throw new Error('Main worker not set')
273 }
274 return this.mainWorker
275 }
276
277 /**
278 * Handles a worker error.
279 * @param error - The error raised by the worker.
280 * @returns The worker error object.
281 */
282 protected abstract handleError (error: Error): {
283 aborted: boolean
284 error?: Error
285 message: string
286 stack?: string
287 }
288
289 /**
290 * Handles a kill message sent by the main worker.
291 * @param message - The kill message.
292 */
293 protected handleKillMessage (message: MessageValue<Data>): void {
294 this.stopCheckActive()
295 if (isAsyncFunction(this.opts.killHandler)) {
296 ;(this.opts.killHandler as () => Promise<void>)()
297 .then(() => {
298 this.sendToMainWorker({ kill: 'success' })
299 return undefined
300 })
301 .catch(() => {
302 this.sendToMainWorker({ kill: 'failure' })
303 })
304 } else {
305 try {
306 ;(this.opts.killHandler as (() => void) | undefined)?.()
307 this.sendToMainWorker({ kill: 'success' })
308 } catch {
309 this.sendToMainWorker({ kill: 'failure' })
310 }
311 }
312 }
313
314 /**
315 * Handles the ready message sent by the main worker.
316 * @param message - The ready message.
317 */
318 protected abstract handleReadyMessage (message: MessageValue<Data>): void
319
320 protected handleTaskFunctionOperationMessage (
321 message: MessageValue<Data>
322 ): void {
323 const { taskFunction, taskFunctionOperation, taskFunctionProperties } =
324 message
325 if (taskFunctionProperties == null) {
326 throw new Error(
327 'Cannot handle task function operation message without task function properties'
328 )
329 }
330 let response: TaskFunctionOperationResult
331 switch (taskFunctionOperation) {
332 case 'add':
333 if (typeof taskFunction !== 'string') {
334 throw new Error(
335 `Cannot handle task function operation ${taskFunctionOperation} message without task function`
336 )
337 }
338 response = this.addTaskFunction(taskFunctionProperties.name, {
339 // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func, @typescript-eslint/no-unsafe-call
340 taskFunction: new Function(
341 `return (${taskFunction})`
342 )() as TaskFunction<Data, Response>,
343 ...(taskFunctionProperties.priority != null && {
344 priority: taskFunctionProperties.priority,
345 }),
346 ...(taskFunctionProperties.strategy != null && {
347 strategy: taskFunctionProperties.strategy,
348 }),
349 })
350 break
351 case 'default':
352 response = this.setDefaultTaskFunction(taskFunctionProperties.name)
353 break
354 case 'remove':
355 response = this.removeTaskFunction(taskFunctionProperties.name)
356 break
357 default:
358 response = {
359 error: new Error(
360 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
361 `Unknown task function operation: ${taskFunctionOperation!}`
362 ),
363 status: false,
364 }
365 break
366 }
367 const { error, status } = response
368 this.sendToMainWorker({
369 taskFunctionOperation,
370 taskFunctionOperationStatus: status,
371 taskFunctionProperties,
372 ...(!status &&
373 error != null && {
374 workerError: {
375 name: taskFunctionProperties.name,
376 ...this.handleError(error),
377 },
378 }),
379 })
380 }
381
382 /**
383 * Worker message listener.
384 * @param message - The received message.
385 */
386 protected messageListener (message: MessageValue<Data>): void {
387 this.checkMessageWorkerId(message)
388 const {
389 checkActive,
390 data,
391 kill,
392 statistics,
393 taskFunctionOperation,
394 taskId,
395 taskOperation,
396 } = message
397 if (statistics != null) {
398 // Statistics message received
399 this.statistics = statistics
400 } else if (checkActive != null) {
401 // Check active message received
402 checkActive ? this.startCheckActive() : this.stopCheckActive()
403 } else if (taskFunctionOperation != null) {
404 // Task function operation message received
405 this.handleTaskFunctionOperationMessage(message)
406 } else if (taskId != null && data != null) {
407 // Task message received
408 this.run(message)
409 } else if (taskOperation === 'abort' && taskId != null) {
410 // Abort task operation message received
411 if (this.taskAbortFunctions.has(taskId)) {
412 this.taskAbortFunctions.get(taskId)?.()
413 }
414 } else if (kill === true) {
415 // Kill message received
416 this.handleKillMessage(message)
417 }
418 }
419
420 /**
421 * Runs the given task.
422 * @param task - The task to execute.
423 */
424 protected readonly run = (task: Task<Data>): void => {
425 const { abortable, data, name, taskId } = task
426 const taskFunctionName = name ?? DEFAULT_TASK_NAME
427 if (!this.taskFunctions.has(taskFunctionName)) {
428 this.sendToMainWorker({
429 taskId,
430 workerError: {
431 data,
432 name,
433 ...this.handleError(
434 new Error(`Task function '${taskFunctionName}' not found`)
435 ),
436 },
437 })
438 return
439 }
440 let fn: TaskFunction<Data, Response>
441 if (abortable === true) {
442 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
443 fn = this.getAbortableTaskFunction(taskFunctionName, taskId!)
444 } else {
445 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
446 fn = this.taskFunctions.get(taskFunctionName)!.taskFunction
447 }
448 if (isAsyncFunction(fn)) {
449 this.runAsync(fn as TaskAsyncFunction<Data, Response>, task)
450 } else {
451 this.runSync(fn as TaskSyncFunction<Data, Response>, task)
452 }
453 }
454
455 /**
456 * Runs the given task function asynchronously.
457 * @param fn - Task function that will be executed.
458 * @param task - Input data for the task function.
459 */
460 protected readonly runAsync = (
461 fn: TaskAsyncFunction<Data, Response>,
462 task: Task<Data>
463 ): void => {
464 const { abortable, data, name, taskId } = task
465 let taskPerformance = this.beginTaskPerformance(name)
466 fn(data)
467 .then(res => {
468 taskPerformance = this.endTaskPerformance(taskPerformance)
469 this.sendToMainWorker({
470 data: res,
471 taskId,
472 taskPerformance,
473 })
474 return undefined
475 })
476 .catch((error: unknown) => {
477 this.sendToMainWorker({
478 taskId,
479 workerError: {
480 data,
481 name,
482 ...this.handleError(error as Error),
483 },
484 })
485 })
486 .finally(() => {
487 this.updateLastTaskTimestamp()
488 if (abortable === true) {
489 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
490 this.taskAbortFunctions.delete(taskId!)
491 }
492 })
493 .catch(EMPTY_FUNCTION)
494 }
495
496 /**
497 * Runs the given task function synchronously.
498 * @param fn - Task function that will be executed.
499 * @param task - Input data for the task function.
500 */
501 protected readonly runSync = (
502 fn: TaskSyncFunction<Data, Response>,
503 task: Task<Data>
504 ): void => {
505 const { abortable, data, name, taskId } = task
506 try {
507 let taskPerformance = this.beginTaskPerformance(name)
508 const res = fn(data)
509 taskPerformance = this.endTaskPerformance(taskPerformance)
510 this.sendToMainWorker({
511 data: res,
512 taskId,
513 taskPerformance,
514 })
515 } catch (error) {
516 this.sendToMainWorker({
517 taskId,
518 workerError: {
519 data,
520 name,
521 ...this.handleError(error as Error),
522 },
523 })
524 } finally {
525 this.updateLastTaskTimestamp()
526 if (abortable === true) {
527 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
528 this.taskAbortFunctions.delete(taskId!)
529 }
530 }
531 }
532
533 /**
534 * Sends task functions properties to the main worker.
535 */
536 protected sendTaskFunctionsPropertiesToMainWorker (): void {
537 this.sendToMainWorker({
538 taskFunctionsProperties: this.listTaskFunctionsProperties(),
539 })
540 }
541
542 /**
543 * Sends a message to main worker.
544 * @param message - The response message.
545 */
546 protected abstract sendToMainWorker (
547 message: MessageValue<Response, Data>
548 ): void
549
550 private beginTaskPerformance (name?: string): TaskPerformance {
551 if (this.statistics == null) {
552 throw new Error('Performance statistics computation requirements not set')
553 }
554 return {
555 name: name ?? DEFAULT_TASK_NAME,
556 timestamp: performance.now(),
557 ...(this.statistics.elu && {
558 elu: performance.eventLoopUtilization(),
559 }),
560 }
561 }
562
563 /**
564 * Checks if the worker should be terminated, because its living too long.
565 */
566 private checkActive (): void {
567 if (
568 performance.now() - this.lastTaskTimestamp >
569 (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME)
570 ) {
571 this.sendToMainWorker({ kill: this.opts.killBehavior })
572 }
573 }
574
575 /**
576 * Check if the message worker id is set and matches the worker id.
577 * @param message - The message to check.
578 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the message worker id is not set or does not match the worker id.
579 */
580 private checkMessageWorkerId (message: MessageValue<Data>): void {
581 if (message.workerId == null) {
582 throw new Error(
583 `Message worker id is not set: ${JSON.stringify(message)}`
584 )
585 }
586 if (message.workerId !== this.id) {
587 throw new Error(
588 `Message worker id ${message.workerId.toString()} does not match the worker id ${this.id.toString()}: ${JSON.stringify(message)}`
589 )
590 }
591 }
592
593 /**
594 * Checks if the `taskFunctions` parameter is passed to the constructor and valid.
595 * @param taskFunctions - The task function(s) parameter that should be checked.
596 */
597 private checkTaskFunctions (
598 taskFunctions:
599 | TaskFunction<Data, Response>
600 | TaskFunctions<Data, Response>
601 | undefined
602 ): void {
603 if (taskFunctions == null) {
604 throw new Error('taskFunctions parameter is mandatory')
605 }
606 this.taskFunctions = new Map<string, TaskFunctionObject<Data, Response>>()
607 if (typeof taskFunctions === 'function') {
608 const fnObj = { taskFunction: taskFunctions.bind(this) }
609 this.taskFunctions.set(DEFAULT_TASK_NAME, fnObj)
610 this.taskFunctions.set(
611 typeof taskFunctions.name === 'string' &&
612 taskFunctions.name.trim().length > 0
613 ? taskFunctions.name
614 : 'fn1',
615 fnObj
616 )
617 } else if (isPlainObject(taskFunctions)) {
618 let firstEntry = true
619 for (let [name, fnObj] of Object.entries(taskFunctions)) {
620 if (typeof fnObj === 'function') {
621 fnObj = { taskFunction: fnObj } satisfies TaskFunctionObject<
622 Data,
623 Response
624 >
625 }
626 checkValidTaskFunctionObjectEntry<Data, Response>(name, fnObj)
627 fnObj.taskFunction = fnObj.taskFunction.bind(this)
628 if (firstEntry) {
629 this.taskFunctions.set(DEFAULT_TASK_NAME, fnObj)
630 firstEntry = false
631 }
632 this.taskFunctions.set(name, fnObj)
633 }
634 if (firstEntry) {
635 throw new Error('taskFunctions parameter object is empty')
636 }
637 } else {
638 throw new TypeError(
639 'taskFunctions parameter is not a function or a plain object'
640 )
641 }
642 }
643
644 private checkWorkerOptions (opts: WorkerOptions): void {
645 checkValidWorkerOptions(opts)
646 this.opts = { ...DEFAULT_WORKER_OPTIONS, ...opts }
647 }
648
649 private endTaskPerformance (
650 taskPerformance: TaskPerformance
651 ): TaskPerformance {
652 if (this.statistics == null) {
653 throw new Error('Performance statistics computation requirements not set')
654 }
655 return {
656 ...taskPerformance,
657 ...(this.statistics.runTime && {
658 runTime: performance.now() - taskPerformance.timestamp,
659 }),
660 ...(this.statistics.elu && {
661 elu: performance.eventLoopUtilization(taskPerformance.elu),
662 }),
663 }
664 }
665
666 /**
667 * Gets abortable task function.
668 * An abortable promise is built to permit the task to be aborted.
669 * @param name - The name of the task.
670 * @param taskId - The task id.
671 * @returns The abortable task function.
672 */
673 private getAbortableTaskFunction (
674 name: string,
675 taskId: `${string}-${string}-${string}-${string}-${string}`
676 ): TaskAsyncFunction<Data, Response> {
677 return async (data?: Data): Promise<Response> =>
678 await new Promise<Response>(
679 (resolve, reject: (reason?: unknown) => void) => {
680 this.taskAbortFunctions.set(taskId, () => {
681 reject(new AbortError(`Task '${name}' id '${taskId}' aborted`))
682 })
683 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
684 const taskFunction = this.taskFunctions.get(name)!.taskFunction
685 if (isAsyncFunction(taskFunction)) {
686 ;(taskFunction as TaskAsyncFunction<Data, Response>)(data)
687 .then(resolve)
688 .catch(reject)
689 } else {
690 resolve((taskFunction as TaskSyncFunction<Data, Response>)(data))
691 }
692 }
693 )
694 }
695
696 /**
697 * Starts the worker check active interval.
698 */
699 private startCheckActive (): void {
700 this.lastTaskTimestamp = performance.now()
701 this.activeInterval = setInterval(
702 this.checkActive.bind(this),
703 (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2
704 )
705 this.activeInterval.unref()
706 }
707
708 /**
709 * Stops the worker check active interval.
710 */
711 private stopCheckActive (): void {
712 if (this.activeInterval != null) {
713 clearInterval(this.activeInterval)
714 this.activeInterval = undefined
715 }
716 }
717
718 private updateLastTaskTimestamp (): void {
719 if (this.activeInterval != null) {
720 this.lastTaskTimestamp = performance.now()
721 }
722 }
723 }