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