Revert "chore: generate documentation"
[poolifier.git] / docs / api.md
CommitLineData
a47027a0
JB
1# [API](https://poolifier.github.io/poolifier/)
2
3## Table of contents
4
5- [Pool](#pool)
6 - [`pool = new FixedThreadPool/FixedClusterPool(numberOfThreads/numberOfWorkers, filePath, opts)`](#pool--new-fixedthreadpoolfixedclusterpoolnumberofthreadsnumberofworkers-filepath-opts)
7 - [`pool = new DynamicThreadPool/DynamicClusterPool(min, max, filePath, opts)`](#pool--new-dynamicthreadpooldynamicclusterpoolmin-max-filepath-opts)
8 - [`pool.execute(data, name)`](#poolexecutedata-name)
9 - [`pool.destroy()`](#pooldestroy)
10 - [`PoolOptions`](#pooloptions)
11 - [`ThreadPoolOptions extends PoolOptions`](#threadpooloptions-extends-pooloptions)
12 - [`ClusterPoolOptions extends PoolOptions`](#clusterpooloptions-extends-pooloptions)
13- [Worker](#worker)
14 - [`class YourWorker extends ThreadWorker/ClusterWorker`](#class-yourworker-extends-threadworkerclusterworker)
15 - [`YourWorker.hasTaskFunction(name)`](#yourworkerhastaskfunctionname)
16 - [`YourWorker.addTaskFunction(name, fn)`](#yourworkeraddtaskfunctionname-fn)
17 - [`YourWorker.removeTaskFunction(name)`](#yourworkerremovetaskfunctionname)
18 - [`YourWorker.listTaskFunctions()`](#yourworkerlisttaskfunctions)
19 - [`YourWorker.setDefaultTaskFunction(name)`](#yourworkersetdefaulttaskfunctionname)
20
21## Pool
22
23### `pool = new FixedThreadPool/FixedClusterPool(numberOfThreads/numberOfWorkers, filePath, opts)`
24
25`numberOfThreads/numberOfWorkers` (mandatory) Number of workers for this pool
26`filePath` (mandatory) Path to a file with a worker implementation
27`opts` (optional) An object with the pool options properties described below
28
29### `pool = new DynamicThreadPool/DynamicClusterPool(min, max, filePath, opts)`
30
31`min` (mandatory) Same as _FixedThreadPool_/_FixedClusterPool_ numberOfThreads/numberOfWorkers, this number of workers will be always active
32`max` (mandatory) Max number of workers that this pool can contain, the new created workers will die after a threshold (default is 1 minute, you can override it in your worker implementation).
33`filePath` (mandatory) Path to a file with a worker implementation
34`opts` (optional) An object with the pool options properties described below
35
36### `pool.execute(data, name)`
37
38`data` (optional) An object that you want to pass to your worker implementation
39`name` (optional) A string with the task function name that you want to execute on the worker. Default: `'default'`
40
41This method is available on both pool implementations and returns a promise with the task function execution response.
42
43### `pool.destroy()`
44
45This method is available on both pool implementations and will call the terminate method on each worker.
46
47### `PoolOptions`
48
49An object with these properties:
50
51- `messageHandler` (optional) - A function that will listen for message event on each worker
52- `errorHandler` (optional) - A function that will listen for error event on each worker
53- `onlineHandler` (optional) - A function that will listen for online event on each worker
54- `exitHandler` (optional) - A function that will listen for exit event on each worker
55- `workerChoiceStrategy` (optional) - The worker choice strategy to use in this pool:
56
57 - `WorkerChoiceStrategies.ROUND_ROBIN`: Submit tasks to worker in a round robin fashion
58 - `WorkerChoiceStrategies.LEAST_USED`: Submit tasks to the worker with the minimum number of executed, executing and queued tasks
59 - `WorkerChoiceStrategies.LEAST_BUSY`: Submit tasks to the worker with the minimum tasks total execution and wait time
60 - `WorkerChoiceStrategies.LEAST_ELU`: Submit tasks to the worker with the minimum event loop utilization (ELU) (experimental)
61 - `WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN`: Submit tasks to worker by using a [weighted round robin scheduling algorithm](./src/pools/selection-strategies/README.md#weighted-round-robin) based on tasks execution time
62 - `WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN`: Submit tasks to worker by using an [interleaved weighted round robin scheduling algorithm](./src/pools/selection-strategies/README.md#interleaved-weighted-round-robin) based on tasks execution time (experimental)
63 - `WorkerChoiceStrategies.FAIR_SHARE`: Submit tasks to worker by using a [fair share scheduling algorithm](./src/pools/selection-strategies/README.md#fair-share) based on tasks execution time (the default) or ELU active time
64
65 `WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN`, `WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN` and `WorkerChoiceStrategies.FAIR_SHARE` strategies are targeted to heavy and long tasks.
66 Default: `WorkerChoiceStrategies.ROUND_ROBIN`
67
68- `workerChoiceStrategyOptions` (optional) - The worker choice strategy options object to use in this pool.
69 Properties:
70
71 - `measurement` (optional) - The measurement to use in worker choice strategies: `runTime`, `waitTime` or `elu`.
72 - `runTime` (optional) - Use the tasks [median](./src/pools/selection-strategies/README.md#median) runtime instead of the tasks average runtime in worker choice strategies.
73 - `waitTime` (optional) - Use the tasks [median](./src/pools/selection-strategies/README.md#median) wait time instead of the tasks average wait time in worker choice strategies.
74 - `elu` (optional) - Use the tasks [median](./src/pools/selection-strategies/README.md#median) ELU instead of the tasks average ELU in worker choice strategies.
75 - `weights` (optional) - The worker weights to use in weighted round robin worker choice strategies: `{ 0: 200, 1: 300, ..., n: 100 }`.
76
77 Default: `{ runTime: { median: false }, waitTime: { median: false }, elu: { median: false } }`
78
79- `restartWorkerOnError` (optional) - Restart worker on uncaught error in this pool.
80 Default: `true`
81- `enableEvents` (optional) - Events emission enablement in this pool.
82 Default: `true`
83- `enableTasksQueue` (optional) - Tasks queue per worker enablement in this pool.
84 Default: `false`
85
86- `tasksQueueOptions` (optional) - The worker tasks queue options object to use in this pool.
87 Properties:
88
89 - `concurrency` (optional) - The maximum number of tasks that can be executed concurrently on a worker.
90
91 Default: `{ concurrency: 1 }`
92
93#### `ThreadPoolOptions extends PoolOptions`
94
95- `workerOptions` (optional) - An object with the worker options to pass to worker. See [worker_threads](https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options) for more details.
96
97#### `ClusterPoolOptions extends PoolOptions`
98
99- `env` (optional) - An object with the environment variables to pass to worker. See [cluster](https://nodejs.org/api/cluster.html#cluster_cluster_fork_env) for more details.
100
101- `settings` (optional) - An object with the cluster settings. See [cluster](https://nodejs.org/api/cluster.html#cluster_cluster_settings) for more details.
102
103## Worker
104
105### `class YourWorker extends ThreadWorker/ClusterWorker`
106
107`taskFunctions` (mandatory) The task function or task functions object `{ name_1: fn_1, ..., name_n: fn_n }` that you want to execute on the worker
108`opts` (optional) An object with these properties:
109
110- `maxInactiveTime` (optional) - Maximum waiting time in milliseconds for tasks on newly created workers. After this time newly created workers will die.
111 The last active time of your worker will be updated when it terminates a task.
112 If `killBehavior` is set to `KillBehaviors.HARD` this value represents also the timeout for the tasks that you submit to the pool, when this timeout expires your tasks is interrupted before completion and removed. The worker is killed if is not part of the minimum size of the pool.
113 If `killBehavior` is set to `KillBehaviors.SOFT` your tasks have no timeout and your workers will not be terminated until your task is completed.
114 Default: `60000`
115
116- `killBehavior` (optional) - Dictates if your worker will be deleted in case a task is active on it.
117 **KillBehaviors.SOFT**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker **won't** be deleted.
118 **KillBehaviors.HARD**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker will be deleted.
119 This option only apply to the newly created workers.
120 Default: `KillBehaviors.SOFT`
121
122#### `YourWorker.hasTaskFunction(name)`
123
124`name` (mandatory) The task function name
125
126This method is available on both worker implementations and returns a boolean.
127
128#### `YourWorker.addTaskFunction(name, fn)`
129
130`name` (mandatory) The task function name
131`fn` (mandatory) The task function
132
133This method is available on both worker implementations and returns a boolean.
134
135#### `YourWorker.removeTaskFunction(name)`
136
137`name` (mandatory) The task function name
138
139This method is available on both worker implementations and returns a boolean.
140
141#### `YourWorker.listTaskFunctions()`
142
143This method is available on both worker implementations and returns an array of the task function names.
144
145#### `YourWorker.setDefaultTaskFunction(name)`
146
147`name` (mandatory) The task function name
148
149This method is available on both worker implementations and returns a boolean.