label: Steps to reproduce
description: Steps to reproduce the bug.
placeholder: |
- - If you can please specify at least a draft of your worker (if it is an async function or not is also important)
+ - If you can please specify at least a draft of your worker (if it is an async task function or not is also important)
- Indicate which pool type are you using (i.e. FixedThreadPool) and with which options (number of threads, and so on)
- type: input
attributes:
### Fixed
-- Fix typescript type definition for worker function: ensure the input data is optional.
+- Fix typescript type definition for task function: ensure the input data is optional.
- Fix typescript type definition for pool execute(): ensure the input data is optional.
## [2.4.9] - 2023-04-15
### Fixed
-- Fix worker function type definition and validation.
+- Fix task function type definition and validation.
- Fix worker choice strategy options handling.
## [2.4.8] - 2023-04-12
- Proper integration with node [async_hooks](https://nodejs.org/api/async_hooks.html) :white_check_mark:
- Support CommonJS, ESM, and TypeScript :white_check_mark:
- Support for [worker_threads](https://nodejs.org/api/worker_threads.html) and [cluster](https://nodejs.org/api/cluster.html) Node.js modules :white_check_mark:
-- Support multiple worker functions :white_check_mark:
-- Support sync and async tasks :white_check_mark:
+- Support multiple task functions :white_check_mark:
+- Support sync and async task functions :white_check_mark:
- Tasks distribution strategies :white_check_mark:
- General guidance on pool choice :white_check_mark:
- Error handling out of the box :white_check_mark:
You can do the same with the classes _ClusterWorker_, _FixedClusterPool_ and _DynamicClusterPool_.
-**See [examples](./examples/) folder for more details (in particular if you want to use a pool with [multiple worker functions](./examples/multiFunctionExample.js))**.
+**See [examples](./examples/) folder for more details (in particular if you want to use a pool with [multiple task functions](./examples/multiFunctionExample.js))**.
Remember that workers can only send and receive structured-cloneable data.
export { KillBehaviors } from './worker/worker-options'
export type { KillBehavior, WorkerOptions } from './worker/worker-options'
export type {
+ TaskAsyncFunction,
+ TaskFunction,
TaskFunctions,
- WorkerAsyncFunction,
- WorkerFunction,
- WorkerSyncFunction
-} from './worker/worker-functions'
+ TaskSyncFunction
+} from './worker/task-functions'
export type {
MessageValue,
PromiseResponseWrapper,
/**
* Executes the specified function in the worker constructor with the task data input parameter.
*
- * @param data - The task input data for the specified worker function. This can only be structured-cloneable data.
- * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
+ * @param data - The task input data for the specified task function. This can only be structured-cloneable data.
+ * @param name - The name of the task function to execute. If not specified, the default task function will be executed.
* @returns Promise that will be fulfilled when the task is completed.
*/
readonly execute: (data?: Data, name?: string) => Promise<Response>
type WorkerOptions
} from './worker-options'
import type {
+ TaskAsyncFunction,
+ TaskFunction,
TaskFunctions,
- WorkerAsyncFunction,
- WorkerFunction,
- WorkerSyncFunction
-} from './worker-functions'
+ TaskSyncFunction
+} from './task-functions'
const DEFAULT_MAX_INACTIVE_TIME = 60000
const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT
/**
* Task function(s) processed by the worker when the pool's `execution` function is invoked.
*/
- protected taskFunctions!: Map<string, WorkerFunction<Data, Response>>
+ protected taskFunctions!: Map<string, TaskFunction<Data, Response>>
/**
* Timestamp of the last task processed by this worker.
*/
type: string,
protected readonly isMain: boolean,
private readonly mainWorker: MainWorker,
- taskFunctions:
- | WorkerFunction<Data, Response>
- | TaskFunctions<Data, Response>,
+ taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>,
protected readonly opts: WorkerOptions = {
/**
* The kill behavior option on this worker or its default value.
* @param taskFunctions - The task function(s) parameter that should be checked.
*/
private checkTaskFunctions (
- taskFunctions:
- | WorkerFunction<Data, Response>
- | TaskFunctions<Data, Response>
+ taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>
): void {
if (taskFunctions == null) {
throw new Error('taskFunctions parameter is mandatory')
}
- this.taskFunctions = new Map<string, WorkerFunction<Data, Response>>()
+ this.taskFunctions = new Map<string, TaskFunction<Data, Response>>()
if (typeof taskFunctions === 'function') {
const boundFn = taskFunctions.bind(this)
this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
*/
public addTaskFunction (
name: string,
- fn: WorkerFunction<Data, Response>
+ fn: TaskFunction<Data, Response>
): boolean {
if (typeof name !== 'string') {
throw new TypeError('name parameter is not a string')
try {
this.taskFunctions.set(
DEFAULT_TASK_NAME,
- this.taskFunctions.get(name) as WorkerFunction<Data, Response>
+ this.taskFunctions.get(name) as TaskFunction<Data, Response>
)
return true
} catch {
* @param task - Input data for the task function.
*/
protected runSync (
- fn: WorkerSyncFunction<Data, Response>,
+ fn: TaskSyncFunction<Data, Response>,
task: Task<Data>
): void {
try {
* @param task - Input data for the task function.
*/
protected runAsync (
- fn: WorkerAsyncFunction<Data, Response>,
+ fn: TaskAsyncFunction<Data, Response>,
task: Task<Data>
): void {
let taskPerformance = this.beginTaskPerformance(task.name)
* @returns The task function.
* @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found.
*/
- private getTaskFunction (name?: string): WorkerFunction<Data, Response> {
+ private getTaskFunction (name?: string): TaskFunction<Data, Response> {
name = name ?? DEFAULT_TASK_NAME
const fn = this.taskFunctions.get(name)
if (fn == null) {
import type { MessageValue } from '../utility-types'
import { AbstractWorker } from './abstract-worker'
import type { WorkerOptions } from './worker-options'
-import type { TaskFunctions, WorkerFunction } from './worker-functions'
+import type { TaskFunction, TaskFunctions } from './task-functions'
/**
* A cluster worker used by a poolifier `ClusterPool`.
* @param opts - Options for the worker.
*/
public constructor (
- taskFunctions:
- | WorkerFunction<Data, Response>
- | TaskFunctions<Data, Response>,
+ taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>,
opts: WorkerOptions = {}
) {
super(
/**
- * Worker synchronous function that can be executed.
+ * Task synchronous function that can be executed.
*
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
*/
-export type WorkerSyncFunction<Data = unknown, Response = unknown> = (
+export type TaskSyncFunction<Data = unknown, Response = unknown> = (
data?: Data
) => Response
/**
- * Worker asynchronous function that can be executed.
+ * Task asynchronous function that can be executed.
* This function must return a promise.
*
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
*/
-export type WorkerAsyncFunction<Data = unknown, Response = unknown> = (
+export type TaskAsyncFunction<Data = unknown, Response = unknown> = (
data?: Data
) => Promise<Response>
/**
- * Worker function that can be executed.
+ * Task function that can be executed.
* This function can be synchronous or asynchronous.
*
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
*/
-export type WorkerFunction<Data = unknown, Response = unknown> =
- | WorkerSyncFunction<Data, Response>
- | WorkerAsyncFunction<Data, Response>
+export type TaskFunction<Data = unknown, Response = unknown> =
+ | TaskSyncFunction<Data, Response>
+ | TaskAsyncFunction<Data, Response>
/**
- * Worker functions that can be executed.
+ * Tasks functions that can be executed.
* This object can contain synchronous or asynchronous functions.
* The key is the name of the function.
* The value is the function itself.
*/
export type TaskFunctions<Data = unknown, Response = unknown> = Record<
string,
-WorkerFunction<Data, Response>
+TaskFunction<Data, Response>
>
import type { MessageValue } from '../utility-types'
import { AbstractWorker } from './abstract-worker'
import type { WorkerOptions } from './worker-options'
-import type { TaskFunctions, WorkerFunction } from './worker-functions'
+import type { TaskFunction, TaskFunctions } from './task-functions'
/**
* A thread worker used by a poolifier `ThreadPool`.
* @param opts - Options for the worker.
*/
public constructor (
- taskFunctions:
- | WorkerFunction<Data, Response>
- | TaskFunctions<Data, Response>,
+ taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>,
opts: WorkerOptions = {}
) {
super(