this.executeTask = this.executeTask.bind(this)
this.enqueueTask = this.enqueueTask.bind(this)
this.dequeueTask = this.dequeueTask.bind(this)
- this.checkAndEmitEvents = this.checkAndEmitEvents.bind(this)
+ this.checkAndEmitTaskExecutionEvents =
+ this.checkAndEmitTaskExecutionEvents.bind(this)
+ this.checkAndEmitTaskQueuingEvents =
+ this.checkAndEmitTaskQueuingEvents.bind(this)
if (this.opts.enableEvents === true) {
this.emitter = new PoolEmitter()
tasksQueueOptions.concurrency <= 0
) {
throw new Error(
- `Invalid worker tasks concurrency '${tasksQueueOptions.concurrency}'`
+ `Invalid worker tasks concurrency '${tasksQueueOptions.concurrency}' is a negative integer or zero`
)
}
}
0
)
}),
+ ...(this.opts.enableTasksQueue === true && {
+ backPressure: this.hasBackPressure()
+ }),
failedTasks: this.workerNodes.reduce(
(accumulator, workerNode) =>
accumulator + workerNode.usage.tasks.failed,
} else {
this.enqueueTask(workerNodeKey, task)
}
- this.checkAndEmitEvents()
})
}
workerTaskStatistics.executing < 0
) {
throw new Error(
- 'Worker usage statistics for tasks executing cannot be negative'
+ 'Worker usage statistic for tasks executing cannot be negative'
)
}
if (message.taskError == null) {
}
}
- private checkAndEmitEvents (): void {
+ private checkAndEmitTaskExecutionEvents (): void {
if (this.emitter != null) {
if (this.busy) {
this.emitter.emit(PoolEvents.busy, this.info)
}
}
+ private checkAndEmitTaskQueuingEvents (): void {
+ if (this.hasBackPressure()) {
+ this.emitter?.emit(PoolEvents.backPressure, this.info)
+ }
+ }
+
/**
* Gets the worker information given its worker node key.
*
this.opts.enableTasksQueue === true &&
this.workerNodes.findIndex(
(workerNode) => !workerNode.hasBackPressure()
- ) !== -1
+ ) === -1
)
}
private executeTask (workerNodeKey: number, task: Task<Data>): void {
this.beforeTaskExecutionHook(workerNodeKey, task)
this.sendToWorker(workerNodeKey, task, task.transferList)
+ this.checkAndEmitTaskExecutionEvents()
}
private enqueueTask (workerNodeKey: number, task: Task<Data>): number {
const tasksQueueSize = this.workerNodes[workerNodeKey].enqueueTask(task)
- if (this.hasBackPressure()) {
- this.emitter?.emit(PoolEvents.backPressure, this.info)
- }
+ this.checkAndEmitTaskQueuingEvents()
return tasksQueueSize
}