From: Jérôme Benoit Date: Sun, 9 Jul 2023 19:36:56 +0000 (+0200) Subject: feat: add public methods to manipulate task functions X-Git-Tag: v2.6.13~13 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=968a2e8ca7bfc81d61b5ffc820a6a76bf44b202f;p=poolifier.git feat: add public methods to manipulate task functions Signed-off-by: Jérôme Benoit --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eccbc4c..f49ef7ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add per task function statistics tracking. +- Add public methods to manipulate the worker tasks function at runtime. + ## [2.6.12] - 2023-07-09 ### Fixed diff --git a/README.md b/README.md index e6d676ec..6ab9a619 100644 --- a/README.md +++ b/README.md @@ -231,17 +231,17 @@ An object with these properties: ### `pool.execute(data, name)` `data` (optional) An object that you want to pass to your worker implementation -`name` (optional) A string with the task function name that you want to execute on the worker. Default: `'default'` -This method is available on both pool implementations and returns a promise. +`name` (optional) A string with the task function name that you want to execute on the worker. Default: `'default'` + +This method is available on both pool implementations and returns a promise with the task function execution response. ### `pool.destroy()` -Destroy method is available on both pool implementations. -This method will call the terminate method on each worker. +This method is available on both pool implementations and will call the terminate method on each worker. ### `class YourWorker extends ThreadWorker/ClusterWorker` -`taskFunctions` (mandatory) The task function or task functions object that you want to execute on the worker +`taskFunctions` (mandatory) The task function or task functions object `{ name_1: fn_1, ..., name_n, fn_n }` that you want to execute on the worker `opts` (optional) An object with these properties: - `maxInactiveTime` (optional) - Max time to wait tasks to work on in milliseconds, after this period the new worker will die. @@ -256,6 +256,31 @@ This method will call the terminate method on each worker. This option only apply to the newly created workers. Default: `KillBehaviors.SOFT` +#### `YourWorker.hasTaskFunction(name)` + +`name` (mandatory) The task function name + +This method is available on both worker implementations and returns a boolean. + +#### `YourWorker.addTaskFunction(name, fn)` + +`name` (mandatory) The task function name +`fn` (mandatory) The task function + +This method is available on both worker implementations and returns a boolean. + +#### `YourWorker.removeTaskFunction(name)` + +`name` (mandatory) The task function name + +This method is available on both worker implementations and returns a boolean. + +#### `YourWorker.setDefaultTaskFunction(name)` + +`name` (mandatory) The task function name + +This method is available on both worker implementations and returns a boolean. + ## General guidance Performance is one of the main target of these worker pool implementations, we want to have a strong focus on this. diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index 2bc99a7b..7729f4f7 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -162,7 +162,7 @@ export class WorkerChoiceStrategyContext< * Executes the worker choice strategy algorithm in the context. * * @returns The key of the worker node. - * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker node key is null or undefined. + * @throws {@link Error} If the worker node key is null or undefined. */ public execute (): number { const workerNodeKey = ( diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 23739270..fcdd1bf2 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -143,6 +143,125 @@ export abstract class AbstractWorker< } } + /** + * Checks if the worker has a task function with the given name. + * + * @param name - The name of the task function to check. + * @returns Whether the worker has a task function with the given name or not. + * @throws {@link TypeError} If the `name` parameter is not a string. + */ + public hasTaskFunction (name: string): boolean { + if (typeof name !== 'string') { + throw new TypeError('name parameter is not a string') + } + return this.taskFunctions.has(name) + } + + /** + * Adds a task function to the worker. + * If a task function with the same name already exists, it is replaced. + * + * @param name - The name of the task function to add. + * @param fn - The task function to add. + * @returns Whether the task function was added or not. + * @throws {@link TypeError} If the `name` parameter is not a string. + * @throws {@link Error} If the `name` parameter is the default task function reserved name. + * @throws {@link TypeError} If the `fn` parameter is not a function. + */ + public addTaskFunction ( + name: string, + fn: WorkerFunction + ): boolean { + if (typeof name !== 'string') { + throw new TypeError('name parameter is not a string') + } + if (name === DEFAULT_TASK_NAME) { + throw new Error( + 'Cannot add a task function with the default reserved name' + ) + } + if (typeof fn !== 'function') { + throw new TypeError('fn parameter is not a function') + } + try { + if ( + this.taskFunctions.get(name) === + this.taskFunctions.get(DEFAULT_TASK_NAME) + ) { + this.taskFunctions.set(DEFAULT_TASK_NAME, fn.bind(this)) + } + this.taskFunctions.set(name, fn.bind(this)) + return true + } catch { + return false + } + } + + /** + * Removes a task function from the worker. + * + * @param name - The name of the task function to remove. + * @returns Whether the task function existed and was removed or not. + * @throws {@link TypeError} If the `name` parameter is not a string. + * @throws {@link Error} If the `name` parameter is the default task function reserved name. + * @throws {@link Error} If the `name` parameter is the task function used as default task function. + */ + public removeTaskFunction (name: string): boolean { + if (typeof name !== 'string') { + throw new TypeError('name parameter is not a string') + } + if (name === DEFAULT_TASK_NAME) { + throw new Error( + 'Cannot remove the task function with the default reserved name' + ) + } + if ( + this.taskFunctions.get(name) === this.taskFunctions.get(DEFAULT_TASK_NAME) + ) { + throw new Error( + 'Cannot remove the task function used as the default task function' + ) + } + return this.taskFunctions.delete(name) + } + + /** + * Sets the default task function to use when no task function name is specified. + * + * @param name - The name of the task function to use as default task function. + * @returns Whether the default task function was set or not. + * @throws {@link TypeError} If the `name` parameter is not a string. + * @throws {@link Error} If the `name` parameter is the default task function reserved name. + * @throws {@link Error} If the `name` parameter is a non-existing task function. + */ + public setDefaultTaskFunction (name: string): boolean { + if (typeof name !== 'string') { + throw new TypeError('name parameter is not a string') + } + if (name === DEFAULT_TASK_NAME) { + throw new Error( + 'Cannot set the default task function reserved name as the default task function' + ) + } + if (!this.taskFunctions.has(name)) { + throw new Error( + 'Cannot set the default task function to a non-existing task function' + ) + } + try { + this.taskFunctions.set( + DEFAULT_TASK_NAME, + this.taskFunctions.get(name)?.bind(this) as WorkerFunction< + Data, + Response + > + ) + return true + } catch { + return false + } + } + /** * Worker message listener. *