https://github.com/tysonrm/cluster-rolling-restart
Add rolling restart and cache synchronization to node.js clusters
https://github.com/tysonrm/cluster-rolling-restart
cache-sync cluster-manager rolling-upgrade
Last synced: 3 months ago
JSON representation
Add rolling restart and cache synchronization to node.js clusters
- Host: GitHub
- URL: https://github.com/tysonrm/cluster-rolling-restart
- Owner: tysonrm
- License: mit
- Created: 2021-03-30T12:08:55.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-04-02T06:59:17.000Z (about 4 years ago)
- Last Synced: 2025-02-14T19:31:59.305Z (3 months ago)
- Topics: cache-sync, cluster-manager, rolling-upgrade
- Language: JavaScript
- Homepage:
- Size: 45.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cluster-rolling-restart
A simple way to turn express into a clustered server with support for rolling restart and cache synchronization.
## Install [
](https://www.npmjs.com/package/cluster-rolling-restart)
```shell
npm install cluster-rolling-restart
```## server.js
```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
```shell
> sysctl -n hw.ncpu
8> start
> node server.jsmaster 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
```shell
> curl http://localhost:8080/reload
```## output
```shell
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...
```js
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...
```js
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;
}
}
});
```