From 027063571693f211b35c8851566a063201adb9af Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 12 Apr 2023 00:42:22 +0200 Subject: [PATCH] refactor: explicity extends Task for MessageValue type MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .eslintrc.js | 1 + src/pools/abstract-pool.ts | 4 +-- src/pools/cluster/dynamic.ts | 2 +- src/pools/cluster/fixed.ts | 2 +- src/pools/pool.ts | 2 +- .../abstract-worker-choice-strategy.ts | 2 +- .../fair-share-worker-choice-strategy.ts | 2 +- .../less-busy-worker-choice-strategy.ts | 2 +- .../less-used-worker-choice-strategy.ts | 2 +- .../round-robin-worker-choice-strategy.ts | 2 +- ...hted-round-robin-worker-choice-strategy.ts | 2 +- .../worker-choice-strategy-context.ts | 2 +- src/pools/thread/dynamic.ts | 2 +- src/pools/thread/fixed.ts | 2 +- src/pools/worker.ts | 35 +++++++++++++++---- src/utility-types.ts | 14 ++------ 16 files changed, 46 insertions(+), 32 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index ae288a7c..cc27eee7 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -46,6 +46,7 @@ module.exports = defineConfig({ 'ecma', 'enqueue', 'enum', + 'errored', 'esm', 'fibonacci', 'fs', diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index b95d16b4..9f7813e7 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -27,7 +27,7 @@ import { CircularArray } from '../circular-array' * * @typeParam Worker - Type of worker which manages this pool. * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of response of execution. This can only be serializable data. + * @typeParam Response - Type of execution response. This can only be serializable data. */ export abstract class AbstractPool< Worker extends IWorker, @@ -261,7 +261,7 @@ export abstract class AbstractPool< id: crypto.randomUUID() } const res = new Promise((resolve, reject) => { - this.promiseResponseMap.set(submittedTask.id, { + this.promiseResponseMap.set(submittedTask.id as string, { resolve, reject, worker: workerNode.worker diff --git a/src/pools/cluster/dynamic.ts b/src/pools/cluster/dynamic.ts index 78c63aca..c42e02b0 100644 --- a/src/pools/cluster/dynamic.ts +++ b/src/pools/cluster/dynamic.ts @@ -9,7 +9,7 @@ import { FixedClusterPool } from './fixed' * When the maximum number of workers is reached and workers are busy, an event is emitted. If you want to listen to this event, use the pool's `emitter`. * * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of response of execution. This can only be serializable data. + * @typeParam Response - Type of execution response. This can only be serializable data. * @author [Christopher Quadflieg](https://github.com/Shinigami92) * @since 2.0.0 */ diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index faa68fc7..ed4f6fed 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -32,7 +32,7 @@ export interface ClusterPoolOptions extends PoolOptions { * This pool selects the workers in a round robin fashion. * * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of response of execution. This can only be serializable data. + * @typeParam Response - Type of execution response. This can only be serializable data. * @author [Christopher Quadflieg](https://github.com/Shinigami92) * @since 2.0.0 */ diff --git a/src/pools/pool.ts b/src/pools/pool.ts index 17088559..4395712f 100644 --- a/src/pools/pool.ts +++ b/src/pools/pool.ts @@ -114,7 +114,7 @@ export interface PoolOptions { * * @typeParam Worker - Type of worker which manages this pool. * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of response of execution. This can only be serializable data. + * @typeParam Response - Type of execution response. This can only be serializable data. */ export interface IPool< Worker extends IWorker, diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index fd9cf1fb..ce7f9589 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -12,7 +12,7 @@ import type { * * @typeParam Worker - Type of worker which manages the strategy. * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of response of execution. This can only be serializable data. + * @typeParam Response - Type of execution response. This can only be serializable data. */ export abstract class AbstractWorkerChoiceStrategy< Worker extends IWorker, diff --git a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts index e1d123a9..3f4e9524 100644 --- a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts @@ -19,7 +19,7 @@ interface WorkerVirtualTaskTimestamp { * * @typeParam Worker - Type of worker which manages the strategy. * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of response of execution. This can only be serializable data. + * @typeParam Response - Type of execution response. This can only be serializable data. */ export class FairShareWorkerChoiceStrategy< Worker extends IWorker, diff --git a/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts b/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts index d98732d6..12e87ff2 100644 --- a/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts @@ -10,7 +10,7 @@ import type { * * @typeParam Worker - Type of worker which manages the strategy. * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of response of execution. This can only be serializable data. + * @typeParam Response - Type of execution response. This can only be serializable data. */ export class LessBusyWorkerChoiceStrategy< Worker extends IWorker, diff --git a/src/pools/selection-strategies/less-used-worker-choice-strategy.ts b/src/pools/selection-strategies/less-used-worker-choice-strategy.ts index acf1e503..166de706 100644 --- a/src/pools/selection-strategies/less-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-used-worker-choice-strategy.ts @@ -7,7 +7,7 @@ import type { IWorkerChoiceStrategy } from './selection-strategies-types' * * @typeParam Worker - Type of worker which manages the strategy. * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of response of execution. This can only be serializable data. + * @typeParam Response - Type of execution response. This can only be serializable data. */ export class LessUsedWorkerChoiceStrategy< Worker extends IWorker, diff --git a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts index eda00c75..80e958a4 100644 --- a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts @@ -7,7 +7,7 @@ import type { IWorkerChoiceStrategy } from './selection-strategies-types' * * @typeParam Worker - Type of worker which manages the strategy. * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of response of execution. This can only be serializable data. + * @typeParam Response - Type of execution response. This can only be serializable data. */ export class RoundRobinWorkerChoiceStrategy< Worker extends IWorker, diff --git a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts index f4bac5d7..665022c7 100644 --- a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts @@ -22,7 +22,7 @@ interface TaskRunTime { * * @typeParam Worker - Type of worker which manages the strategy. * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of response of execution. This can only be serializable data. + * @typeParam Response - Type of execution response. This can only be serializable data. */ export class WeightedRoundRobinWorkerChoiceStrategy< Worker extends IWorker, diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index 34aa332e..91441ace 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -19,7 +19,7 @@ import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-w * * @typeParam Worker - Type of worker. * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of response of execution. This can only be serializable data. + * @typeParam Response - Type of execution response. This can only be serializable data. */ export class WorkerChoiceStrategyContext< Worker extends IWorker, diff --git a/src/pools/thread/dynamic.ts b/src/pools/thread/dynamic.ts index 7bf477dd..bb177f87 100644 --- a/src/pools/thread/dynamic.ts +++ b/src/pools/thread/dynamic.ts @@ -10,7 +10,7 @@ import { FixedThreadPool } from './fixed' * When the maximum number of threads is reached and workers are busy, an event is emitted. If you want to listen to this event, use the pool's `emitter`. * * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of response of execution. This can only be serializable data. + * @typeParam Response - Type of execution response. This can only be serializable data. * @author [Alessandro Pio Ardizio](https://github.com/pioardi) * @since 0.0.1 */ diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index 764d5851..3dc10821 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -22,7 +22,7 @@ export type ThreadWorkerWithMessageChannel = Worker & Draft * This pool selects the threads in a round robin fashion. * * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of response of execution. This can only be serializable data. + * @typeParam Response - Type of execution response. This can only be serializable data. * @author [Alessandro Pio Ardizio](https://github.com/pioardi) * @since 0.0.1 */ diff --git a/src/pools/worker.ts b/src/pools/worker.ts index 3bb0ac9d..05b55985 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -30,32 +30,53 @@ export type ExitHandler = ( ) => void /** - * Worker task interface. + * Message object that is passed as a task between main worker and worker. * * @typeParam Data - Type of data sent to the worker. This can only be serializable data. * @internal */ export interface Task { /** - * Worker task data. + * Input data that will be passed to the worker. */ - data: Data + readonly data?: Data /** - * Task UUID. + * UUID of the message. */ - id: string + readonly id?: string } /** * Worker tasks usage statistics. */ export interface TasksUsage { + /** + * Number of tasks executed. + */ run: number + /** + * Number of tasks running. + */ running: number + /** + * Tasks runtime. + */ runTime: number + /** + * Tasks runtime history. + */ runTimeHistory: CircularArray + /** + * Average tasks runtime. + */ avgRunTime: number + /** + * Median tasks runtime. + */ medRunTime: number + /** + * Number of tasks errored. + */ error: number } @@ -93,7 +114,7 @@ export interface WorkerNode { /** * Worker node worker. */ - worker: Worker + readonly worker: Worker /** * Worker node tasks usage statistics. */ @@ -101,5 +122,5 @@ export interface WorkerNode { /** * Worker node tasks queue. */ - tasksQueue: Array> + readonly tasksQueue: Array> } diff --git a/src/utility-types.ts b/src/utility-types.ts index 685e5c4c..3a454f3e 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -1,7 +1,7 @@ import type { Worker as ClusterWorker } from 'node:cluster' import type { MessagePort } from 'node:worker_threads' import type { KillBehavior } from './worker/worker-options' -import type { IWorker } from './pools/worker' +import type { IWorker, Task } from './pools/worker' /** * Make all properties in T non-readonly. @@ -9,7 +9,7 @@ import type { IWorker } from './pools/worker' export type Draft = { -readonly [P in keyof T]?: T[P] } /** - * Message object that is passed between worker and main worker. + * Message object that is passed between main worker and worker. * * @typeParam Data - Type of data sent to the worker. This can only be serializable data. * @typeParam MainWorker - Type of main worker. @@ -17,15 +17,7 @@ export type Draft = { -readonly [P in keyof T]?: T[P] } export interface MessageValue< Data = unknown, MainWorker extends ClusterWorker | MessagePort | unknown = unknown -> { - /** - * Input data that will be passed to the worker. - */ - readonly data?: Data - /** - * Id of the message. - */ - readonly id?: string +> extends Task { /** * Kill code. */ -- 2.34.1