poolifier 2.4.7 → 2.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,7 +9,7 @@ import { WorkerChoiceStrategyContext } from './selection-strategies/worker-choic
9
9
  *
10
10
  * @typeParam Worker - Type of worker which manages this pool.
11
11
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
12
- * @typeParam Response - Type of response of execution. This can only be serializable data.
12
+ * @typeParam Response - Type of execution response. This can only be serializable data.
13
13
  */
14
14
  export declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Response = unknown> implements IPool<Worker, Data, Response> {
15
15
  readonly numberOfWorkers: number;
@@ -174,6 +174,7 @@ export declare abstract class AbstractPool<Worker extends IWorker, Data = unknow
174
174
  * Gets the given worker its tasks usage in the pool.
175
175
  *
176
176
  * @param worker - The worker.
177
+ * @throws {@link Error} if the worker is not found in the pool worker nodes.
177
178
  * @returns The worker tasks usage.
178
179
  */
179
180
  private getWorkerTasksUsage;
@@ -8,7 +8,7 @@ import { FixedClusterPool } from './fixed';
8
8
  * 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`.
9
9
  *
10
10
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
11
- * @typeParam Response - Type of response of execution. This can only be serializable data.
11
+ * @typeParam Response - Type of execution response. This can only be serializable data.
12
12
  * @author [Christopher Quadflieg](https://github.com/Shinigami92)
13
13
  * @since 2.0.0
14
14
  */
@@ -29,7 +29,7 @@ export interface ClusterPoolOptions extends PoolOptions<Worker> {
29
29
  * This pool selects the workers in a round robin fashion.
30
30
  *
31
31
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
32
- * @typeParam Response - Type of response of execution. This can only be serializable data.
32
+ * @typeParam Response - Type of execution response. This can only be serializable data.
33
33
  * @author [Christopher Quadflieg](https://github.com/Shinigami92)
34
34
  * @since 2.0.0
35
35
  */
@@ -6,6 +6,7 @@ import type { WorkerChoiceStrategy, WorkerChoiceStrategyOptions } from './select
6
6
  * Pool types.
7
7
  *
8
8
  * @enum
9
+ * @internal
9
10
  */
10
11
  export declare enum PoolType {
11
12
  /**
@@ -47,7 +48,7 @@ export interface TasksQueueOptions {
47
48
  /**
48
49
  * Options for a poolifier pool.
49
50
  *
50
- * @typeParam Worker - The worker type.
51
+ * @typeParam Worker - Type of worker.
51
52
  */
52
53
  export interface PoolOptions<Worker extends IWorker> {
53
54
  /**
@@ -99,7 +100,7 @@ export interface PoolOptions<Worker extends IWorker> {
99
100
  *
100
101
  * @typeParam Worker - Type of worker which manages this pool.
101
102
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
102
- * @typeParam Response - Type of response of execution. This can only be serializable data.
103
+ * @typeParam Response - Type of execution response. This can only be serializable data.
103
104
  */
104
105
  export interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
105
106
  /**
@@ -6,7 +6,7 @@ import type { IWorkerChoiceStrategy, RequiredStatistics, WorkerChoiceStrategyOpt
6
6
  *
7
7
  * @typeParam Worker - Type of worker which manages the strategy.
8
8
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
9
- * @typeParam Response - Type of response of execution. This can only be serializable data.
9
+ * @typeParam Response - Type of execution response. This can only be serializable data.
10
10
  */
11
11
  export declare abstract class AbstractWorkerChoiceStrategy<Worker extends IWorker, Data = unknown, Response = unknown> implements IWorkerChoiceStrategy {
12
12
  protected readonly pool: IPool<Worker, Data, Response>;
@@ -7,7 +7,7 @@ import type { IWorkerChoiceStrategy, RequiredStatistics } from './selection-stra
7
7
  *
8
8
  * @typeParam Worker - Type of worker which manages the strategy.
9
9
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
10
- * @typeParam Response - Type of response of execution. This can only be serializable data.
10
+ * @typeParam Response - Type of execution response. This can only be serializable data.
11
11
  */
12
12
  export declare class FairShareWorkerChoiceStrategy<Worker extends IWorker, Data = unknown, Response = unknown> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> implements IWorkerChoiceStrategy {
13
13
  /** @inheritDoc */
@@ -6,7 +6,7 @@ import type { IWorkerChoiceStrategy, RequiredStatistics } from './selection-stra
6
6
  *
7
7
  * @typeParam Worker - Type of worker which manages the strategy.
8
8
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
9
- * @typeParam Response - Type of response of execution. This can only be serializable data.
9
+ * @typeParam Response - Type of execution response. This can only be serializable data.
10
10
  */
11
11
  export declare class LessBusyWorkerChoiceStrategy<Worker extends IWorker, Data = unknown, Response = unknown> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> implements IWorkerChoiceStrategy {
12
12
  /** @inheritDoc */
@@ -6,7 +6,7 @@ import type { IWorkerChoiceStrategy } from './selection-strategies-types';
6
6
  *
7
7
  * @typeParam Worker - Type of worker which manages the strategy.
8
8
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
9
- * @typeParam Response - Type of response of execution. This can only be serializable data.
9
+ * @typeParam Response - Type of execution response. This can only be serializable data.
10
10
  */
11
11
  export declare class LessUsedWorkerChoiceStrategy<Worker extends IWorker, Data = unknown, Response = unknown> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> implements IWorkerChoiceStrategy {
12
12
  /** @inheritDoc */
@@ -6,7 +6,7 @@ import type { IWorkerChoiceStrategy } from './selection-strategies-types';
6
6
  *
7
7
  * @typeParam Worker - Type of worker which manages the strategy.
8
8
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
9
- * @typeParam Response - Type of response of execution. This can only be serializable data.
9
+ * @typeParam Response - Type of execution response. This can only be serializable data.
10
10
  */
11
11
  export declare class RoundRobinWorkerChoiceStrategy<Worker extends IWorker, Data = unknown, Response = unknown> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> implements IWorkerChoiceStrategy {
12
12
  /**
@@ -38,6 +38,8 @@ export interface WorkerChoiceStrategyOptions {
38
38
  }
39
39
  /**
40
40
  * Pool worker tasks usage statistics requirements.
41
+ *
42
+ * @internal
41
43
  */
42
44
  export interface RequiredStatistics {
43
45
  /**
@@ -8,7 +8,7 @@ import type { IPool } from '../pool';
8
8
  *
9
9
  * @typeParam Worker - Type of worker which manages the strategy.
10
10
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
11
- * @typeParam Response - Type of response of execution. This can only be serializable data.
11
+ * @typeParam Response - Type of execution response. This can only be serializable data.
12
12
  */
13
13
  export declare class WeightedRoundRobinWorkerChoiceStrategy<Worker extends IWorker, Data = unknown, Response = unknown> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> implements IWorkerChoiceStrategy {
14
14
  /** @inheritDoc */
@@ -6,7 +6,7 @@ import type { RequiredStatistics, WorkerChoiceStrategy, WorkerChoiceStrategyOpti
6
6
  *
7
7
  * @typeParam Worker - Type of worker.
8
8
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
9
- * @typeParam Response - Type of response of execution. This can only be serializable data.
9
+ * @typeParam Response - Type of execution response. This can only be serializable data.
10
10
  */
11
11
  export declare class WorkerChoiceStrategyContext<Worker extends IWorker, Data = unknown, Response = unknown> {
12
12
  private workerChoiceStrategyType;
@@ -9,7 +9,7 @@ import { FixedThreadPool } from './fixed';
9
9
  * 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`.
10
10
  *
11
11
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
12
- * @typeParam Response - Type of response of execution. This can only be serializable data.
12
+ * @typeParam Response - Type of execution response. This can only be serializable data.
13
13
  * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
14
14
  * @since 0.0.1
15
15
  */
@@ -16,7 +16,7 @@ export type ThreadWorkerWithMessageChannel = Worker & Draft<MessageChannel>;
16
16
  * This pool selects the threads in a round robin fashion.
17
17
  *
18
18
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
19
- * @typeParam Response - Type of response of execution. This can only be serializable data.
19
+ * @typeParam Response - Type of execution response. This can only be serializable data.
20
20
  * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
21
21
  * @since 0.0.1
22
22
  */
@@ -16,31 +16,54 @@ export type OnlineHandler<Worker extends IWorker> = (this: Worker) => void;
16
16
  */
17
17
  export type ExitHandler<Worker extends IWorker> = (this: Worker, code: number) => void;
18
18
  /**
19
- * Worker task interface.
19
+ * Message object that is passed as a task between main worker and worker.
20
20
  *
21
21
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
22
22
  * @internal
23
23
  */
24
24
  export interface Task<Data = unknown> {
25
25
  /**
26
- * Worker task data.
26
+ * Input data that will be passed to the worker.
27
27
  */
28
- data: Data;
28
+ readonly data?: Data;
29
29
  /**
30
- * Task UUID.
30
+ * UUID of the message.
31
31
  */
32
- id: string;
32
+ readonly id?: string;
33
33
  }
34
34
  /**
35
35
  * Worker tasks usage statistics.
36
+ *
37
+ * @internal
36
38
  */
37
39
  export interface TasksUsage {
40
+ /**
41
+ * Number of tasks executed.
42
+ */
38
43
  run: number;
44
+ /**
45
+ * Number of tasks running.
46
+ */
39
47
  running: number;
48
+ /**
49
+ * Tasks runtime.
50
+ */
40
51
  runTime: number;
52
+ /**
53
+ * Tasks runtime history.
54
+ */
41
55
  runTimeHistory: CircularArray<number>;
56
+ /**
57
+ * Average tasks runtime.
58
+ */
42
59
  avgRunTime: number;
60
+ /**
61
+ * Median tasks runtime.
62
+ */
43
63
  medRunTime: number;
64
+ /**
65
+ * Number of tasks errored.
66
+ */
44
67
  error: number;
45
68
  }
46
69
  /**
@@ -73,7 +96,7 @@ export interface WorkerNode<Worker extends IWorker, Data = unknown> {
73
96
  /**
74
97
  * Worker node worker.
75
98
  */
76
- worker: Worker;
99
+ readonly worker: Worker;
77
100
  /**
78
101
  * Worker node tasks usage statistics.
79
102
  */
@@ -81,5 +104,5 @@ export interface WorkerNode<Worker extends IWorker, Data = unknown> {
81
104
  /**
82
105
  * Worker node tasks queue.
83
106
  */
84
- tasksQueue: Array<Task<Data>>;
107
+ readonly tasksQueue: Array<Task<Data>>;
85
108
  }
@@ -3,7 +3,7 @@
3
3
  import type { Worker as ClusterWorker } from 'node:cluster';
4
4
  import type { MessagePort } from 'node:worker_threads';
5
5
  import type { KillBehavior } from './worker/worker-options';
6
- import type { IWorker } from './pools/worker';
6
+ import type { IWorker, Task } from './pools/worker';
7
7
  /**
8
8
  * Make all properties in T non-readonly.
9
9
  */
@@ -11,20 +11,13 @@ export type Draft<T> = {
11
11
  -readonly [P in keyof T]?: T[P];
12
12
  };
13
13
  /**
14
- * Message object that is passed between worker and main worker.
14
+ * Message object that is passed between main worker and worker.
15
15
  *
16
16
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
17
17
  * @typeParam MainWorker - Type of main worker.
18
+ * @internal
18
19
  */
19
- export interface MessageValue<Data = unknown, MainWorker extends ClusterWorker | MessagePort | unknown = unknown> {
20
- /**
21
- * Input data that will be passed to the worker.
22
- */
23
- readonly data?: Data;
24
- /**
25
- * Id of the message.
26
- */
27
- readonly id?: string;
20
+ export interface MessageValue<Data = unknown, MainWorker extends ClusterWorker | MessagePort | unknown = unknown> extends Task<Data> {
28
21
  /**
29
22
  * Kill code.
30
23
  */
@@ -18,7 +18,7 @@ export type KillBehavior = keyof typeof KillBehaviors;
18
18
  /**
19
19
  * Detects whether the given value is a kill behavior or not.
20
20
  *
21
- * @typeParam KB - Which specific KillBehavior to test against.
21
+ * @typeParam KB - Which specific KillBehavior type to test against.
22
22
  * @param killBehavior - Which kind of kill behavior to detect.
23
23
  * @param value - Any value.
24
24
  * @returns `true` if `value` was strictly equals to `killBehavior`, otherwise `false`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "poolifier",
3
- "version": "2.4.7",
3
+ "version": "2.4.8",
4
4
  "description": "A fast, easy to use Node.js Worker Thread Pool and Cluster Pool implementation",
5
5
  "license": "MIT",
6
6
  "main": "./lib/index.js",