@s1seven/cluster-service
TypeScript icon, indicating that this package has built-in type declarations

0.15.5 • Public • Published

ClusterService

npm

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);
  });

Package Sidebar

Install

npm i @s1seven/cluster-service

Weekly Downloads

2

Version

0.15.5

License

ISC

Unpacked Size

57.6 kB

Total Files

10

Last publish

Collaborators

  • christophbuehler
  • eamon0989
  • s1seven-service
  • stiebitzhofer