Commit | Line | Data |
---|---|---|
fc3e6586 | 1 | import { AsyncResource } from 'node:async_hooks' |
6677a3d3 | 2 | import type { Worker } from 'node:cluster' |
fc3e6586 | 3 | import type { MessagePort } from 'node:worker_threads' |
d715b7bc JB |
4 | import { performance } from 'node:perf_hooks' |
5 | import type { | |
6 | MessageValue, | |
5c4d16da | 7 | Task, |
d715b7bc JB |
8 | TaskPerformance, |
9 | WorkerStatistics | |
10 | } from '../utility-types' | |
ff128cc9 JB |
11 | import { |
12 | DEFAULT_TASK_NAME, | |
13 | EMPTY_FUNCTION, | |
14 | isAsyncFunction, | |
15 | isPlainObject | |
16 | } from '../utils' | |
241f23c2 JB |
17 | import { |
18 | type KillBehavior, | |
19 | KillBehaviors, | |
20 | type WorkerOptions | |
21 | } from './worker-options' | |
b6b32453 | 22 | import type { |
82ea6492 JB |
23 | TaskAsyncFunction, |
24 | TaskFunction, | |
b6b32453 | 25 | TaskFunctions, |
82ea6492 JB |
26 | TaskSyncFunction |
27 | } from './task-functions' | |
4c35177b | 28 | |
978aad6f | 29 | const DEFAULT_MAX_INACTIVE_TIME = 60000 |
1a81f8af | 30 | const 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 | 39 | export 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 | */ | |
82ea6492 | 51 | protected taskFunctions!: Map<string, TaskFunction<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. | |
38e795c1 | 69 | * @param mainWorker - Reference to main worker. |
85aeb3f3 | 70 | * @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 | 71 | * @param opts - Options for the worker. |
c97c7edb S |
72 | */ |
73 | public constructor ( | |
74 | type: string, | |
c2ade475 | 75 | protected readonly isMain: boolean, |
6c0c538c | 76 | private readonly mainWorker: MainWorker, |
82ea6492 | 77 | taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, |
d99ba5a8 | 78 | protected readonly opts: WorkerOptions = { |
e088a00c | 79 | /** |
aee46736 | 80 | * The kill behavior option on this worker or its default value. |
e088a00c | 81 | */ |
1a81f8af | 82 | killBehavior: DEFAULT_KILL_BEHAVIOR, |
e088a00c | 83 | /** |
b0a4db63 | 84 | * The maximum time to keep this worker active while idle. |
e088a00c JB |
85 | * The pool automatically checks and terminates this worker when the time expires. |
86 | */ | |
df9aaf20 JB |
87 | maxInactiveTime: DEFAULT_MAX_INACTIVE_TIME, |
88 | /** | |
89 | * The function to call when the worker is killed. | |
90 | */ | |
91 | killHandler: EMPTY_FUNCTION | |
4c35177b | 92 | } |
c97c7edb S |
93 | ) { |
94 | super(type) | |
e088a00c | 95 | this.checkWorkerOptions(this.opts) |
a86b6df1 | 96 | this.checkTaskFunctions(taskFunctions) |
1f68cede | 97 | if (!this.isMain) { |
85aeb3f3 | 98 | this.getMainWorker()?.on('message', this.handleReadyMessage.bind(this)) |
c97c7edb S |
99 | } |
100 | } | |
101 | ||
41aa7dcd JB |
102 | private checkWorkerOptions (opts: WorkerOptions): void { |
103 | this.opts.killBehavior = opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR | |
104 | this.opts.maxInactiveTime = | |
105 | opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME | |
df9aaf20 | 106 | this.opts.killHandler = opts.killHandler ?? EMPTY_FUNCTION |
3c5dc3fb | 107 | delete this.opts.async |
41aa7dcd JB |
108 | } |
109 | ||
110 | /** | |
a86b6df1 | 111 | * Checks if the `taskFunctions` parameter is passed to the constructor. |
41aa7dcd | 112 | * |
82888165 | 113 | * @param taskFunctions - The task function(s) parameter that should be checked. |
41aa7dcd | 114 | */ |
a86b6df1 | 115 | private checkTaskFunctions ( |
82ea6492 | 116 | taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response> |
a86b6df1 | 117 | ): void { |
ec8fd331 JB |
118 | if (taskFunctions == null) { |
119 | throw new Error('taskFunctions parameter is mandatory') | |
120 | } | |
82ea6492 | 121 | this.taskFunctions = new Map<string, TaskFunction<Data, Response>>() |
0d80593b | 122 | if (typeof taskFunctions === 'function') { |
2a69b8c5 JB |
123 | const boundFn = taskFunctions.bind(this) |
124 | this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn) | |
125 | this.taskFunctions.set( | |
126 | typeof taskFunctions.name === 'string' && | |
8ebe6c30 | 127 | taskFunctions.name.trim().length > 0 |
2a69b8c5 JB |
128 | ? taskFunctions.name |
129 | : 'fn1', | |
130 | boundFn | |
131 | ) | |
0d80593b | 132 | } else if (isPlainObject(taskFunctions)) { |
82888165 | 133 | let firstEntry = true |
a86b6df1 | 134 | for (const [name, fn] of Object.entries(taskFunctions)) { |
42e2b8a6 JB |
135 | if (typeof name !== 'string') { |
136 | throw new TypeError( | |
137 | 'A taskFunctions parameter object key is not a string' | |
138 | ) | |
139 | } | |
90d7d101 JB |
140 | if (typeof name === 'string' && name.trim().length === 0) { |
141 | throw new TypeError( | |
142 | 'A taskFunctions parameter object key an empty string' | |
143 | ) | |
144 | } | |
a86b6df1 | 145 | if (typeof fn !== 'function') { |
0d80593b | 146 | throw new TypeError( |
a86b6df1 JB |
147 | 'A taskFunctions parameter object value is not a function' |
148 | ) | |
149 | } | |
2a69b8c5 | 150 | const boundFn = fn.bind(this) |
82888165 | 151 | if (firstEntry) { |
2a69b8c5 | 152 | this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn) |
82888165 JB |
153 | firstEntry = false |
154 | } | |
c50b93fb | 155 | this.taskFunctions.set(name, boundFn) |
a86b6df1 | 156 | } |
630f0acf JB |
157 | if (firstEntry) { |
158 | throw new Error('taskFunctions parameter object is empty') | |
159 | } | |
a86b6df1 | 160 | } else { |
f34fdabe JB |
161 | throw new TypeError( |
162 | 'taskFunctions parameter is not a function or a plain object' | |
163 | ) | |
41aa7dcd JB |
164 | } |
165 | } | |
166 | ||
968a2e8c JB |
167 | /** |
168 | * Checks if the worker has a task function with the given name. | |
169 | * | |
170 | * @param name - The name of the task function to check. | |
171 | * @returns Whether the worker has a task function with the given name or not. | |
9e746eec | 172 | * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string. |
968a2e8c JB |
173 | */ |
174 | public hasTaskFunction (name: string): boolean { | |
175 | if (typeof name !== 'string') { | |
176 | throw new TypeError('name parameter is not a string') | |
177 | } | |
90d7d101 JB |
178 | if (typeof name === 'string' && name.trim().length === 0) { |
179 | throw new TypeError('name parameter is an empty string') | |
180 | } | |
968a2e8c JB |
181 | return this.taskFunctions.has(name) |
182 | } | |
183 | ||
184 | /** | |
185 | * Adds a task function to the worker. | |
186 | * If a task function with the same name already exists, it is replaced. | |
187 | * | |
188 | * @param name - The name of the task function to add. | |
189 | * @param fn - The task function to add. | |
190 | * @returns Whether the task function was added or not. | |
9e746eec | 191 | * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string. |
13a332e6 JB |
192 | * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name. |
193 | * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `fn` parameter is not a function. | |
968a2e8c JB |
194 | */ |
195 | public addTaskFunction ( | |
196 | name: string, | |
82ea6492 | 197 | fn: TaskFunction<Data, Response> |
968a2e8c JB |
198 | ): boolean { |
199 | if (typeof name !== 'string') { | |
200 | throw new TypeError('name parameter is not a string') | |
201 | } | |
90d7d101 JB |
202 | if (typeof name === 'string' && name.trim().length === 0) { |
203 | throw new TypeError('name parameter is an empty string') | |
204 | } | |
968a2e8c JB |
205 | if (name === DEFAULT_TASK_NAME) { |
206 | throw new Error( | |
207 | 'Cannot add a task function with the default reserved name' | |
208 | ) | |
209 | } | |
210 | if (typeof fn !== 'function') { | |
211 | throw new TypeError('fn parameter is not a function') | |
212 | } | |
213 | try { | |
646d040a | 214 | const boundFn = fn.bind(this) |
968a2e8c JB |
215 | if ( |
216 | this.taskFunctions.get(name) === | |
217 | this.taskFunctions.get(DEFAULT_TASK_NAME) | |
218 | ) { | |
2a69b8c5 | 219 | this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn) |
968a2e8c | 220 | } |
2a69b8c5 | 221 | this.taskFunctions.set(name, boundFn) |
90d7d101 | 222 | this.sendTaskFunctionsListToMainWorker() |
968a2e8c JB |
223 | return true |
224 | } catch { | |
225 | return false | |
226 | } | |
227 | } | |
228 | ||
229 | /** | |
230 | * Removes a task function from the worker. | |
231 | * | |
232 | * @param name - The name of the task function to remove. | |
233 | * @returns Whether the task function existed and was removed or not. | |
9e746eec | 234 | * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string. |
13a332e6 JB |
235 | * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name. |
236 | * @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 |
237 | */ |
238 | public removeTaskFunction (name: string): boolean { | |
239 | if (typeof name !== 'string') { | |
240 | throw new TypeError('name parameter is not a string') | |
241 | } | |
90d7d101 JB |
242 | if (typeof name === 'string' && name.trim().length === 0) { |
243 | throw new TypeError('name parameter is an empty string') | |
244 | } | |
968a2e8c JB |
245 | if (name === DEFAULT_TASK_NAME) { |
246 | throw new Error( | |
247 | 'Cannot remove the task function with the default reserved name' | |
248 | ) | |
249 | } | |
250 | if ( | |
251 | this.taskFunctions.get(name) === this.taskFunctions.get(DEFAULT_TASK_NAME) | |
252 | ) { | |
253 | throw new Error( | |
254 | 'Cannot remove the task function used as the default task function' | |
255 | ) | |
256 | } | |
90d7d101 JB |
257 | const deleteStatus = this.taskFunctions.delete(name) |
258 | this.sendTaskFunctionsListToMainWorker() | |
259 | return deleteStatus | |
968a2e8c JB |
260 | } |
261 | ||
262 | /** | |
c50b93fb JB |
263 | * Lists the names of the worker's task functions. |
264 | * | |
265 | * @returns The names of the worker's task functions. | |
266 | */ | |
267 | public listTaskFunctions (): string[] { | |
440dd7d7 | 268 | return [...this.taskFunctions.keys()] |
c50b93fb JB |
269 | } |
270 | ||
271 | /** | |
272 | * Sets the default task function to use in the worker. | |
968a2e8c JB |
273 | * |
274 | * @param name - The name of the task function to use as default task function. | |
275 | * @returns Whether the default task function was set or not. | |
9e746eec | 276 | * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string. |
13a332e6 JB |
277 | * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name. |
278 | * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is a non-existing task function. | |
968a2e8c JB |
279 | */ |
280 | public setDefaultTaskFunction (name: string): boolean { | |
281 | if (typeof name !== 'string') { | |
282 | throw new TypeError('name parameter is not a string') | |
283 | } | |
90d7d101 JB |
284 | if (typeof name === 'string' && name.trim().length === 0) { |
285 | throw new TypeError('name parameter is an empty string') | |
286 | } | |
968a2e8c JB |
287 | if (name === DEFAULT_TASK_NAME) { |
288 | throw new Error( | |
289 | 'Cannot set the default task function reserved name as the default task function' | |
290 | ) | |
291 | } | |
292 | if (!this.taskFunctions.has(name)) { | |
293 | throw new Error( | |
294 | 'Cannot set the default task function to a non-existing task function' | |
295 | ) | |
296 | } | |
297 | try { | |
298 | this.taskFunctions.set( | |
299 | DEFAULT_TASK_NAME, | |
82ea6492 | 300 | this.taskFunctions.get(name) as TaskFunction<Data, Response> |
968a2e8c JB |
301 | ) |
302 | return true | |
303 | } catch { | |
304 | return false | |
305 | } | |
306 | } | |
307 | ||
a038b517 JB |
308 | /** |
309 | * Handles the ready message sent by the main worker. | |
310 | * | |
311 | * @param message - The ready message. | |
312 | */ | |
313 | protected abstract handleReadyMessage (message: MessageValue<Data>): void | |
314 | ||
aee46736 JB |
315 | /** |
316 | * Worker message listener. | |
317 | * | |
6b813701 | 318 | * @param message - The received message. |
aee46736 | 319 | */ |
85aeb3f3 | 320 | protected messageListener (message: MessageValue<Data>): void { |
9e746eec | 321 | this.checkMessageWorkerId(message) |
310de0aa JB |
322 | if (message.statistics != null) { |
323 | // Statistics message received | |
324 | this.statistics = message.statistics | |
325 | } else if (message.checkActive != null) { | |
326 | // Check active message received | |
327 | message.checkActive ? this.startCheckActive() : this.stopCheckActive() | |
328 | } else if (message.taskId != null && message.data != null) { | |
329 | // Task message received | |
330 | this.run(message) | |
331 | } else if (message.kill === true) { | |
332 | // Kill message received | |
333 | this.handleKillMessage(message) | |
cf597bc5 JB |
334 | } |
335 | } | |
336 | ||
984dc9c8 JB |
337 | /** |
338 | * Handles a kill message sent by the main worker. | |
339 | * | |
340 | * @param message - The kill message. | |
341 | */ | |
342 | protected handleKillMessage (message: MessageValue<Data>): void { | |
29d8b961 | 343 | this.stopCheckActive() |
07588f30 JB |
344 | if (isAsyncFunction(this.opts.killHandler)) { |
345 | (this.opts.killHandler?.() as Promise<void>) | |
1e3214b6 JB |
346 | .then(() => { |
347 | this.sendToMainWorker({ kill: 'success', workerId: this.id }) | |
348 | return null | |
349 | }) | |
350 | .catch(() => { | |
351 | this.sendToMainWorker({ kill: 'failure', workerId: this.id }) | |
352 | }) | |
353 | .finally(() => { | |
354 | this.emitDestroy() | |
355 | }) | |
07588f30 JB |
356 | .catch(EMPTY_FUNCTION) |
357 | } else { | |
1e3214b6 JB |
358 | try { |
359 | // eslint-disable-next-line @typescript-eslint/no-invalid-void-type | |
360 | this.opts.killHandler?.() as void | |
361 | this.sendToMainWorker({ kill: 'success', workerId: this.id }) | |
7c8ac84e | 362 | } catch { |
1e3214b6 JB |
363 | this.sendToMainWorker({ kill: 'failure', workerId: this.id }) |
364 | } finally { | |
365 | this.emitDestroy() | |
366 | } | |
07588f30 | 367 | } |
984dc9c8 JB |
368 | } |
369 | ||
9e746eec JB |
370 | /** |
371 | * Check if the message worker id is set and matches the worker id. | |
372 | * | |
373 | * @param message - The message to check. | |
374 | * @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. | |
375 | */ | |
376 | private checkMessageWorkerId (message: MessageValue<Data>): void { | |
377 | if (message.workerId == null) { | |
378 | throw new Error('Message worker id is not set') | |
379 | } else if (message.workerId != null && message.workerId !== this.id) { | |
380 | throw new Error( | |
381 | `Message worker id ${message.workerId} does not match the worker id ${this.id}` | |
382 | ) | |
383 | } | |
384 | } | |
385 | ||
48487131 | 386 | /** |
b0a4db63 | 387 | * Starts the worker check active interval. |
48487131 | 388 | */ |
b0a4db63 | 389 | private startCheckActive (): void { |
75d3401a | 390 | this.lastTaskTimestamp = performance.now() |
b0a4db63 JB |
391 | this.activeInterval = setInterval( |
392 | this.checkActive.bind(this), | |
75d3401a | 393 | (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2 |
984dc9c8 | 394 | ) |
75d3401a JB |
395 | } |
396 | ||
48487131 | 397 | /** |
b0a4db63 | 398 | * Stops the worker check active interval. |
48487131 | 399 | */ |
b0a4db63 | 400 | private stopCheckActive (): void { |
c3f498b5 JB |
401 | if (this.activeInterval != null) { |
402 | clearInterval(this.activeInterval) | |
403 | delete this.activeInterval | |
404 | } | |
48487131 JB |
405 | } |
406 | ||
407 | /** | |
408 | * Checks if the worker should be terminated, because its living too long. | |
409 | */ | |
b0a4db63 | 410 | private checkActive (): void { |
48487131 JB |
411 | if ( |
412 | performance.now() - this.lastTaskTimestamp > | |
413 | (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) | |
414 | ) { | |
21f710aa | 415 | this.sendToMainWorker({ kill: this.opts.killBehavior, workerId: this.id }) |
48487131 JB |
416 | } |
417 | } | |
418 | ||
729c563d S |
419 | /** |
420 | * Returns the main worker. | |
838898f1 S |
421 | * |
422 | * @returns Reference to the main worker. | |
729c563d | 423 | */ |
838898f1 | 424 | protected getMainWorker (): MainWorker { |
78cea37e | 425 | if (this.mainWorker == null) { |
e102732c | 426 | throw new Error('Main worker not set') |
838898f1 S |
427 | } |
428 | return this.mainWorker | |
429 | } | |
c97c7edb | 430 | |
729c563d | 431 | /** |
aa9eede8 | 432 | * Sends a message to main worker. |
729c563d | 433 | * |
38e795c1 | 434 | * @param message - The response message. |
729c563d | 435 | */ |
82f36766 JB |
436 | protected abstract sendToMainWorker ( |
437 | message: MessageValue<Response, Data> | |
438 | ): void | |
c97c7edb | 439 | |
90d7d101 JB |
440 | /** |
441 | * Sends the list of task function names to the main worker. | |
442 | */ | |
443 | protected sendTaskFunctionsListToMainWorker (): void { | |
444 | this.sendToMainWorker({ | |
445 | taskFunctions: this.listTaskFunctions(), | |
446 | workerId: this.id | |
447 | }) | |
448 | } | |
449 | ||
729c563d | 450 | /** |
8accb8d5 | 451 | * Handles an error and convert it to a string so it can be sent back to the main worker. |
729c563d | 452 | * |
38e795c1 | 453 | * @param e - The error raised by the worker. |
ab80dc46 | 454 | * @returns The error message. |
729c563d | 455 | */ |
c97c7edb | 456 | protected handleError (e: Error | string): string { |
985d0e79 | 457 | return e instanceof Error ? e.message : e |
c97c7edb S |
458 | } |
459 | ||
5c4d16da JB |
460 | /** |
461 | * Runs the given task. | |
462 | * | |
463 | * @param task - The task to execute. | |
464 | * @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found. | |
465 | */ | |
466 | protected run (task: Task<Data>): void { | |
467 | const fn = this.getTaskFunction(task.name) | |
468 | if (isAsyncFunction(fn)) { | |
469 | this.runInAsyncScope(this.runAsync.bind(this), this, fn, task) | |
470 | } else { | |
471 | this.runInAsyncScope(this.runSync.bind(this), this, fn, task) | |
472 | } | |
473 | } | |
474 | ||
729c563d | 475 | /** |
4dd93fcf | 476 | * Runs the given task function synchronously. |
729c563d | 477 | * |
5c4d16da JB |
478 | * @param fn - Task function that will be executed. |
479 | * @param task - Input data for the task function. | |
729c563d | 480 | */ |
70a4f5ea | 481 | protected runSync ( |
82ea6492 | 482 | fn: TaskSyncFunction<Data, Response>, |
5c4d16da | 483 | task: Task<Data> |
c97c7edb | 484 | ): void { |
310de0aa | 485 | const { name, taskId, data } = task |
c97c7edb | 486 | try { |
310de0aa JB |
487 | let taskPerformance = this.beginTaskPerformance(name) |
488 | const res = fn(data) | |
d715b7bc | 489 | taskPerformance = this.endTaskPerformance(taskPerformance) |
3fafb1b2 JB |
490 | this.sendToMainWorker({ |
491 | data: res, | |
d715b7bc | 492 | taskPerformance, |
f59e1027 | 493 | workerId: this.id, |
310de0aa | 494 | taskId |
3fafb1b2 | 495 | }) |
c97c7edb | 496 | } catch (e) { |
985d0e79 | 497 | const errorMessage = this.handleError(e as Error | string) |
91ee39ed | 498 | this.sendToMainWorker({ |
82f36766 | 499 | taskError: { |
310de0aa | 500 | name: name ?? DEFAULT_TASK_NAME, |
985d0e79 | 501 | message: errorMessage, |
310de0aa | 502 | data |
82f36766 | 503 | }, |
21f710aa | 504 | workerId: this.id, |
310de0aa | 505 | taskId |
91ee39ed | 506 | }) |
6e9d10db | 507 | } finally { |
c3f498b5 | 508 | this.updateLastTaskTimestamp() |
c97c7edb S |
509 | } |
510 | } | |
511 | ||
729c563d | 512 | /** |
4dd93fcf | 513 | * Runs the given task function asynchronously. |
729c563d | 514 | * |
5c4d16da JB |
515 | * @param fn - Task function that will be executed. |
516 | * @param task - Input data for the task function. | |
729c563d | 517 | */ |
c97c7edb | 518 | protected runAsync ( |
82ea6492 | 519 | fn: TaskAsyncFunction<Data, Response>, |
5c4d16da | 520 | task: Task<Data> |
c97c7edb | 521 | ): void { |
310de0aa JB |
522 | const { name, taskId, data } = task |
523 | let taskPerformance = this.beginTaskPerformance(name) | |
524 | fn(data) | |
8ebe6c30 | 525 | .then((res) => { |
d715b7bc | 526 | taskPerformance = this.endTaskPerformance(taskPerformance) |
3fafb1b2 JB |
527 | this.sendToMainWorker({ |
528 | data: res, | |
d715b7bc | 529 | taskPerformance, |
f59e1027 | 530 | workerId: this.id, |
310de0aa | 531 | taskId |
3fafb1b2 | 532 | }) |
c97c7edb S |
533 | return null |
534 | }) | |
8ebe6c30 | 535 | .catch((e) => { |
985d0e79 | 536 | const errorMessage = this.handleError(e as Error | string) |
91ee39ed | 537 | this.sendToMainWorker({ |
82f36766 | 538 | taskError: { |
310de0aa | 539 | name: name ?? DEFAULT_TASK_NAME, |
985d0e79 | 540 | message: errorMessage, |
310de0aa | 541 | data |
82f36766 | 542 | }, |
21f710aa | 543 | workerId: this.id, |
310de0aa | 544 | taskId |
91ee39ed | 545 | }) |
6e9d10db JB |
546 | }) |
547 | .finally(() => { | |
c3f498b5 | 548 | this.updateLastTaskTimestamp() |
c97c7edb | 549 | }) |
6e9d10db | 550 | .catch(EMPTY_FUNCTION) |
c97c7edb | 551 | } |
ec8fd331 | 552 | |
82888165 | 553 | /** |
5c4d16da | 554 | * Gets the task function with the given name. |
82888165 | 555 | * |
ff128cc9 | 556 | * @param name - Name of the task function that will be returned. |
5c4d16da JB |
557 | * @returns The task function. |
558 | * @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found. | |
82888165 | 559 | */ |
82ea6492 | 560 | private getTaskFunction (name?: string): TaskFunction<Data, Response> { |
ff128cc9 | 561 | name = name ?? DEFAULT_TASK_NAME |
ec8fd331 JB |
562 | const fn = this.taskFunctions.get(name) |
563 | if (fn == null) { | |
ace229a1 | 564 | throw new Error(`Task function '${name}' not found`) |
ec8fd331 JB |
565 | } |
566 | return fn | |
567 | } | |
62c15a68 | 568 | |
197b4aa5 | 569 | private beginTaskPerformance (name?: string): TaskPerformance { |
8a970421 | 570 | this.checkStatistics() |
62c15a68 | 571 | return { |
ff128cc9 | 572 | name: name ?? DEFAULT_TASK_NAME, |
1c6fe997 | 573 | timestamp: performance.now(), |
b6b32453 | 574 | ...(this.statistics.elu && { elu: performance.eventLoopUtilization() }) |
62c15a68 JB |
575 | } |
576 | } | |
577 | ||
d9d31201 JB |
578 | private endTaskPerformance ( |
579 | taskPerformance: TaskPerformance | |
580 | ): TaskPerformance { | |
8a970421 | 581 | this.checkStatistics() |
62c15a68 JB |
582 | return { |
583 | ...taskPerformance, | |
b6b32453 JB |
584 | ...(this.statistics.runTime && { |
585 | runTime: performance.now() - taskPerformance.timestamp | |
586 | }), | |
587 | ...(this.statistics.elu && { | |
62c15a68 | 588 | elu: performance.eventLoopUtilization(taskPerformance.elu) |
b6b32453 | 589 | }) |
62c15a68 JB |
590 | } |
591 | } | |
8a970421 JB |
592 | |
593 | private checkStatistics (): void { | |
594 | if (this.statistics == null) { | |
595 | throw new Error('Performance statistics computation requirements not set') | |
596 | } | |
597 | } | |
c3f498b5 JB |
598 | |
599 | private updateLastTaskTimestamp (): void { | |
29d8b961 | 600 | if (this.activeInterval != null) { |
c3f498b5 JB |
601 | this.lastTaskTimestamp = performance.now() |
602 | } | |
603 | } | |
c97c7edb | 604 | } |