this.chooseWorkerNode = this.chooseWorkerNode.bind(this)
this.executeTask = this.executeTask.bind(this)
this.enqueueTask = this.enqueueTask.bind(this)
+ this.dequeueTask = this.dequeueTask.bind(this)
this.checkAndEmitEvents = this.checkAndEmitEvents.bind(this)
if (this.opts.enableEvents === true) {
while (this.tasksQueueSize(workerNodeKey) > 0) {
let targetWorkerNodeKey: number = workerNodeKey
let minQueuedTasks = Infinity
+ let executeTask = false
for (const [workerNodeId, workerNode] of this.workerNodes.entries()) {
const workerInfo = this.getWorkerInfo(workerNodeId)
if (
workerInfo.ready &&
workerNode.usage.tasks.queued === 0
) {
+ if (workerNode.usage.tasks.executing === 0) {
+ executeTask = true
+ }
targetWorkerNodeKey = workerNodeId
break
}
targetWorkerNodeKey = workerNodeId
}
}
- this.enqueueTask(
- targetWorkerNodeKey,
- this.dequeueTask(workerNodeKey) as Task<Data>
- )
+ if (executeTask) {
+ this.executeTask(
+ targetWorkerNodeKey,
+ this.dequeueTask(workerNodeKey) as Task<Data>
+ )
+ } else {
+ this.enqueueTask(
+ targetWorkerNodeKey,
+ this.dequeueTask(workerNodeKey) as Task<Data>
+ )
+ }
}
}