} from '../utils'
import { Deque } from '../deque'
import {
- type BackPressureCallback,
- type EmptyQueueCallback,
type IWorker,
type IWorkerNode,
+ type StrategyData,
type WorkerInfo,
+ type WorkerNodeEventCallback,
type WorkerType,
WorkerTypes,
type WorkerUsage
/** @inheritdoc */
public usage: WorkerUsage
/** @inheritdoc */
+ public strategyData?: StrategyData
+ /** @inheritdoc */
public messageChannel?: MessageChannel
/** @inheritdoc */
public tasksQueueBackPressureSize: number
/** @inheritdoc */
- public onBackPressure?: BackPressureCallback
+ public onBackPressure?: WorkerNodeEventCallback
/** @inheritdoc */
- public onEmptyQueue?: EmptyQueueCallback
+ public onEmptyQueue?: WorkerNodeEventCallback
private readonly tasksQueue: Deque<Task<Data>>
private onEmptyQueueCount: number
private readonly taskFunctionsUsage: Map<string, WorkerUsage>
* @param tasksQueueBackPressureSize - The tasks queue back pressure size.
*/
constructor (worker: Worker, tasksQueueBackPressureSize: number) {
- if (worker == null) {
- throw new TypeError('Cannot construct a worker node without a worker')
- }
- if (tasksQueueBackPressureSize == null) {
- throw new TypeError(
- 'Cannot construct a worker node without a tasks queue back pressure size'
- )
- }
- if (!Number.isSafeInteger(tasksQueueBackPressureSize)) {
- throw new TypeError(
- 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
- )
- }
+ this.checkWorkerNodeArguments(worker, tasksQueueBackPressureSize)
this.worker = worker
this.info = this.initWorkerInfo(worker)
this.usage = this.initWorkerUsage()
/** @inheritdoc */
public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined {
- if (!Array.isArray(this.info.taskFunctions)) {
+ if (!Array.isArray(this.info.taskFunctionNames)) {
throw new Error(
`Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined`
)
}
if (
- Array.isArray(this.info.taskFunctions) &&
- this.info.taskFunctions.length < 3
+ Array.isArray(this.info.taskFunctionNames) &&
+ this.info.taskFunctionNames.length < 3
) {
throw new Error(
`Cannot get task function worker usage for task function name '${name}' when task function names list has less than 3 elements`
)
}
if (name === DEFAULT_TASK_NAME) {
- name = this.info.taskFunctions[1]
+ name = this.info.taskFunctionNames[1]
}
if (!this.taskFunctionsUsage.has(name)) {
this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name))
this.onEmptyQueueCount = 0
return
}
- (this.onEmptyQueue as EmptyQueueCallback)(this.info.id as number)
+ (this.onEmptyQueue as WorkerNodeEventCallback)(this.info.id as number)
++this.onEmptyQueueCount
await sleep(exponentialDelay(this.onEmptyQueueCount))
await this.startOnEmptyQueue()
for (const task of this.tasksQueue) {
if (
(task.name === DEFAULT_TASK_NAME &&
- name === (this.info.taskFunctions as string[])[1]) ||
+ name === (this.info.taskFunctionNames as string[])[1]) ||
(task.name !== DEFAULT_TASK_NAME && name === task.name)
) {
++taskFunctionQueueSize
}
}
}
+
+ private checkWorkerNodeArguments (
+ worker: Worker,
+ tasksQueueBackPressureSize: number
+ ): void {
+ if (worker == null) {
+ throw new TypeError('Cannot construct a worker node without a worker')
+ }
+ if (tasksQueueBackPressureSize == null) {
+ throw new TypeError(
+ 'Cannot construct a worker node without a tasks queue back pressure size'
+ )
+ }
+ if (!Number.isSafeInteger(tasksQueueBackPressureSize)) {
+ throw new TypeError(
+ 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
+ )
+ }
+ if (tasksQueueBackPressureSize <= 0) {
+ throw new RangeError(
+ 'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer'
+ )
+ }
+ }
}