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