Merge branch 'master' of github.com:poolifier/poolifier
[poolifier.git] / src / pools / pool.ts
CommitLineData
fc3e6586 1import EventEmitter from 'node:events'
bdaf31cd
JB
2import type {
3 ErrorHandler,
4 ExitHandler,
50e66724 5 IWorker,
bdaf31cd
JB
6 MessageHandler,
7 OnlineHandler
f06e48d8 8} from './worker'
da309861
JB
9import type {
10 WorkerChoiceStrategy,
11 WorkerChoiceStrategyOptions
12} from './selection-strategies/selection-strategies-types'
bdaf31cd 13
b4904890
JB
14/**
15 * Pool events emitter.
16 */
17export class PoolEmitter extends EventEmitter {}
18
aee46736
JB
19/**
20 * Enumeration of pool events.
21 */
22export const PoolEvents = Object.freeze({
23 full: 'full',
24 busy: 'busy'
25} as const)
26
27/**
28 * Pool event.
29 */
30export type PoolEvent = keyof typeof PoolEvents
31
7171d33f
JB
32/**
33 * Worker tasks queue options.
34 */
35export 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 47export 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
7171d33f
JB
89 */
90 tasksQueueOptions?: TasksQueueOptions
bdaf31cd 91}
a35560ba 92
729c563d
S
93/**
94 * Contract definition for a poolifier pool.
95 *
38e795c1
JB
96 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
97 * @typeParam Response - Type of response of execution. This can only be serializable data.
729c563d 98 */
d3c8a1a8 99export interface IPool<Data = unknown, Response = unknown> {
b4904890
JB
100 /**
101 * Emitter on which events can be listened to.
102 *
103 * Events that can currently be listened to:
104 *
164d950a
JB
105 * - `'full'`: Emitted when the pool is dynamic and full.
106 * - `'busy'`: Emitted when the pool is busy.
b4904890
JB
107 */
108 readonly emitter?: PoolEmitter
729c563d 109 /**
bdede008 110 * Performs the task specified in the constructor with the data parameter.
729c563d 111 *
38e795c1 112 * @param data - The input for the specified task. This can only be serializable data.
729c563d
S
113 * @returns Promise that will be resolved when the task is successfully completed.
114 */
78cea37e 115 execute: (data: Data) => Promise<Response>
280c2a77 116 /**
675bb809 117 * Shutdowns every current worker in this pool.
280c2a77 118 */
78cea37e 119 destroy: () => Promise<void>
a35560ba 120 /**
bdede008 121 * Sets the worker choice strategy in this pool.
a35560ba 122 *
38e795c1 123 * @param workerChoiceStrategy - The worker choice strategy.
a35560ba 124 */
78cea37e 125 setWorkerChoiceStrategy: (workerChoiceStrategy: WorkerChoiceStrategy) => void
c97c7edb 126}