Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cluster-rolling-restart

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cluster-rolling-restart

A simple way to turn express into a clustered server with support for rolling restart and cache synchronization.

  • 1.0.12
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

cluster-rolling-restart

A simple way to turn express into a clustered server with support for rolling restart and cache synchronization.

Install

npm install cluster-rolling-restart

server.js

const cluster = require("cluster-rolling-restart");
const express = require("express");
const app = express();

app.get("/", (req, res) => res.send(`I'm pid ${process.pid}`));
app.get("/reload", (req, res) => process.send({ cmd: "reload" }));
app.get("/reload-reset", (req, res) => process.send({ cmd: "reload-reset" }));

cluster.startCluster(() => app.listen(8080));

output

> sysctl -n hw.ncpu
8

> start
> node server.js

master starting 8 workers 🌎
worker up 77653
worker up 77655
worker up 77652
worker up 77654
worker up 77658
worker up 77657
worker up 77656
worker up 77659

request restart

> curl http://localhost:8080/reload

output

reload requested 👍
worker down 77659
worker up 77662
worker down 77658
worker up 77663
worker down 77657
worker up 77664
worker down 77656
worker up 77665
worker down 77655
worker up 77666
worker down 77654
worker up 77667
worker down 77653
worker up 77668
worker down 77652
worker up 77669
reload complete ✅

Using Cache Synchronization

To use cache synchronization, you send the data you want to cache to the master process. You then listen for events from the master, and when a saveCommand event arrives, you update your cache with the data in the event.

Send data to save to master...

 async save(id, data) {
    if (clusterEnabled) {
      process.send({
        cmd: "saveBroadcast",
        pid: process.pid,
        id,
        data,
        name: this.name,
      });
    }
    return this.dataSource.set(id, data).get(id);
  }

Listen for save event and update cache...

process.on("message", ({ cmd, id, pid, data, name }) => {
  if (cmd && id && data && process.pid !== pid) {
    if (cmd === "saveCommand") {
      const ds = DataSourceFactory.getDataSource(name);
      ds.clusterSave(id, ModelFactory.loadModel(observer, ds, data, name));
      return;
    }
    if (cmd === "deleteCommand") {
      const ds = DataSourceFactory.getDataSource(name);
      ds.clusterDelete(id);
      return;
    }
  }
});

Keywords

FAQs

Package last updated on 02 Apr 2021

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc