From: Jérôme Benoit Date: Sat, 15 Jul 2023 09:39:59 +0000 (+0200) Subject: feat: add method to list registered task functions on a worker X-Git-Tag: v2.6.17~10 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=c50b93fb2dc560d475f5a51cbe11c455c310be22;p=poolifier.git feat: add method to list registered task functions on a worker Signed-off-by: Jérôme Benoit --- diff --git a/README.md b/README.md index 0d17e08b..d8b3eb63 100644 --- a/README.md +++ b/README.md @@ -275,6 +275,10 @@ This method is available on both worker implementations and returns a boolean. This method is available on both worker implementations and returns a boolean. +#### `YourWorker.listTaskFunctions()` + +This method is available on both worker implementations and returns an array of the task function names. + #### `YourWorker.setDefaultTaskFunction(name)` `name` (mandatory) The task function name @@ -284,7 +288,7 @@ 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. -We already have a benchmarks folder where you can find some comparisons. +We already have a [benchmarks](./benchmarks/) folder where you can find some comparisons. ### Internal Node.js thread pool diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 3adc56d1..8a6b2104 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -142,11 +142,11 @@ export abstract class AbstractWorker< ) } const boundFn = fn.bind(this) - this.taskFunctions.set(name, boundFn) if (firstEntry) { this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn) firstEntry = false } + this.taskFunctions.set(name, boundFn) } if (firstEntry) { throw new Error('taskFunctions parameter object is empty') @@ -242,7 +242,16 @@ export abstract class AbstractWorker< } /** - * Sets the default task function to use when no task function name is specified. + * Lists the names of the worker's task functions. + * + * @returns The names of the worker's task functions. + */ + public listTaskFunctions (): string[] { + return Array.from(this.taskFunctions.keys()) + } + + /** + * Sets the default task function to use in the worker. * * @param name - The name of the task function to use as default task function. * @returns Whether the default task function was set or not. diff --git a/tests/worker/abstract-worker.test.js b/tests/worker/abstract-worker.test.js index 944acbec..e9da7a87 100644 --- a/tests/worker/abstract-worker.test.js +++ b/tests/worker/abstract-worker.test.js @@ -218,6 +218,17 @@ describe('Abstract worker test suite', () => { expect(worker.taskFunctions.size).toBe(2) }) + it('Verify that listTaskFunctions() works', () => { + const fn1 = () => { + return 1 + } + const fn2 = () => { + return 2 + } + const worker = new ClusterWorker({ fn1, fn2 }) + expect(worker.listTaskFunctions()).toStrictEqual(['default', 'fn1', 'fn2']) + }) + it('Verify that setDefaultTaskFunction() works', () => { const fn1 = () => { return 1