Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/iamolegga/workers-cluster
Run cluster of workers with graceful shutdown and autorestarting failed workers.
https://github.com/iamolegga/workers-cluster
autorestart cluster gracefull nodejs shutdown workers
Last synced: 3 months ago
JSON representation
Run cluster of workers with graceful shutdown and autorestarting failed workers.
- Host: GitHub
- URL: https://github.com/iamolegga/workers-cluster
- Owner: iamolegga
- License: mit
- Created: 2018-07-16T10:06:49.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-25T21:36:05.000Z (about 6 years ago)
- Last Synced: 2024-10-05T02:39:14.933Z (4 months ago)
- Topics: autorestart, cluster, gracefull, nodejs, shutdown, workers
- Language: TypeScript
- Homepage:
- Size: 36.1 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# workers-cluster
**Run cluster of workers with graceful shutdown and autorestarting failed workers.**
## Example
```js
/**
* index.js
*
* In your entrypoint index.js file you should call
* `startCluster` function with one argument - object, where
* keys - are full paths to workers,
* and values - are count of worker instances (child processes).
* `startCluster` returns Promise, that will be fulfilled
* only in master process and only when all workers instances
* will be started
*/const { startCluster } = require('workers-cluster');
const pathToWorkerA = `${__dirname}/workerA.js`;
const workerAInstances = 10;const pathToWorkerB = `${__dirname}/workerB.js`;
const workerBInstances = 1;const workers = {
[pathToWorkerA]: workerAInstances,
[pathToWorkerB]: workerBInstances,
};
startCluster(workers).then(() => {
console.log('All workers have been started')
});/**
* workerA.js / workerB.js
*
* Worker module MUST export `start` and `close` methods.
* Both of them must return Promise.
*/let interval
exports.start = async () => {
interval = setInterval(() => {
// this will break worker's process
// but worker will be restarted automatically
if (Math.random() < .1) {
throw new Error('Ooops');
}
console.log(Date.now());
}, 1000);
};
exports.close = () => {
clearInterval(interval);
return Promise.resolve();
};
```