feat: internal benchmark with worker tasks queue enabled
[poolifier.git] / src / pools / abstract-pool.ts
CommitLineData
fc3e6586 1import crypto from 'node:crypto'
2740a743 2import type { MessageValue, PromiseResponseWrapper } from '../utility-types'
bbeadd16
JB
3import {
4 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS,
5 EMPTY_FUNCTION,
6 median
7} from '../utils'
34a0cfab 8import { KillBehaviors, isKillBehavior } from '../worker/worker-options'
aee46736 9import { PoolEvents, type PoolOptions } from './pool'
b4904890 10import { PoolEmitter } from './pool'
f06e48d8 11import type { IPoolInternal } from './pool-internal'
b4904890 12import { PoolType } from './pool-internal'
f06e48d8 13import type { IWorker, Task, TasksUsage, WorkerNode } from './worker'
a35560ba
S
14import {
15 WorkerChoiceStrategies,
63220255 16 type WorkerChoiceStrategy
bdaf31cd
JB
17} from './selection-strategies/selection-strategies-types'
18import { WorkerChoiceStrategyContext } from './selection-strategies/worker-choice-strategy-context'
78099a15 19import { CircularArray } from '../circular-array'
c97c7edb 20
729c563d 21/**
ea7a90d3 22 * Base class that implements some shared logic for all poolifier pools.
729c563d 23 *
38e795c1
JB
24 * @typeParam Worker - Type of worker which manages this pool.
25 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
26 * @typeParam Response - Type of response of execution. This can only be serializable data.
729c563d 27 */
c97c7edb 28export abstract class AbstractPool<
f06e48d8 29 Worker extends IWorker,
d3c8a1a8
S
30 Data = unknown,
31 Response = unknown
9b2fdd9f 32> implements IPoolInternal<Worker, Data, Response> {
afc003b2 33 /** @inheritDoc */
f06e48d8 34 public readonly workerNodes: Array<WorkerNode<Worker, Data>> = []
4a6952ff 35
afc003b2 36 /** @inheritDoc */
7c0ba920
JB
37 public readonly emitter?: PoolEmitter
38
be0676b3 39 /**
a3445496 40 * The execution response promise map.
be0676b3 41 *
2740a743 42 * - `key`: The message id of each submitted task.
a3445496 43 * - `value`: An object that contains the worker, the execution response promise resolve and reject callbacks.
be0676b3 44 *
a3445496 45 * When we receive a message from the worker, we get a map entry with the promise resolve/reject bound to the message id.
be0676b3 46 */
c923ce56
JB
47 protected promiseResponseMap: Map<
48 string,
49 PromiseResponseWrapper<Worker, Response>
50 > = new Map<string, PromiseResponseWrapper<Worker, Response>>()
c97c7edb 51
a35560ba 52 /**
51fe3d3c 53 * Worker choice strategy context referencing a worker choice algorithm implementation.
a35560ba 54 *
51fe3d3c 55 * Default to a round robin algorithm.
a35560ba
S
56 */
57 protected workerChoiceStrategyContext: WorkerChoiceStrategyContext<
78cea37e
JB
58 Worker,
59 Data,
60 Response
a35560ba
S
61 >
62
729c563d
S
63 /**
64 * Constructs a new poolifier pool.
65 *
38e795c1
JB
66 * @param numberOfWorkers - Number of workers that this pool should manage.
67 * @param filePath - Path to the worker-file.
68 * @param opts - Options for the pool.
729c563d 69 */
c97c7edb 70 public constructor (
5c5a1fb7 71 public readonly numberOfWorkers: number,
c97c7edb 72 public readonly filePath: string,
1927ee67 73 public readonly opts: PoolOptions<Worker>
c97c7edb 74 ) {
78cea37e 75 if (!this.isMain()) {
c97c7edb
S
76 throw new Error('Cannot start a pool from a worker!')
77 }
8d3782fa 78 this.checkNumberOfWorkers(this.numberOfWorkers)
c510fea7 79 this.checkFilePath(this.filePath)
7c0ba920 80 this.checkPoolOptions(this.opts)
1086026a 81
adc3c320 82 this.chooseWorkerNode.bind(this)
2e81254d
JB
83 this.executeTask.bind(this)
84 this.enqueueTask.bind(this)
ff733df7 85 this.checkAndEmitEvents.bind(this)
1086026a 86
c97c7edb
S
87 this.setupHook()
88
5c5a1fb7 89 for (let i = 1; i <= this.numberOfWorkers; i++) {
280c2a77 90 this.createAndSetupWorker()
c97c7edb
S
91 }
92
6bd72cd0 93 if (this.opts.enableEvents === true) {
7c0ba920
JB
94 this.emitter = new PoolEmitter()
95 }
d59df138
JB
96 this.workerChoiceStrategyContext = new WorkerChoiceStrategyContext<
97 Worker,
98 Data,
99 Response
da309861
JB
100 >(
101 this,
102 this.opts.workerChoiceStrategy,
103 this.opts.workerChoiceStrategyOptions
104 )
c97c7edb
S
105 }
106
a35560ba 107 private checkFilePath (filePath: string): void {
ffcbbad8
JB
108 if (
109 filePath == null ||
110 (typeof filePath === 'string' && filePath.trim().length === 0)
111 ) {
c510fea7
APA
112 throw new Error('Please specify a file with a worker implementation')
113 }
114 }
115
8d3782fa
JB
116 private checkNumberOfWorkers (numberOfWorkers: number): void {
117 if (numberOfWorkers == null) {
118 throw new Error(
119 'Cannot instantiate a pool without specifying the number of workers'
120 )
78cea37e 121 } else if (!Number.isSafeInteger(numberOfWorkers)) {
473c717a 122 throw new TypeError(
8d3782fa
JB
123 'Cannot instantiate a pool with a non integer number of workers'
124 )
125 } else if (numberOfWorkers < 0) {
473c717a 126 throw new RangeError(
8d3782fa
JB
127 'Cannot instantiate a pool with a negative number of workers'
128 )
7c0ba920 129 } else if (this.type === PoolType.FIXED && numberOfWorkers === 0) {
8d3782fa
JB
130 throw new Error('Cannot instantiate a fixed pool with no worker')
131 }
132 }
133
7c0ba920 134 private checkPoolOptions (opts: PoolOptions<Worker>): void {
e843b904
JB
135 this.opts.workerChoiceStrategy =
136 opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
aee46736 137 this.checkValidWorkerChoiceStrategy(this.opts.workerChoiceStrategy)
da309861 138 this.opts.workerChoiceStrategyOptions =
bbeadd16 139 opts.workerChoiceStrategyOptions ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
aee46736 140 this.opts.enableEvents = opts.enableEvents ?? true
ff733df7 141 this.opts.enableTasksQueue = opts.enableTasksQueue ?? false
aee46736
JB
142 }
143
144 private checkValidWorkerChoiceStrategy (
145 workerChoiceStrategy: WorkerChoiceStrategy
146 ): void {
147 if (!Object.values(WorkerChoiceStrategies).includes(workerChoiceStrategy)) {
b529c323 148 throw new Error(
aee46736 149 `Invalid worker choice strategy '${workerChoiceStrategy}'`
b529c323
JB
150 )
151 }
7c0ba920
JB
152 }
153
afc003b2 154 /** @inheritDoc */
7c0ba920
JB
155 public abstract get type (): PoolType
156
c2ade475 157 /**
ff733df7 158 * Number of tasks running in the pool.
c2ade475
JB
159 */
160 private get numberOfRunningTasks (): number {
ff733df7
JB
161 return this.workerNodes.reduce(
162 (accumulator, workerNode) => accumulator + workerNode.tasksUsage.running,
163 0
164 )
165 }
166
167 /**
168 * Number of tasks queued in the pool.
169 */
170 private get numberOfQueuedTasks (): number {
171 if (this.opts.enableTasksQueue === false) {
172 return 0
173 }
174 return this.workerNodes.reduce(
175 (accumulator, workerNode) => accumulator + workerNode.tasksQueue.length,
176 0
177 )
a35560ba
S
178 }
179
ffcbbad8 180 /**
f06e48d8 181 * Gets the given worker its worker node key.
ffcbbad8
JB
182 *
183 * @param worker - The worker.
f06e48d8 184 * @returns The worker node key if the worker is found in the pool worker nodes, `-1` otherwise.
ffcbbad8 185 */
f06e48d8
JB
186 private getWorkerNodeKey (worker: Worker): number {
187 return this.workerNodes.findIndex(
188 workerNode => workerNode.worker === worker
189 )
bf9549ae
JB
190 }
191
afc003b2 192 /** @inheritDoc */
a35560ba
S
193 public setWorkerChoiceStrategy (
194 workerChoiceStrategy: WorkerChoiceStrategy
195 ): void {
aee46736 196 this.checkValidWorkerChoiceStrategy(workerChoiceStrategy)
b98ec2e6 197 this.opts.workerChoiceStrategy = workerChoiceStrategy
0ebe2a9f
JB
198 for (const workerNode of this.workerNodes) {
199 this.setWorkerNodeTasksUsage(workerNode, {
200 run: 0,
201 running: 0,
202 runTime: 0,
203 runTimeHistory: new CircularArray(),
204 avgRunTime: 0,
205 medRunTime: 0,
206 error: 0
207 })
ea7a90d3 208 }
a35560ba
S
209 this.workerChoiceStrategyContext.setWorkerChoiceStrategy(
210 workerChoiceStrategy
211 )
212 }
213
afc003b2 214 /** @inheritDoc */
c2ade475
JB
215 public abstract get full (): boolean
216
afc003b2 217 /** @inheritDoc */
7c0ba920
JB
218 public abstract get busy (): boolean
219
c2ade475 220 protected internalBusy (): boolean {
24990079 221 return this.findFreeWorkerNodeKey() === -1
7c0ba920
JB
222 }
223
afc003b2 224 /** @inheritDoc */
f06e48d8
JB
225 public findFreeWorkerNodeKey (): number {
226 return this.workerNodes.findIndex(workerNode => {
227 return workerNode.tasksUsage?.running === 0
c923ce56 228 })
7c0ba920
JB
229 }
230
afc003b2 231 /** @inheritDoc */
78cea37e 232 public async execute (data: Data): Promise<Response> {
adc3c320
JB
233 const [workerNodeKey, workerNode] = this.chooseWorkerNode()
234 const submittedTask: Task<Data> = {
e5a5c0fc
JB
235 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
236 data: data ?? ({} as Data),
adc3c320
JB
237 id: crypto.randomUUID()
238 }
2e81254d
JB
239 const res = new Promise<Response>((resolve, reject) => {
240 this.promiseResponseMap.set(submittedTask.id, {
241 resolve,
242 reject,
243 worker: workerNode.worker
244 })
245 })
ff733df7
JB
246 if (
247 this.opts.enableTasksQueue === true &&
26a929d7 248 (this.busy || this.workerNodes[workerNodeKey].tasksUsage.running > 0)
ff733df7 249 ) {
26a929d7
JB
250 this.enqueueTask(workerNodeKey, submittedTask)
251 } else {
2e81254d 252 this.executeTask(workerNodeKey, submittedTask)
adc3c320 253 }
ff733df7 254 this.checkAndEmitEvents()
78cea37e 255 // eslint-disable-next-line @typescript-eslint/return-await
280c2a77
S
256 return res
257 }
c97c7edb 258
afc003b2 259 /** @inheritDoc */
c97c7edb 260 public async destroy (): Promise<void> {
1fbcaa7c 261 await Promise.all(
875a7c37
JB
262 this.workerNodes.map(async (workerNode, workerNodeKey) => {
263 this.flushTasksQueue(workerNodeKey)
f06e48d8 264 await this.destroyWorker(workerNode.worker)
1fbcaa7c
JB
265 })
266 )
c97c7edb
S
267 }
268
4a6952ff 269 /**
f06e48d8 270 * Shutdowns the given worker.
4a6952ff 271 *
f06e48d8 272 * @param worker - A worker within `workerNodes`.
4a6952ff
JB
273 */
274 protected abstract destroyWorker (worker: Worker): void | Promise<void>
c97c7edb 275
729c563d 276 /**
2e81254d 277 * Setup hook to execute code before worker node are created in the abstract constructor.
d99ba5a8 278 * Can be overridden
afc003b2
JB
279 *
280 * @virtual
729c563d 281 */
280c2a77 282 protected setupHook (): void {
d99ba5a8 283 // Intentionally empty
280c2a77 284 }
c97c7edb 285
729c563d 286 /**
280c2a77
S
287 * Should return whether the worker is the main worker or not.
288 */
289 protected abstract isMain (): boolean
290
291 /**
2e81254d 292 * Hook executed before the worker task execution.
bf9549ae 293 * Can be overridden.
729c563d 294 *
f06e48d8 295 * @param workerNodeKey - The worker node key.
729c563d 296 */
2e81254d 297 protected beforeTaskExecutionHook (workerNodeKey: number): void {
f06e48d8 298 ++this.workerNodes[workerNodeKey].tasksUsage.running
c97c7edb
S
299 }
300
c01733f1 301 /**
2e81254d 302 * Hook executed after the worker task execution.
bf9549ae 303 * Can be overridden.
c01733f1 304 *
c923ce56 305 * @param worker - The worker.
38e795c1 306 * @param message - The received message.
c01733f1 307 */
2e81254d 308 protected afterTaskExecutionHook (
c923ce56 309 worker: Worker,
2740a743 310 message: MessageValue<Response>
bf9549ae 311 ): void {
c923ce56 312 const workerTasksUsage = this.getWorkerTasksUsage(worker) as TasksUsage
3032893a
JB
313 --workerTasksUsage.running
314 ++workerTasksUsage.run
2740a743
JB
315 if (message.error != null) {
316 ++workerTasksUsage.error
317 }
97a2abc3 318 if (this.workerChoiceStrategyContext.getRequiredStatistics().runTime) {
aee46736 319 workerTasksUsage.runTime += message.runTime ?? 0
c6bd2650
JB
320 if (
321 this.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime &&
322 workerTasksUsage.run !== 0
323 ) {
3032893a
JB
324 workerTasksUsage.avgRunTime =
325 workerTasksUsage.runTime / workerTasksUsage.run
326 }
78099a15
JB
327 if (this.workerChoiceStrategyContext.getRequiredStatistics().medRunTime) {
328 workerTasksUsage.runTimeHistory.push(message.runTime ?? 0)
329 workerTasksUsage.medRunTime = median(workerTasksUsage.runTimeHistory)
330 }
3032893a 331 }
c01733f1 332 }
333
280c2a77 334 /**
f06e48d8 335 * Chooses a worker node for the next task.
280c2a77 336 *
51fe3d3c 337 * The default uses a round robin algorithm to distribute the load.
280c2a77 338 *
adc3c320 339 * @returns [worker node key, worker node].
280c2a77 340 */
adc3c320 341 protected chooseWorkerNode (): [number, WorkerNode<Worker, Data>] {
f06e48d8 342 let workerNodeKey: number
0527b6db 343 if (this.type === PoolType.DYNAMIC && !this.full && this.internalBusy()) {
adc3c320
JB
344 const workerCreated = this.createAndSetupWorker()
345 this.registerWorkerMessageListener(workerCreated, message => {
17393ac8
JB
346 if (
347 isKillBehavior(KillBehaviors.HARD, message.kill) ||
d2097c13 348 (message.kill != null &&
adc3c320 349 this.getWorkerTasksUsage(workerCreated)?.running === 0)
17393ac8 350 ) {
ff733df7
JB
351 // Kill message received from the worker: no new tasks are submitted to that worker for a while ( > maxInactiveTime)
352 this.flushTasksQueueByWorker(workerCreated)
adc3c320 353 void this.destroyWorker(workerCreated)
17393ac8
JB
354 }
355 })
adc3c320 356 workerNodeKey = this.getWorkerNodeKey(workerCreated)
17393ac8 357 } else {
f06e48d8 358 workerNodeKey = this.workerChoiceStrategyContext.execute()
17393ac8 359 }
adc3c320 360 return [workerNodeKey, this.workerNodes[workerNodeKey]]
c97c7edb
S
361 }
362
280c2a77 363 /**
675bb809 364 * Sends a message to the given worker.
280c2a77 365 *
38e795c1
JB
366 * @param worker - The worker which should receive the message.
367 * @param message - The message.
280c2a77
S
368 */
369 protected abstract sendToWorker (
370 worker: Worker,
371 message: MessageValue<Data>
372 ): void
373
4a6952ff 374 /**
f06e48d8 375 * Registers a listener callback on the given worker.
4a6952ff 376 *
38e795c1
JB
377 * @param worker - The worker which should register a listener.
378 * @param listener - The message listener callback.
4a6952ff
JB
379 */
380 protected abstract registerWorkerMessageListener<
4f7fa42a 381 Message extends Data | Response
78cea37e 382 >(worker: Worker, listener: (message: MessageValue<Message>) => void): void
c97c7edb 383
729c563d
S
384 /**
385 * Returns a newly created worker.
386 */
280c2a77 387 protected abstract createWorker (): Worker
c97c7edb 388
729c563d 389 /**
f06e48d8 390 * Function that can be hooked up when a worker has been newly created and moved to the pool worker nodes.
729c563d 391 *
38e795c1 392 * Can be used to update the `maxListeners` or binding the `main-worker`\<-\>`worker` connection if not bind by default.
729c563d 393 *
38e795c1 394 * @param worker - The newly created worker.
729c563d 395 */
280c2a77 396 protected abstract afterWorkerSetup (worker: Worker): void
c97c7edb 397
4a6952ff 398 /**
f06e48d8 399 * Creates a new worker and sets it up completely in the pool worker nodes.
4a6952ff
JB
400 *
401 * @returns New, completely set up worker.
402 */
403 protected createAndSetupWorker (): Worker {
bdacc2d2 404 const worker = this.createWorker()
280c2a77 405
35cf1c03 406 worker.on('message', this.opts.messageHandler ?? EMPTY_FUNCTION)
a35560ba
S
407 worker.on('error', this.opts.errorHandler ?? EMPTY_FUNCTION)
408 worker.on('online', this.opts.onlineHandler ?? EMPTY_FUNCTION)
409 worker.on('exit', this.opts.exitHandler ?? EMPTY_FUNCTION)
a974afa6 410 worker.once('exit', () => {
f06e48d8 411 this.removeWorkerNode(worker)
a974afa6 412 })
280c2a77 413
f06e48d8 414 this.pushWorkerNode(worker)
280c2a77
S
415
416 this.afterWorkerSetup(worker)
417
c97c7edb
S
418 return worker
419 }
be0676b3
APA
420
421 /**
ff733df7 422 * This function is the listener registered for each worker message.
be0676b3 423 *
bdacc2d2 424 * @returns The listener function to execute when a message is received from a worker.
be0676b3
APA
425 */
426 protected workerListener (): (message: MessageValue<Response>) => void {
4a6952ff 427 return message => {
b1989cfd 428 if (message.id != null) {
a3445496 429 // Task execution response received
2740a743 430 const promiseResponse = this.promiseResponseMap.get(message.id)
b1989cfd 431 if (promiseResponse != null) {
78cea37e 432 if (message.error != null) {
2740a743 433 promiseResponse.reject(message.error)
a05c10de 434 } else {
2740a743 435 promiseResponse.resolve(message.data as Response)
a05c10de 436 }
2e81254d 437 this.afterTaskExecutionHook(promiseResponse.worker, message)
2740a743 438 this.promiseResponseMap.delete(message.id)
ff733df7
JB
439 const workerNodeKey = this.getWorkerNodeKey(promiseResponse.worker)
440 if (
441 this.opts.enableTasksQueue === true &&
416fd65c 442 this.tasksQueueSize(workerNodeKey) > 0
ff733df7 443 ) {
2e81254d
JB
444 this.executeTask(
445 workerNodeKey,
ff733df7
JB
446 this.dequeueTask(workerNodeKey) as Task<Data>
447 )
448 }
be0676b3
APA
449 }
450 }
451 }
be0676b3 452 }
7c0ba920 453
ff733df7
JB
454 private checkAndEmitEvents (): void {
455 if (this.opts.enableEvents === true) {
456 if (this.busy) {
457 this.emitter?.emit(PoolEvents.busy)
458 }
459 if (this.type === PoolType.DYNAMIC && this.full) {
460 this.emitter?.emit(PoolEvents.full)
461 }
164d950a
JB
462 }
463 }
464
0ebe2a9f
JB
465 /**
466 * Sets the given worker node its tasks usage in the pool.
467 *
468 * @param workerNode - The worker node.
469 * @param tasksUsage - The worker node tasks usage.
470 */
471 private setWorkerNodeTasksUsage (
472 workerNode: WorkerNode<Worker, Data>,
473 tasksUsage: TasksUsage
474 ): void {
475 workerNode.tasksUsage = tasksUsage
476 }
477
c923ce56 478 /**
f06e48d8 479 * Gets the given worker its tasks usage in the pool.
c923ce56
JB
480 *
481 * @param worker - The worker.
482 * @returns The worker tasks usage.
483 */
484 private getWorkerTasksUsage (worker: Worker): TasksUsage | undefined {
f06e48d8
JB
485 const workerNodeKey = this.getWorkerNodeKey(worker)
486 if (workerNodeKey !== -1) {
487 return this.workerNodes[workerNodeKey].tasksUsage
ffcbbad8 488 }
f06e48d8 489 throw new Error('Worker could not be found in the pool worker nodes')
a05c10de
JB
490 }
491
492 /**
f06e48d8 493 * Pushes the given worker in the pool worker nodes.
ea7a90d3 494 *
38e795c1 495 * @param worker - The worker.
f06e48d8 496 * @returns The worker nodes length.
ea7a90d3 497 */
f06e48d8
JB
498 private pushWorkerNode (worker: Worker): number {
499 return this.workerNodes.push({
ffcbbad8 500 worker,
f06e48d8
JB
501 tasksUsage: {
502 run: 0,
503 running: 0,
504 runTime: 0,
505 runTimeHistory: new CircularArray(),
506 avgRunTime: 0,
507 medRunTime: 0,
508 error: 0
509 },
510 tasksQueue: []
ea7a90d3
JB
511 })
512 }
c923ce56
JB
513
514 /**
f06e48d8 515 * Sets the given worker in the pool worker nodes.
c923ce56 516 *
f06e48d8 517 * @param workerNodeKey - The worker node key.
c923ce56
JB
518 * @param worker - The worker.
519 * @param tasksUsage - The worker tasks usage.
f06e48d8 520 * @param tasksQueue - The worker task queue.
c923ce56 521 */
f06e48d8
JB
522 private setWorkerNode (
523 workerNodeKey: number,
c923ce56 524 worker: Worker,
f06e48d8
JB
525 tasksUsage: TasksUsage,
526 tasksQueue: Array<Task<Data>>
c923ce56 527 ): void {
f06e48d8 528 this.workerNodes[workerNodeKey] = {
c923ce56 529 worker,
f06e48d8
JB
530 tasksUsage,
531 tasksQueue
c923ce56
JB
532 }
533 }
51fe3d3c
JB
534
535 /**
f06e48d8 536 * Removes the given worker from the pool worker nodes.
51fe3d3c 537 *
f06e48d8 538 * @param worker - The worker.
51fe3d3c 539 */
416fd65c 540 private removeWorkerNode (worker: Worker): void {
f06e48d8
JB
541 const workerNodeKey = this.getWorkerNodeKey(worker)
542 this.workerNodes.splice(workerNodeKey, 1)
543 this.workerChoiceStrategyContext.remove(workerNodeKey)
51fe3d3c 544 }
adc3c320 545
2e81254d
JB
546 private executeTask (workerNodeKey: number, task: Task<Data>): void {
547 this.beforeTaskExecutionHook(workerNodeKey)
548 this.sendToWorker(this.workerNodes[workerNodeKey].worker, task)
549 }
550
f9f00b5f
JB
551 private enqueueTask (workerNodeKey: number, task: Task<Data>): number {
552 return this.workerNodes[workerNodeKey].tasksQueue.push(task)
adc3c320
JB
553 }
554
416fd65c 555 private dequeueTask (workerNodeKey: number): Task<Data> | undefined {
adc3c320
JB
556 return this.workerNodes[workerNodeKey].tasksQueue.shift()
557 }
558
416fd65c 559 private tasksQueueSize (workerNodeKey: number): number {
adc3c320
JB
560 return this.workerNodes[workerNodeKey].tasksQueue.length
561 }
ff733df7 562
416fd65c
JB
563 private flushTasksQueue (workerNodeKey: number): void {
564 if (this.tasksQueueSize(workerNodeKey) > 0) {
ff733df7 565 for (const task of this.workerNodes[workerNodeKey].tasksQueue) {
2e81254d 566 this.executeTask(workerNodeKey, task)
ff733df7 567 }
ff733df7
JB
568 }
569 }
570
416fd65c 571 private flushTasksQueueByWorker (worker: Worker): void {
ff733df7
JB
572 const workerNodeKey = this.getWorkerNodeKey(worker)
573 this.flushTasksQueue(workerNodeKey)
574 }
c97c7edb 575}