import { MessageChannel } from 'node:worker_threads'
import { CircularArray } from '../circular-array'
import type { Task } from '../utility-types'
-import { DEFAULT_TASK_NAME } from '../utils'
+import {
+ DEFAULT_TASK_NAME,
+ EMPTY_FUNCTION,
+ exponentialDelay,
+ sleep
+} from '../utils'
import { Deque } from '../deque'
import {
type IWorker,
/** @inheritdoc */
public readonly info: WorkerInfo
/** @inheritdoc */
- public messageChannel?: MessageChannel
- /** @inheritdoc */
public usage: WorkerUsage
/** @inheritdoc */
+ public messageChannel?: MessageChannel
+ /** @inheritdoc */
public tasksQueueBackPressureSize: number
- private readonly taskFunctionsUsage: Map<string, WorkerUsage>
+ /** @inheritdoc */
+ public onBackPressure?: (workerId: number) => void
+ /** @inheritdoc */
+ public onEmptyQueue?: (workerId: number) => void
private readonly tasksQueue: Deque<Task<Data>>
+ private onEmptyQueueCount: number
+ private readonly taskFunctionsUsage: Map<string, WorkerUsage>
/**
* Constructs a new worker node.
}
this.worker = worker
this.info = this.initWorkerInfo(worker, workerType)
+ this.usage = this.initWorkerUsage()
if (workerType === WorkerTypes.thread) {
this.messageChannel = new MessageChannel()
}
- this.usage = this.initWorkerUsage()
- this.taskFunctionsUsage = new Map<string, WorkerUsage>()
- this.tasksQueue = new Deque<Task<Data>>()
this.tasksQueueBackPressureSize = tasksQueueBackPressureSize
+ this.tasksQueue = new Deque<Task<Data>>()
+ this.onEmptyQueueCount = 0
+ this.taskFunctionsUsage = new Map<string, WorkerUsage>()
}
/** @inheritdoc */
return this.tasksQueue.size
}
- /**
- * Tasks queue maximum size.
- *
- * @returns The tasks queue maximum size.
- */
- private tasksQueueMaxSize (): number {
- return this.tasksQueue.maxSize
+ /** @inheritdoc */
+ public enqueueTask (task: Task<Data>): number {
+ const tasksQueueSize = this.tasksQueue.push(task)
+ if (this.onBackPressure != null && this.hasBackPressure()) {
+ this.onBackPressure(this.info.id as number)
+ }
+ return tasksQueueSize
}
/** @inheritdoc */
- public enqueueTask (task: Task<Data>): number {
- return this.tasksQueue.push(task)
+ public unshiftTask (task: Task<Data>): number {
+ const tasksQueueSize = this.tasksQueue.unshift(task)
+ if (this.onBackPressure != null && this.hasBackPressure()) {
+ this.onBackPressure(this.info.id as number)
+ }
+ return tasksQueueSize
}
/** @inheritdoc */
public dequeueTask (): Task<Data> | undefined {
- return this.tasksQueue.shift()
+ const task = this.tasksQueue.shift()
+ if (this.onEmptyQueue != null && this.tasksQueue.size === 0) {
+ this.startOnEmptyQueue().catch(EMPTY_FUNCTION)
+ }
+ return task
+ }
+
+ /** @inheritdoc */
+ public popTask (): Task<Data> | undefined {
+ const task = this.tasksQueue.pop()
+ if (this.onEmptyQueue != null && this.tasksQueue.size === 0) {
+ this.startOnEmptyQueue().catch(EMPTY_FUNCTION)
+ }
+ return task
}
/** @inheritdoc */
return this.taskFunctionsUsage.get(name)
}
+ private async startOnEmptyQueue (): Promise<void> {
+ if (
+ this.onEmptyQueueCount > 0 &&
+ (this.usage.tasks.executing > 0 || this.tasksQueue.size > 0)
+ ) {
+ this.onEmptyQueueCount = 0
+ return
+ }
+ (this.onEmptyQueue as (workerId: number) => void)(this.info.id as number)
+ ++this.onEmptyQueueCount
+ await sleep(exponentialDelay(this.onEmptyQueueCount))
+ await this.startOnEmptyQueue()
+ }
+
private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo {
return {
id: this.getWorkerId(worker, workerType),
private initWorkerUsage (): WorkerUsage {
const getTasksQueueSize = (): number => {
- return this.tasksQueueSize()
+ return this.tasksQueue.size
}
const getTasksQueueMaxSize = (): number => {
- return this.tasksQueueMaxSize()
+ return this.tasksQueue.maxSize
}
return {
tasks: {
get maxQueued (): number {
return getTasksQueueMaxSize()
},
+ stolen: 0,
failed: 0
},
runTime: {
get queued (): number {
return getTaskFunctionQueueSize()
},
+ stolen: 0,
failed: 0
},
runTime: {