feat: expose pool information
[poolifier.git] / src / pools / abstract-pool.ts
1 import crypto from 'node:crypto'
2 import type { MessageValue, PromiseResponseWrapper } from '../utility-types'
3 import {
4 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS,
5 EMPTY_FUNCTION,
6 isPlainObject,
7 median
8 } from '../utils'
9 import { KillBehaviors, isKillBehavior } from '../worker/worker-options'
10 import { CircularArray } from '../circular-array'
11 import { Queue } from '../queue'
12 import {
13 type IPool,
14 PoolEmitter,
15 PoolEvents,
16 type PoolInfo,
17 type PoolOptions,
18 type PoolType,
19 PoolTypes,
20 type TasksQueueOptions
21 } from './pool'
22 import type { IWorker, Task, TasksUsage, WorkerNode } from './worker'
23 import {
24 WorkerChoiceStrategies,
25 type WorkerChoiceStrategy,
26 type WorkerChoiceStrategyOptions
27 } from './selection-strategies/selection-strategies-types'
28 import { WorkerChoiceStrategyContext } from './selection-strategies/worker-choice-strategy-context'
29
30 /**
31 * Base class that implements some shared logic for all poolifier pools.
32 *
33 * @typeParam Worker - Type of worker which manages this pool.
34 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
35 * @typeParam Response - Type of execution response. This can only be serializable data.
36 */
37 export abstract class AbstractPool<
38 Worker extends IWorker,
39 Data = unknown,
40 Response = unknown
41 > implements IPool<Worker, Data, Response> {
42 /** @inheritDoc */
43 public readonly workerNodes: Array<WorkerNode<Worker, Data>> = []
44
45 /** @inheritDoc */
46 public readonly emitter?: PoolEmitter
47
48 /**
49 * The execution response promise map.
50 *
51 * - `key`: The message id of each submitted task.
52 * - `value`: An object that contains the worker, the execution response promise resolve and reject callbacks.
53 *
54 * When we receive a message from the worker, we get a map entry with the promise resolve/reject bound to the message id.
55 */
56 protected promiseResponseMap: Map<
57 string,
58 PromiseResponseWrapper<Worker, Response>
59 > = new Map<string, PromiseResponseWrapper<Worker, Response>>()
60
61 /**
62 * Worker choice strategy context referencing a worker choice algorithm implementation.
63 *
64 * Default to a round robin algorithm.
65 */
66 protected workerChoiceStrategyContext: WorkerChoiceStrategyContext<
67 Worker,
68 Data,
69 Response
70 >
71
72 /**
73 * Constructs a new poolifier pool.
74 *
75 * @param numberOfWorkers - Number of workers that this pool should manage.
76 * @param filePath - Path to the worker file.
77 * @param opts - Options for the pool.
78 */
79 public constructor (
80 public readonly numberOfWorkers: number,
81 public readonly filePath: string,
82 public readonly opts: PoolOptions<Worker>
83 ) {
84 if (!this.isMain()) {
85 throw new Error('Cannot start a pool from a worker!')
86 }
87 this.checkNumberOfWorkers(this.numberOfWorkers)
88 this.checkFilePath(this.filePath)
89 this.checkPoolOptions(this.opts)
90
91 this.chooseWorkerNode = this.chooseWorkerNode.bind(this)
92 this.executeTask = this.executeTask.bind(this)
93 this.enqueueTask = this.enqueueTask.bind(this)
94 this.checkAndEmitEvents = this.checkAndEmitEvents.bind(this)
95
96 this.setupHook()
97
98 for (let i = 1; i <= this.numberOfWorkers; i++) {
99 this.createAndSetupWorker()
100 }
101
102 if (this.opts.enableEvents === true) {
103 this.emitter = new PoolEmitter()
104 }
105 this.workerChoiceStrategyContext = new WorkerChoiceStrategyContext<
106 Worker,
107 Data,
108 Response
109 >(
110 this,
111 this.opts.workerChoiceStrategy,
112 this.opts.workerChoiceStrategyOptions
113 )
114 }
115
116 private checkFilePath (filePath: string): void {
117 if (
118 filePath == null ||
119 (typeof filePath === 'string' && filePath.trim().length === 0)
120 ) {
121 throw new Error('Please specify a file with a worker implementation')
122 }
123 }
124
125 private checkNumberOfWorkers (numberOfWorkers: number): void {
126 if (numberOfWorkers == null) {
127 throw new Error(
128 'Cannot instantiate a pool without specifying the number of workers'
129 )
130 } else if (!Number.isSafeInteger(numberOfWorkers)) {
131 throw new TypeError(
132 'Cannot instantiate a pool with a non safe integer number of workers'
133 )
134 } else if (numberOfWorkers < 0) {
135 throw new RangeError(
136 'Cannot instantiate a pool with a negative number of workers'
137 )
138 } else if (this.type === PoolTypes.fixed && numberOfWorkers === 0) {
139 throw new Error('Cannot instantiate a fixed pool with no worker')
140 }
141 }
142
143 private checkPoolOptions (opts: PoolOptions<Worker>): void {
144 if (isPlainObject(opts)) {
145 this.opts.workerChoiceStrategy =
146 opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
147 this.checkValidWorkerChoiceStrategy(this.opts.workerChoiceStrategy)
148 this.opts.workerChoiceStrategyOptions =
149 opts.workerChoiceStrategyOptions ??
150 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
151 this.checkValidWorkerChoiceStrategyOptions(
152 this.opts.workerChoiceStrategyOptions
153 )
154 this.opts.restartWorkerOnError = opts.restartWorkerOnError ?? true
155 this.opts.enableEvents = opts.enableEvents ?? true
156 this.opts.enableTasksQueue = opts.enableTasksQueue ?? false
157 if (this.opts.enableTasksQueue) {
158 this.checkValidTasksQueueOptions(
159 opts.tasksQueueOptions as TasksQueueOptions
160 )
161 this.opts.tasksQueueOptions = this.buildTasksQueueOptions(
162 opts.tasksQueueOptions as TasksQueueOptions
163 )
164 }
165 } else {
166 throw new TypeError('Invalid pool options: must be a plain object')
167 }
168 }
169
170 private checkValidWorkerChoiceStrategy (
171 workerChoiceStrategy: WorkerChoiceStrategy
172 ): void {
173 if (!Object.values(WorkerChoiceStrategies).includes(workerChoiceStrategy)) {
174 throw new Error(
175 `Invalid worker choice strategy '${workerChoiceStrategy}'`
176 )
177 }
178 }
179
180 private checkValidWorkerChoiceStrategyOptions (
181 workerChoiceStrategyOptions: WorkerChoiceStrategyOptions
182 ): void {
183 if (!isPlainObject(workerChoiceStrategyOptions)) {
184 throw new TypeError(
185 'Invalid worker choice strategy options: must be a plain object'
186 )
187 }
188 if (
189 workerChoiceStrategyOptions.weights != null &&
190 Object.keys(workerChoiceStrategyOptions.weights).length !== this.maxSize
191 ) {
192 throw new Error(
193 'Invalid worker choice strategy options: must have a weight for each worker node'
194 )
195 }
196 }
197
198 private checkValidTasksQueueOptions (
199 tasksQueueOptions: TasksQueueOptions
200 ): void {
201 if (tasksQueueOptions != null && !isPlainObject(tasksQueueOptions)) {
202 throw new TypeError('Invalid tasks queue options: must be a plain object')
203 }
204 if ((tasksQueueOptions?.concurrency as number) <= 0) {
205 throw new Error(
206 `Invalid worker tasks concurrency '${
207 tasksQueueOptions.concurrency as number
208 }'`
209 )
210 }
211 }
212
213 /** @inheritDoc */
214 public abstract get type (): PoolType
215
216 /** @inheritDoc */
217 public get info (): PoolInfo {
218 return {
219 type: this.type,
220 minSize: this.minSize,
221 maxSize: this.maxSize,
222 workerNodes: this.workerNodes.length,
223 idleWorkerNodes: this.workerNodes.reduce(
224 (accumulator, workerNode) =>
225 workerNode.tasksUsage.running === 0 ? accumulator + 1 : accumulator,
226 0
227 ),
228 busyWorkerNodes: this.workerNodes.reduce(
229 (accumulator, workerNode) =>
230 workerNode.tasksUsage.running > 0 ? accumulator + 1 : accumulator,
231 0
232 ),
233 runningTasks: this.workerNodes.reduce(
234 (accumulator, workerNode) =>
235 accumulator + workerNode.tasksUsage.running,
236 0
237 ),
238 queuedTasks: this.workerNodes.reduce(
239 (accumulator, workerNode) => accumulator + workerNode.tasksQueue.size,
240 0
241 ),
242 maxQueuedTasks: this.workerNodes.reduce(
243 (accumulator, workerNode) =>
244 accumulator + workerNode.tasksQueue.maxSize,
245 0
246 )
247 }
248 }
249
250 /**
251 * Pool minimum size.
252 */
253 protected abstract get minSize (): number
254
255 /**
256 * Pool maximum size.
257 */
258 protected abstract get maxSize (): number
259
260 /**
261 * Gets the given worker its worker node key.
262 *
263 * @param worker - The worker.
264 * @returns The worker node key if the worker is found in the pool worker nodes, `-1` otherwise.
265 */
266 private getWorkerNodeKey (worker: Worker): number {
267 return this.workerNodes.findIndex(
268 workerNode => workerNode.worker === worker
269 )
270 }
271
272 /** @inheritDoc */
273 public setWorkerChoiceStrategy (
274 workerChoiceStrategy: WorkerChoiceStrategy,
275 workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions
276 ): void {
277 this.checkValidWorkerChoiceStrategy(workerChoiceStrategy)
278 this.opts.workerChoiceStrategy = workerChoiceStrategy
279 for (const workerNode of this.workerNodes) {
280 this.setWorkerNodeTasksUsage(workerNode, {
281 run: 0,
282 running: 0,
283 runTime: 0,
284 runTimeHistory: new CircularArray(),
285 avgRunTime: 0,
286 medRunTime: 0,
287 waitTime: 0,
288 waitTimeHistory: new CircularArray(),
289 avgWaitTime: 0,
290 medWaitTime: 0,
291 error: 0
292 })
293 }
294 this.workerChoiceStrategyContext.setWorkerChoiceStrategy(
295 this.opts.workerChoiceStrategy
296 )
297 if (workerChoiceStrategyOptions != null) {
298 this.setWorkerChoiceStrategyOptions(workerChoiceStrategyOptions)
299 }
300 }
301
302 /** @inheritDoc */
303 public setWorkerChoiceStrategyOptions (
304 workerChoiceStrategyOptions: WorkerChoiceStrategyOptions
305 ): void {
306 this.checkValidWorkerChoiceStrategyOptions(workerChoiceStrategyOptions)
307 this.opts.workerChoiceStrategyOptions = workerChoiceStrategyOptions
308 this.workerChoiceStrategyContext.setOptions(
309 this.opts.workerChoiceStrategyOptions
310 )
311 }
312
313 /** @inheritDoc */
314 public enableTasksQueue (
315 enable: boolean,
316 tasksQueueOptions?: TasksQueueOptions
317 ): void {
318 if (this.opts.enableTasksQueue === true && !enable) {
319 this.flushTasksQueues()
320 }
321 this.opts.enableTasksQueue = enable
322 this.setTasksQueueOptions(tasksQueueOptions as TasksQueueOptions)
323 }
324
325 /** @inheritDoc */
326 public setTasksQueueOptions (tasksQueueOptions: TasksQueueOptions): void {
327 if (this.opts.enableTasksQueue === true) {
328 this.checkValidTasksQueueOptions(tasksQueueOptions)
329 this.opts.tasksQueueOptions =
330 this.buildTasksQueueOptions(tasksQueueOptions)
331 } else {
332 delete this.opts.tasksQueueOptions
333 }
334 }
335
336 private buildTasksQueueOptions (
337 tasksQueueOptions: TasksQueueOptions
338 ): TasksQueueOptions {
339 return {
340 concurrency: tasksQueueOptions?.concurrency ?? 1
341 }
342 }
343
344 /**
345 * Whether the pool is full or not.
346 *
347 * The pool filling boolean status.
348 */
349 protected abstract get full (): boolean
350
351 /**
352 * Whether the pool is busy or not.
353 *
354 * The pool busyness boolean status.
355 */
356 protected abstract get busy (): boolean
357
358 protected internalBusy (): boolean {
359 return (
360 this.workerNodes.findIndex(workerNode => {
361 return workerNode.tasksUsage.running === 0
362 }) === -1
363 )
364 }
365
366 /** @inheritDoc */
367 public async execute (data?: Data, name?: string): Promise<Response> {
368 const submissionTimestamp = performance.now()
369 const workerNodeKey = this.chooseWorkerNode()
370 const submittedTask: Task<Data> = {
371 name,
372 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
373 data: data ?? ({} as Data),
374 submissionTimestamp,
375 id: crypto.randomUUID()
376 }
377 const res = new Promise<Response>((resolve, reject) => {
378 this.promiseResponseMap.set(submittedTask.id as string, {
379 resolve,
380 reject,
381 worker: this.workerNodes[workerNodeKey].worker
382 })
383 })
384 if (
385 this.opts.enableTasksQueue === true &&
386 (this.busy ||
387 this.workerNodes[workerNodeKey].tasksUsage.running >=
388 ((this.opts.tasksQueueOptions as TasksQueueOptions)
389 .concurrency as number))
390 ) {
391 this.enqueueTask(workerNodeKey, submittedTask)
392 } else {
393 this.executeTask(workerNodeKey, submittedTask)
394 }
395 this.workerChoiceStrategyContext.update(workerNodeKey)
396 this.checkAndEmitEvents()
397 // eslint-disable-next-line @typescript-eslint/return-await
398 return res
399 }
400
401 /** @inheritDoc */
402 public async destroy (): Promise<void> {
403 await Promise.all(
404 this.workerNodes.map(async (workerNode, workerNodeKey) => {
405 this.flushTasksQueue(workerNodeKey)
406 await this.destroyWorker(workerNode.worker)
407 })
408 )
409 }
410
411 /**
412 * Shutdowns the given worker.
413 *
414 * @param worker - A worker within `workerNodes`.
415 */
416 protected abstract destroyWorker (worker: Worker): void | Promise<void>
417
418 /**
419 * Setup hook to execute code before worker node are created in the abstract constructor.
420 * Can be overridden
421 *
422 * @virtual
423 */
424 protected setupHook (): void {
425 // Intentionally empty
426 }
427
428 /**
429 * Should return whether the worker is the main worker or not.
430 */
431 protected abstract isMain (): boolean
432
433 /**
434 * Hook executed before the worker task execution.
435 * Can be overridden.
436 *
437 * @param workerNodeKey - The worker node key.
438 */
439 protected beforeTaskExecutionHook (workerNodeKey: number): void {
440 ++this.workerNodes[workerNodeKey].tasksUsage.running
441 }
442
443 /**
444 * Hook executed after the worker task execution.
445 * Can be overridden.
446 *
447 * @param worker - The worker.
448 * @param message - The received message.
449 */
450 protected afterTaskExecutionHook (
451 worker: Worker,
452 message: MessageValue<Response>
453 ): void {
454 const workerTasksUsage =
455 this.workerNodes[this.getWorkerNodeKey(worker)].tasksUsage
456 --workerTasksUsage.running
457 ++workerTasksUsage.run
458 if (message.error != null) {
459 ++workerTasksUsage.error
460 }
461 this.updateRunTimeTasksUsage(workerTasksUsage, message)
462 this.updateWaitTimeTasksUsage(workerTasksUsage, message)
463 }
464
465 private updateRunTimeTasksUsage (
466 workerTasksUsage: TasksUsage,
467 message: MessageValue<Response>
468 ): void {
469 if (this.workerChoiceStrategyContext.getRequiredStatistics().runTime) {
470 workerTasksUsage.runTime += message.runTime ?? 0
471 if (
472 this.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime &&
473 workerTasksUsage.run !== 0
474 ) {
475 workerTasksUsage.avgRunTime =
476 workerTasksUsage.runTime / workerTasksUsage.run
477 }
478 if (
479 this.workerChoiceStrategyContext.getRequiredStatistics().medRunTime &&
480 message.runTime != null
481 ) {
482 workerTasksUsage.runTimeHistory.push(message.runTime)
483 workerTasksUsage.medRunTime = median(workerTasksUsage.runTimeHistory)
484 }
485 }
486 }
487
488 private updateWaitTimeTasksUsage (
489 workerTasksUsage: TasksUsage,
490 message: MessageValue<Response>
491 ): void {
492 if (this.workerChoiceStrategyContext.getRequiredStatistics().waitTime) {
493 workerTasksUsage.waitTime += message.waitTime ?? 0
494 if (
495 this.workerChoiceStrategyContext.getRequiredStatistics().avgWaitTime &&
496 workerTasksUsage.run !== 0
497 ) {
498 workerTasksUsage.avgWaitTime =
499 workerTasksUsage.waitTime / workerTasksUsage.run
500 }
501 if (
502 this.workerChoiceStrategyContext.getRequiredStatistics().medWaitTime &&
503 message.waitTime != null
504 ) {
505 workerTasksUsage.waitTimeHistory.push(message.waitTime)
506 workerTasksUsage.medWaitTime = median(workerTasksUsage.waitTimeHistory)
507 }
508 }
509 }
510
511 /**
512 * Chooses a worker node for the next task.
513 *
514 * The default worker choice strategy uses a round robin algorithm to distribute the load.
515 *
516 * @returns The worker node key
517 */
518 protected chooseWorkerNode (): number {
519 let workerNodeKey: number
520 if (this.type === PoolTypes.dynamic && !this.full && this.internalBusy()) {
521 const workerCreated = this.createAndSetupWorker()
522 this.registerWorkerMessageListener(workerCreated, message => {
523 const currentWorkerNodeKey = this.getWorkerNodeKey(workerCreated)
524 if (
525 isKillBehavior(KillBehaviors.HARD, message.kill) ||
526 (message.kill != null &&
527 this.workerNodes[currentWorkerNodeKey].tasksUsage.running === 0)
528 ) {
529 // Kill message received from the worker: no new tasks are submitted to that worker for a while ( > maxInactiveTime)
530 this.flushTasksQueue(currentWorkerNodeKey)
531 void (this.destroyWorker(workerCreated) as Promise<void>)
532 }
533 })
534 workerNodeKey = this.getWorkerNodeKey(workerCreated)
535 } else {
536 workerNodeKey = this.workerChoiceStrategyContext.execute()
537 }
538 return workerNodeKey
539 }
540
541 /**
542 * Sends a message to the given worker.
543 *
544 * @param worker - The worker which should receive the message.
545 * @param message - The message.
546 */
547 protected abstract sendToWorker (
548 worker: Worker,
549 message: MessageValue<Data>
550 ): void
551
552 /**
553 * Registers a listener callback on the given worker.
554 *
555 * @param worker - The worker which should register a listener.
556 * @param listener - The message listener callback.
557 */
558 protected abstract registerWorkerMessageListener<
559 Message extends Data | Response
560 >(worker: Worker, listener: (message: MessageValue<Message>) => void): void
561
562 /**
563 * Returns a newly created worker.
564 */
565 protected abstract createWorker (): Worker
566
567 /**
568 * Function that can be hooked up when a worker has been newly created and moved to the pool worker nodes.
569 *
570 * Can be used to update the `maxListeners` or binding the `main-worker`\<-\>`worker` connection if not bind by default.
571 *
572 * @param worker - The newly created worker.
573 */
574 protected abstract afterWorkerSetup (worker: Worker): void
575
576 /**
577 * Creates a new worker and sets it up completely in the pool worker nodes.
578 *
579 * @returns New, completely set up worker.
580 */
581 protected createAndSetupWorker (): Worker {
582 const worker = this.createWorker()
583
584 worker.on('message', this.opts.messageHandler ?? EMPTY_FUNCTION)
585 worker.on('error', this.opts.errorHandler ?? EMPTY_FUNCTION)
586 worker.on('error', error => {
587 if (this.emitter != null) {
588 this.emitter.emit(PoolEvents.error, error)
589 }
590 })
591 if (this.opts.restartWorkerOnError === true) {
592 worker.on('error', () => {
593 this.createAndSetupWorker()
594 })
595 }
596 worker.on('online', this.opts.onlineHandler ?? EMPTY_FUNCTION)
597 worker.on('exit', this.opts.exitHandler ?? EMPTY_FUNCTION)
598 worker.once('exit', () => {
599 this.removeWorkerNode(worker)
600 })
601
602 this.pushWorkerNode(worker)
603
604 this.afterWorkerSetup(worker)
605
606 return worker
607 }
608
609 /**
610 * This function is the listener registered for each worker message.
611 *
612 * @returns The listener function to execute when a message is received from a worker.
613 */
614 protected workerListener (): (message: MessageValue<Response>) => void {
615 return message => {
616 if (message.id != null) {
617 // Task execution response received
618 const promiseResponse = this.promiseResponseMap.get(message.id)
619 if (promiseResponse != null) {
620 if (message.error != null) {
621 promiseResponse.reject(message.error)
622 if (this.emitter != null) {
623 this.emitter.emit(PoolEvents.taskError, {
624 error: message.error,
625 errorData: message.errorData
626 })
627 }
628 } else {
629 promiseResponse.resolve(message.data as Response)
630 }
631 this.afterTaskExecutionHook(promiseResponse.worker, message)
632 this.promiseResponseMap.delete(message.id)
633 const workerNodeKey = this.getWorkerNodeKey(promiseResponse.worker)
634 if (
635 this.opts.enableTasksQueue === true &&
636 this.tasksQueueSize(workerNodeKey) > 0
637 ) {
638 this.executeTask(
639 workerNodeKey,
640 this.dequeueTask(workerNodeKey) as Task<Data>
641 )
642 }
643 }
644 }
645 }
646 }
647
648 private checkAndEmitEvents (): void {
649 if (this.emitter != null) {
650 if (this.busy) {
651 this.emitter?.emit(PoolEvents.busy, this.info)
652 }
653 if (this.type === PoolTypes.dynamic && this.full) {
654 this.emitter?.emit(PoolEvents.full, this.info)
655 }
656 }
657 }
658
659 /**
660 * Sets the given worker node its tasks usage in the pool.
661 *
662 * @param workerNode - The worker node.
663 * @param tasksUsage - The worker node tasks usage.
664 */
665 private setWorkerNodeTasksUsage (
666 workerNode: WorkerNode<Worker, Data>,
667 tasksUsage: TasksUsage
668 ): void {
669 workerNode.tasksUsage = tasksUsage
670 }
671
672 /**
673 * Pushes the given worker in the pool worker nodes.
674 *
675 * @param worker - The worker.
676 * @returns The worker nodes length.
677 */
678 private pushWorkerNode (worker: Worker): number {
679 return this.workerNodes.push({
680 worker,
681 tasksUsage: {
682 run: 0,
683 running: 0,
684 runTime: 0,
685 runTimeHistory: new CircularArray(),
686 avgRunTime: 0,
687 medRunTime: 0,
688 waitTime: 0,
689 waitTimeHistory: new CircularArray(),
690 avgWaitTime: 0,
691 medWaitTime: 0,
692 error: 0
693 },
694 tasksQueue: new Queue<Task<Data>>()
695 })
696 }
697
698 /**
699 * Sets the given worker in the pool worker nodes.
700 *
701 * @param workerNodeKey - The worker node key.
702 * @param worker - The worker.
703 * @param tasksUsage - The worker tasks usage.
704 * @param tasksQueue - The worker task queue.
705 */
706 private setWorkerNode (
707 workerNodeKey: number,
708 worker: Worker,
709 tasksUsage: TasksUsage,
710 tasksQueue: Queue<Task<Data>>
711 ): void {
712 this.workerNodes[workerNodeKey] = {
713 worker,
714 tasksUsage,
715 tasksQueue
716 }
717 }
718
719 /**
720 * Removes the given worker from the pool worker nodes.
721 *
722 * @param worker - The worker.
723 */
724 private removeWorkerNode (worker: Worker): void {
725 const workerNodeKey = this.getWorkerNodeKey(worker)
726 if (workerNodeKey !== -1) {
727 this.workerNodes.splice(workerNodeKey, 1)
728 this.workerChoiceStrategyContext.remove(workerNodeKey)
729 }
730 }
731
732 private executeTask (workerNodeKey: number, task: Task<Data>): void {
733 this.beforeTaskExecutionHook(workerNodeKey)
734 this.sendToWorker(this.workerNodes[workerNodeKey].worker, task)
735 }
736
737 private enqueueTask (workerNodeKey: number, task: Task<Data>): number {
738 return this.workerNodes[workerNodeKey].tasksQueue.enqueue(task)
739 }
740
741 private dequeueTask (workerNodeKey: number): Task<Data> | undefined {
742 return this.workerNodes[workerNodeKey].tasksQueue.dequeue()
743 }
744
745 private tasksQueueSize (workerNodeKey: number): number {
746 return this.workerNodes[workerNodeKey].tasksQueue.size
747 }
748
749 private flushTasksQueue (workerNodeKey: number): void {
750 if (this.tasksQueueSize(workerNodeKey) > 0) {
751 for (let i = 0; i < this.tasksQueueSize(workerNodeKey); i++) {
752 this.executeTask(
753 workerNodeKey,
754 this.dequeueTask(workerNodeKey) as Task<Data>
755 )
756 }
757 }
758 }
759
760 private flushTasksQueues (): void {
761 for (const [workerNodeKey] of this.workerNodes.entries()) {
762 this.flushTasksQueue(workerNodeKey)
763 }
764 }
765 }