0.15.5 • Published 8 months ago

@s1seven/cluster-service v0.15.5

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

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

8 months ago

0.15.4

1 year ago