From: Jérôme Benoit Date: Wed, 2 Aug 2023 00:54:55 +0000 (+0200) Subject: refactor: uniform namespace for task function(s) X-Git-Tag: v2.6.21~5 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=82ea6492d3318a170559bb57501dc16023bb18d8;p=poolifier.git refactor: uniform namespace for task function(s) Signed-off-by: Jérôme Benoit --- diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index d930d75e..e2122b8e 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -36,7 +36,7 @@ body: 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: diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ae2e31b..5ac36ccc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -298,7 +298,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### 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 @@ -315,7 +315,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### 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 diff --git a/README.md b/README.md index 238002bc..7ae15e10 100644 --- a/README.md +++ b/README.md @@ -42,8 +42,8 @@ Please consult our [general guidelines](#general-guidance). - 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: @@ -150,7 +150,7 @@ pool 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. diff --git a/src/index.ts b/src/index.ts index 8a3cb9b1..c9ee4c63 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,11 +52,11 @@ export { ThreadWorker } from './worker/thread-worker' 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, diff --git a/src/pools/pool.ts b/src/pools/pool.ts index 4b9f40f4..45915e55 100644 --- a/src/pools/pool.ts +++ b/src/pools/pool.ts @@ -192,8 +192,8 @@ export interface IPool< /** * 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 diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 428f6313..1b34fb13 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -20,11 +20,11 @@ import { 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 @@ -48,7 +48,7 @@ export abstract class AbstractWorker< /** * Task function(s) processed by the worker when the pool's `execution` function is invoked. */ - protected taskFunctions!: Map> + protected taskFunctions!: Map> /** * Timestamp of the last task processed by this worker. */ @@ -74,9 +74,7 @@ export abstract class AbstractWorker< type: string, protected readonly isMain: boolean, private readonly mainWorker: MainWorker, - taskFunctions: - | WorkerFunction - | TaskFunctions, + taskFunctions: TaskFunction | TaskFunctions, protected readonly opts: WorkerOptions = { /** * The kill behavior option on this worker or its default value. @@ -110,14 +108,12 @@ export abstract class AbstractWorker< * @param taskFunctions - The task function(s) parameter that should be checked. */ private checkTaskFunctions ( - taskFunctions: - | WorkerFunction - | TaskFunctions + taskFunctions: TaskFunction | TaskFunctions ): void { if (taskFunctions == null) { throw new Error('taskFunctions parameter is mandatory') } - this.taskFunctions = new Map>() + this.taskFunctions = new Map>() if (typeof taskFunctions === 'function') { const boundFn = taskFunctions.bind(this) this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn) @@ -185,7 +181,7 @@ export abstract class AbstractWorker< */ public addTaskFunction ( name: string, - fn: WorkerFunction + fn: TaskFunction ): boolean { if (typeof name !== 'string') { throw new TypeError('name parameter is not a string') @@ -276,7 +272,7 @@ export abstract class AbstractWorker< try { this.taskFunctions.set( DEFAULT_TASK_NAME, - this.taskFunctions.get(name) as WorkerFunction + this.taskFunctions.get(name) as TaskFunction ) return true } catch { @@ -415,7 +411,7 @@ export abstract class AbstractWorker< * @param task - Input data for the task function. */ protected runSync ( - fn: WorkerSyncFunction, + fn: TaskSyncFunction, task: Task ): void { try { @@ -451,7 +447,7 @@ export abstract class AbstractWorker< * @param task - Input data for the task function. */ protected runAsync ( - fn: WorkerAsyncFunction, + fn: TaskAsyncFunction, task: Task ): void { let taskPerformance = this.beginTaskPerformance(task.name) @@ -491,7 +487,7 @@ export abstract class AbstractWorker< * @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 { + private getTaskFunction (name?: string): TaskFunction { name = name ?? DEFAULT_TASK_NAME const fn = this.taskFunctions.get(name) if (fn == null) { diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index 4c1e1187..0ccc0013 100644 --- a/src/worker/cluster-worker.ts +++ b/src/worker/cluster-worker.ts @@ -2,7 +2,7 @@ import cluster, { type Worker } from 'node:cluster' 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`. @@ -29,9 +29,7 @@ export class ClusterWorker< * @param opts - Options for the worker. */ public constructor ( - taskFunctions: - | WorkerFunction - | TaskFunctions, + taskFunctions: TaskFunction | TaskFunctions, opts: WorkerOptions = {} ) { super( diff --git a/src/worker/worker-functions.ts b/src/worker/task-functions.ts similarity index 70% rename from src/worker/worker-functions.ts rename to src/worker/task-functions.ts index 9232e58f..a61d6962 100644 --- a/src/worker/worker-functions.ts +++ b/src/worker/task-functions.ts @@ -1,37 +1,37 @@ /** - * 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 = ( +export type TaskSyncFunction = ( 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 = ( +export type TaskAsyncFunction = ( data?: Data ) => Promise /** - * 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 = - | WorkerSyncFunction - | WorkerAsyncFunction +export type TaskFunction = + | TaskSyncFunction + | TaskAsyncFunction /** - * 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. @@ -41,5 +41,5 @@ export type WorkerFunction = */ export type TaskFunctions = Record< string, -WorkerFunction +TaskFunction > diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index dc628bed..4666fbc7 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -7,7 +7,7 @@ import { 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`. @@ -38,9 +38,7 @@ export class ThreadWorker< * @param opts - Options for the worker. */ public constructor ( - taskFunctions: - | WorkerFunction - | TaskFunctions, + taskFunctions: TaskFunction | TaskFunctions, opts: WorkerOptions = {} ) { super(