Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@s1seven/cluster-service

Package Overview
Dependencies
Maintainers
5
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@s1seven/cluster-service

Cluster and workers manager

latest
Source
npmnpm
Version
0.15.5
Version published
Maintainers
5
Created
Source

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

Keywords

cluster

FAQs

Package last updated on 04 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts