Documenation and improvements
[poolifier.git] / README.MD
1 # Node Pool :arrow_double_up: :on:
2 [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
3 [![Dependabot](https://badgen.net/dependabot/dependabot/dependabot-core/?icon=dependabot)](https://badgen.net/dependabot/dependabot/dependabot-core/?icon=dependabot)
4 [![Actions Status](https://github.com/pioardi/node-pool/workflows/NodeCI/badge.svg)](https://github.com/pioardi/node-pool/actions)
5
6 <h2>Contents </h2>
7 <h3 align="center">
8 <a href="#installation">Installation</a>
9 <span> · </span>
10 <a href="#usage">Usage</a>
11 <span> · </span>
12 <a href="#api">API</a>
13 <span> · </span>
14 <a href="#contribute">Contribute</a>
15 <span> · </span>
16 <a href="#nv">Compatibility</a>
17 <span> · </span>
18 <a href="#license">License</a>
19 </h3>
20
21 Node pool contains two <a href="https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads">worker-threads </a> pool implementations , you don' t have to deal with worker-threads complexity. <br>
22 The first implementation is a static thread pool , with a defined number of threads that are started at creation time .<br>
23 The second implementation is a dynamic thread pool with a number of threads started at creation time and other threads created when the load will increase ( with an upper limit ), the new created threads will be stopped after a threshold. <br>
24 You have to implement your worker extending the ThreadWorker class<br>
25 <h2 id="installation">Installation</h2>
26
27 ```
28 npm install node-pool --save
29 ```
30 <h2 id="usage">Usage</h2>
31
32 You can implement a worker in a simple way , extending the class ThreadWorker :
33
34 ```js
35 'use strict'
36 const { ThreadWorker } = require('node-pool')
37
38 class MyWorker extends ThreadWorker {
39 constructor () {
40 super((data) => {
41 // this will be executed in the worker thread,
42 // the data will be received by using the execute method
43 return { ok: 1 }
44 }, { maxInactiveTime: 1000 * 60})
45 }
46 }
47 module.exports = new MyWorker()
48 ```
49
50 Instantiate your pool based on your needed :
51
52 ```js
53 'use strict'
54 const { FixedThreadPool, DynamicThreadPool } = require('node-pool')
55
56 // a fixed thread pool
57 const pool = new FixedThreadPool(15,
58 './yourWorker.js')
59
60 // or a dynamic thread pool
61 const pool = new DynamicThreadPool(10, 100,
62 './yourWorker.js')
63 pool.emitter.on('FullPool', () => console.log('Pool is full'))
64
65 // the execute method signature is the same for both implementations,
66 // so you can easy switch from one to another
67 pool.execute({}).then(res => {
68 console.log(res)
69 }).catch ....
70
71 ```
72
73 <strong> See examples folder for more details.</strong>
74
75 <h2 id="nv">Node versions</h2>
76
77 You can use node version 10.x with --experimental-worker flag, or you can use an higher version (i.e 12.x) <br>
78
79 <h2 id="api">API</h2>
80
81 ### `pool = new FixedThreadPool(numThreads, filePath, opts)`
82 `numThreads` (mandatory) Num of threads for this worker pool <br>
83 `filePath` (mandatory) Path to a file with a worker implementation <br>
84 `opts` (optional) An object with these properties :
85 - `errorHandler` - A function that will listen for error event on each worker thread
86 - `onlineHandler` - A function that will listen for online event on each worker thread
87 - `exitHandler` - A function that will listen for exit event on each worker thread
88 - `maxTasks` - This is just to avoid not useful warnings message, is used to set <a href="https://nodejs.org/dist/latest-v12.x/docs/api/events.html#events_emitter_setmaxlisteners_n">maxListeners</a> on event emitters ( workers are event emitters)
89
90 ### `pool = new DynamicThreadPool(min, max, filePath, opts)`
91 `min` (mandatory) Same as FixedThreadPool numThreads , this number of threads will be always active <br>
92 `max` (mandatory) Max number of workers that this pool can contain, the new created threads will die after a threshold ( default is 1 minute , you can override it in your worker implementation). <br>
93 `filePath` (mandatory) Same as FixedThreadPool <br>
94 `opts` (optional) Same as FixedThreadPool <br>
95
96 ### `pool.execute(data)`
97 Execute method is available on both pool implementations ( return type : Promise): <br>
98 `data` (mandatory) An object that you want to pass to your worker implementation <br>
99
100 ### `pool.destroy()`
101 Destroy method is available on both pool implementations.<br>
102 This method will call the terminate method on each worker.
103
104
105 ### `class YourWorker extends ThreadWorker`
106 `fn` (mandatory) The function that you want to execute on the worker thread <br>
107 `opts` (optional) An object with these properties :
108 - `maxInactiveTime` - Max time to wait tasks to work on ( in ms) , after this period the new worker threads will die.
109
110 <h2 id="contribute">Contribute</h2>
111
112 See guidelines [CONTRIBUTING](./.github/CONTRIBUTING.md)
113
114
115 <h2 id="license">License</h2>
116
117 [MIT](https://github.com/pioardi/node-pool/blob/master/LICENSE)
118