import type {
MessageValue,
PromiseResponseWrapper,
- Task,
- Writable
+ Task
} from '../utility-types'
import {
DEFAULT_TASK_NAME,
}
if (
workerChoiceStrategyOptions.choiceRetries != null &&
- workerChoiceStrategyOptions.choiceRetries <= 0
+ workerChoiceStrategyOptions.choiceRetries < 0
) {
throw new RangeError(
- `Invalid worker choice strategy options: choice retries '${workerChoiceStrategyOptions.choiceRetries}' must be greater than zero`
+ `Invalid worker choice strategy options: choice retries '${workerChoiceStrategyOptions.choiceRetries}' must be greater or equal than zero`
)
}
if (
}
private checkValidTasksQueueOptions (
- tasksQueueOptions: Writable<TasksQueueOptions>
+ tasksQueueOptions: TasksQueueOptions
): void {
if (tasksQueueOptions != null && !isPlainObject(tasksQueueOptions)) {
throw new TypeError('Invalid tasks queue options: must be a plain object')
`Invalid worker node tasks concurrency: ${tasksQueueOptions.concurrency} is a negative integer or zero`
)
}
- if (
- tasksQueueOptions?.queueMaxSize != null &&
- tasksQueueOptions?.size != null
- ) {
+ if (tasksQueueOptions?.queueMaxSize != null) {
throw new Error(
- 'Invalid tasks queue options: cannot specify both queueMaxSize and size'
+ 'Invalid tasks queue options: queueMaxSize is deprecated, please use size instead'
)
}
- if (tasksQueueOptions?.queueMaxSize != null) {
- tasksQueueOptions.size = tasksQueueOptions.queueMaxSize
- }
if (
tasksQueueOptions?.size != null &&
!Number.isSafeInteger(tasksQueueOptions.size)
) {
throw new TypeError(
- 'Invalid worker node tasks queue max size: must be an integer'
+ 'Invalid worker node tasks queue size: must be an integer'
)
}
if (tasksQueueOptions?.size != null && tasksQueueOptions.size <= 0) {
throw new RangeError(
- `Invalid worker node tasks queue max size: ${tasksQueueOptions.size} is a negative integer or zero`
+ `Invalid worker node tasks queue size: ${tasksQueueOptions.size} is a negative integer or zero`
)
}
}
return []
}
+ private shallExecuteTask (workerNodeKey: number): boolean {
+ return (
+ this.tasksQueueSize(workerNodeKey) === 0 &&
+ this.workerNodes[workerNodeKey].usage.tasks.executing <
+ (this.opts.tasksQueueOptions?.concurrency as number)
+ )
+ }
+
/** @inheritDoc */
public async execute (
data?: Data,
return await new Promise<Response>((resolve, reject) => {
if (!this.started) {
reject(new Error('Cannot execute a task on destroyed pool'))
+ return
}
if (name != null && typeof name !== 'string') {
reject(new TypeError('name argument must be a string'))
+ return
}
if (
name != null &&
name.trim().length === 0
) {
reject(new TypeError('name argument must not be an empty string'))
+ return
}
if (transferList != null && !Array.isArray(transferList)) {
reject(new TypeError('transferList argument must be an array'))
+ return
}
const timestamp = performance.now()
const workerNodeKey = this.chooseWorkerNode()
const workerInfo = this.getWorkerInfo(workerNodeKey) as WorkerInfo
- if (
- name != null &&
- Array.isArray(workerInfo.taskFunctions) &&
- !workerInfo.taskFunctions.includes(name)
- ) {
- reject(
- new Error(`Task function '${name}' is not registered in the pool`)
- )
- }
const task: Task<Data> = {
name: name ?? DEFAULT_TASK_NAME,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
if (
this.opts.enableTasksQueue === false ||
(this.opts.enableTasksQueue === true &&
- this.tasksQueueSize(workerNodeKey) === 0 &&
- this.workerNodes[workerNodeKey].usage.tasks.executing <
- (this.opts.tasksQueueOptions?.concurrency as number))
+ this.shallExecuteTask(workerNodeKey))
) {
this.executeTask(workerNodeKey, task)
} else {
...(this.dequeueTask(workerNodeKey) as Task<Data>),
workerId: destinationWorkerNode.info.id as number
}
- if (
- this.tasksQueueSize(destinationWorkerNodeKey) === 0 &&
- destinationWorkerNode.usage.tasks.executing <
- (this.opts.tasksQueueOptions?.concurrency as number)
- ) {
+ if (this.shallExecuteTask(destinationWorkerNodeKey)) {
this.executeTask(destinationWorkerNodeKey, task)
} else {
this.enqueueTask(destinationWorkerNodeKey, task)
...(sourceWorkerNode.popTask() as Task<Data>),
workerId: destinationWorkerNode.info.id as number
}
- if (
- this.tasksQueueSize(destinationWorkerNodeKey) === 0 &&
- destinationWorkerNode.usage.tasks.executing <
- (this.opts.tasksQueueOptions?.concurrency as number)
- ) {
+ if (this.shallExecuteTask(destinationWorkerNodeKey)) {
this.executeTask(destinationWorkerNodeKey, task)
} else {
this.enqueueTask(destinationWorkerNodeKey, task)
}
private tasksStealingOnBackPressure (workerId: number): void {
+ if ((this.opts.tasksQueueOptions?.size as number) <= 1) {
+ return
+ }
const sourceWorkerNode =
this.workerNodes[this.getWorkerNodeKeyByWorkerId(workerId)]
const workerNodes = this.workerNodes
...(sourceWorkerNode.popTask() as Task<Data>),
workerId: workerNode.info.id as number
}
- if (
- this.tasksQueueSize(workerNodeKey) === 0 &&
- workerNode.usage.tasks.executing <
- (this.opts.tasksQueueOptions?.concurrency as number)
- ) {
+ if (this.shallExecuteTask(workerNodeKey)) {
this.executeTask(workerNodeKey, task)
} else {
this.enqueueTask(workerNodeKey, task)