import {
SHARE_ENV,
Worker as ThreadWorker,
- type WorkerOptions
+ type WorkerOptions,
} from 'node:worker_threads'
import type { MessageValue, Task } from '../utility-types.js'
import {
type MeasurementStatisticsRequirements,
WorkerChoiceStrategies,
- type WorkerChoiceStrategy
+ type WorkerChoiceStrategy,
} from './selection-strategies/selection-strategies-types.js'
import type { WorkerChoiceStrategiesContext } from './selection-strategies/worker-choice-strategies-context.js'
import {
type WorkerNodeOptions,
type WorkerType,
WorkerTypes,
- type WorkerUsage
+ type WorkerUsage,
} from './worker.js'
/**
{
aggregate: false,
average: false,
- median: false
+ median: false,
}
export const getDefaultTasksQueueOptions = (
concurrency: 1,
taskStealing: true,
tasksStealingOnBackPressure: false,
- tasksFinishedTimeout: 2000
+ tasksFinishedTimeout: 2000,
}
}
export const checkValidPriority = (priority: number | undefined): void => {
if (priority != null && !Number.isSafeInteger(priority)) {
- throw new TypeError(`Invalid property 'priority': '${priority}'`)
+ throw new TypeError(`Invalid property 'priority': '${priority.toString()}'`)
}
if (
priority != null &&
tasksQueueOptions.concurrency <= 0
) {
throw new RangeError(
- `Invalid worker node tasks concurrency: ${tasksQueueOptions.concurrency} is a negative integer or zero`
+ `Invalid worker node tasks concurrency: ${tasksQueueOptions.concurrency.toString()} is a negative integer or zero`
)
}
if (
}
if (tasksQueueOptions?.size != null && tasksQueueOptions.size <= 0) {
throw new RangeError(
- `Invalid worker node tasks queue size: ${tasksQueueOptions.size} is a negative integer or zero`
+ `Invalid worker node tasks queue size: ${tasksQueueOptions.size.toString()} is a negative integer or zero`
)
}
}
}
if (!isPlainObject(opts)) {
throw new TypeError(
- 'Cannot construct a worker node with invalid options: must be a plain object'
+ 'Cannot construct a worker node with invalid worker node options: must be a plain object'
)
}
if (opts.tasksQueueBackPressureSize == null) {
'Cannot construct a worker node with a tasks queue bucket size option that is not a positive integer'
)
}
+ if (opts.tasksQueuePriority == null) {
+ throw new TypeError(
+ 'Cannot construct a worker node without a tasks queue priority option'
+ )
+ }
+ if (typeof opts.tasksQueuePriority !== 'boolean') {
+ throw new TypeError(
+ 'Cannot construct a worker node with a tasks queue priority option that is not a boolean'
+ )
+ }
}
/**
* Updates the given measurement statistics.
- *
* @param measurementStatistics - The measurement statistics to update.
* @param measurementRequirements - The measurement statistics requirements.
* @param measurementValue - The measurement value.
export const createWorker = <Worker extends IWorker>(
type: WorkerType,
filePath: string,
- opts: { env?: Record<string, unknown>, workerOptions?: WorkerOptions }
+ opts: { env?: Record<string, unknown>; workerOptions?: WorkerOptions }
): Worker => {
switch (type) {
case WorkerTypes.thread:
return new ThreadWorker(filePath, {
env: SHARE_ENV,
- ...opts.workerOptions
+ ...opts.workerOptions,
}) as unknown as Worker
case WorkerTypes.cluster:
return cluster.fork(opts.env) as unknown as Worker
/**
* Returns the worker type of the given worker.
- *
* @param worker - The worker to get the type of.
* @returns The worker type of the given worker.
* @internal
/**
* Returns the worker id of the given worker.
- *
* @param worker - The worker to get the id of.
* @returns The worker id of the given worker.
* @internal