Commit | Line | Data |
---|---|---|
fc3e6586 | 1 | import EventEmitter from 'node:events' |
bdaf31cd JB |
2 | import type { |
3 | ErrorHandler, | |
4 | ExitHandler, | |
50e66724 | 5 | IWorker, |
bdaf31cd JB |
6 | MessageHandler, |
7 | OnlineHandler | |
f06e48d8 | 8 | } from './worker' |
da309861 JB |
9 | import type { |
10 | WorkerChoiceStrategy, | |
11 | WorkerChoiceStrategyOptions | |
12 | } from './selection-strategies/selection-strategies-types' | |
bdaf31cd | 13 | |
b4904890 JB |
14 | /** |
15 | * Pool events emitter. | |
16 | */ | |
17 | export class PoolEmitter extends EventEmitter {} | |
18 | ||
aee46736 JB |
19 | /** |
20 | * Enumeration of pool events. | |
21 | */ | |
22 | export const PoolEvents = Object.freeze({ | |
23 | full: 'full', | |
24 | busy: 'busy' | |
25 | } as const) | |
26 | ||
27 | /** | |
28 | * Pool event. | |
29 | */ | |
30 | export type PoolEvent = keyof typeof PoolEvents | |
31 | ||
7171d33f JB |
32 | /** |
33 | * Worker tasks queue options. | |
34 | */ | |
35 | export interface TasksQueueOptions { | |
36 | /** | |
37 | * Maximum number of tasks that can be executed concurrently on a worker. | |
38 | * | |
39 | * @defaultValue 1 | |
40 | */ | |
41 | concurrency?: number | |
42 | } | |
43 | ||
bdaf31cd JB |
44 | /** |
45 | * Options for a poolifier pool. | |
46 | */ | |
50e66724 | 47 | export interface PoolOptions<Worker extends IWorker> { |
bdaf31cd JB |
48 | /** |
49 | * A function that will listen for message event on each worker. | |
50 | */ | |
51 | messageHandler?: MessageHandler<Worker> | |
52 | /** | |
53 | * A function that will listen for error event on each worker. | |
54 | */ | |
55 | errorHandler?: ErrorHandler<Worker> | |
56 | /** | |
57 | * A function that will listen for online event on each worker. | |
58 | */ | |
59 | onlineHandler?: OnlineHandler<Worker> | |
60 | /** | |
61 | * A function that will listen for exit event on each worker. | |
62 | */ | |
63 | exitHandler?: ExitHandler<Worker> | |
64 | /** | |
46e857ca | 65 | * The worker choice strategy to use in this pool. |
bdaf31cd JB |
66 | */ |
67 | workerChoiceStrategy?: WorkerChoiceStrategy | |
da309861 JB |
68 | /** |
69 | * The worker choice strategy options. | |
70 | */ | |
71 | workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions | |
bdaf31cd JB |
72 | /** |
73 | * Pool events emission. | |
74 | * | |
38e795c1 | 75 | * @defaultValue true |
bdaf31cd JB |
76 | */ |
77 | enableEvents?: boolean | |
ff733df7 JB |
78 | /** |
79 | * Pool worker tasks queue. | |
80 | * | |
81 | * @experimental | |
82 | * @defaultValue false | |
83 | */ | |
84 | enableTasksQueue?: boolean | |
7171d33f JB |
85 | /** |
86 | * Pool worker tasks queue options. | |
87 | * | |
88 | * @experimental | |
89 | * @defaultValue \{ concurrency: 1 \} | |
90 | */ | |
91 | tasksQueueOptions?: TasksQueueOptions | |
bdaf31cd | 92 | } |
a35560ba | 93 | |
729c563d S |
94 | /** |
95 | * Contract definition for a poolifier pool. | |
96 | * | |
38e795c1 JB |
97 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. |
98 | * @typeParam Response - Type of response of execution. This can only be serializable data. | |
729c563d | 99 | */ |
d3c8a1a8 | 100 | export interface IPool<Data = unknown, Response = unknown> { |
b4904890 JB |
101 | /** |
102 | * Emitter on which events can be listened to. | |
103 | * | |
104 | * Events that can currently be listened to: | |
105 | * | |
164d950a JB |
106 | * - `'full'`: Emitted when the pool is dynamic and full. |
107 | * - `'busy'`: Emitted when the pool is busy. | |
b4904890 JB |
108 | */ |
109 | readonly emitter?: PoolEmitter | |
729c563d | 110 | /** |
bdede008 | 111 | * Performs the task specified in the constructor with the data parameter. |
729c563d | 112 | * |
38e795c1 | 113 | * @param data - The input for the specified task. This can only be serializable data. |
729c563d S |
114 | * @returns Promise that will be resolved when the task is successfully completed. |
115 | */ | |
78cea37e | 116 | execute: (data: Data) => Promise<Response> |
280c2a77 | 117 | /** |
675bb809 | 118 | * Shutdowns every current worker in this pool. |
280c2a77 | 119 | */ |
78cea37e | 120 | destroy: () => Promise<void> |
a35560ba | 121 | /** |
bdede008 | 122 | * Sets the worker choice strategy in this pool. |
a35560ba | 123 | * |
38e795c1 | 124 | * @param workerChoiceStrategy - The worker choice strategy. |
a35560ba | 125 | */ |
78cea37e | 126 | setWorkerChoiceStrategy: (workerChoiceStrategy: WorkerChoiceStrategy) => void |
c97c7edb | 127 | } |