Add support for [cluster settings](https://nodejs.org/api/cluster.html#cluster_cluste...
[poolifier.git] / src / pools / pool.ts
1 import type {
2 ErrorHandler,
3 ExitHandler,
4 MessageHandler,
5 OnlineHandler
6 } from './pool-worker'
7 import type { WorkerChoiceStrategy } from './selection-strategies/selection-strategies-types'
8
9 /**
10 * Options for a poolifier pool.
11 */
12 export interface PoolOptions<Worker> {
13 /**
14 * A function that will listen for message event on each worker.
15 */
16 messageHandler?: MessageHandler<Worker>
17 /**
18 * A function that will listen for error event on each worker.
19 */
20 errorHandler?: ErrorHandler<Worker>
21 /**
22 * A function that will listen for online event on each worker.
23 */
24 onlineHandler?: OnlineHandler<Worker>
25 /**
26 * A function that will listen for exit event on each worker.
27 */
28 exitHandler?: ExitHandler<Worker>
29 /**
30 * The worker choice strategy to use in this pool.
31 */
32 workerChoiceStrategy?: WorkerChoiceStrategy
33 /**
34 * Pool events emission.
35 *
36 * @default true
37 */
38 enableEvents?: boolean
39 }
40
41 /**
42 * Contract definition for a poolifier pool.
43 *
44 * @template Data Type of data sent to the worker. This can only be serializable data.
45 * @template Response Type of response of execution. This can only be serializable data.
46 */
47 export interface IPool<Data = unknown, Response = unknown> {
48 /**
49 * Performs the task specified in the constructor with the data parameter.
50 *
51 * @param data The input for the specified task. This can only be serializable data.
52 * @returns Promise that will be resolved when the task is successfully completed.
53 */
54 execute(data: Data): Promise<Response>
55 /**
56 * Shutdowns every current worker in this pool.
57 */
58 destroy(): Promise<void>
59 /**
60 * Sets the worker choice strategy in this pool.
61 *
62 * @param workerChoiceStrategy The worker choice strategy.
63 */
64 setWorkerChoiceStrategy(workerChoiceStrategy: WorkerChoiceStrategy): void
65 }