@s1seven/cluster-service
v0.15.5
Published
Cluster and workers manager
Downloads
94
Readme
ClusterService
The ClusterService class allows you to easily create a cluster of NestJS (or other Node based) applications.
Installation
$ npm install --save @s1seven/cluster-service
Usage
import { ClusterService, ClusterServiceConfig, WorkerFn } from '@s1seven/cluster-service';
const clusterConfig: ClusterServiceConfig = {
workers: 3,
delay: 200,
grace: 1000,
};
const workerFn: WorkerFn = (opts, disconnect, send) => {
const { workerId } = opts;
setTimeout(() => {
send(`Hello from worker ${workerId}`);
disconnect();
}, 100);
};
const clusterService = new ClusterService(clusterConfig);
clusterService
.on('ready', () => {
clusterService.send('Hello from primary', 1);
})
.on('message', (worker, msg) => {
console.log(worker.id, msg);
});
clusterService
.clusterize(workerFn)
.then((workerId) => {
console.log({ workerId });
})
.catch((e) => {
clusterService.logger.error(e);
process.exit(1);
});