{"id":19384564,"url":"https://github.com/tysonrm/cluster-rolling-restart","last_synced_at":"2025-06-17T11:07:36.729Z","repository":{"id":57200990,"uuid":"352987563","full_name":"tysonrm/cluster-rolling-restart","owner":"tysonrm","description":"Add rolling restart and cache synchronization to node.js clusters","archived":false,"fork":false,"pushed_at":"2021-04-02T06:59:17.000Z","size":47,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-14T19:31:59.305Z","etag":null,"topics":["cache-sync","cluster-manager","rolling-upgrade"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tysonrm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-30T12:08:55.000Z","updated_at":"2022-03-09T13:35:45.000Z","dependencies_parsed_at":"2022-09-16T15:12:07.532Z","dependency_job_id":null,"html_url":"https://github.com/tysonrm/cluster-rolling-restart","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tysonrm%2Fcluster-rolling-restart","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tysonrm%2Fcluster-rolling-restart/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tysonrm%2Fcluster-rolling-restart/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tysonrm%2Fcluster-rolling-restart/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tysonrm","download_url":"https://codeload.github.com/tysonrm/cluster-rolling-restart/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240522813,"owners_count":19814980,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache-sync","cluster-manager","rolling-upgrade"],"created_at":"2024-11-10T09:31:18.855Z","updated_at":"2025-02-24T17:26:06.996Z","avatar_url":"https://github.com/tysonrm.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cluster-rolling-restart\n\nA simple way to turn express into a clustered server with support for rolling restart and cache synchronization.\n\n## Install [\u003cimg src=\"https://github.com/tysonrm/cluster-rolling-restart/blob/main/npm-tile.png\"\u003e](https://www.npmjs.com/package/cluster-rolling-restart)\n\n```shell\nnpm install cluster-rolling-restart\n```\n\n## server.js\n\n```js\nconst cluster = require(\"cluster-rolling-restart\");\nconst express = require(\"express\");\nconst app = express();\n\napp.get(\"/\", (req, res) =\u003e res.send(`I'm pid ${process.pid}`));\napp.get(\"/reload\", (req, res) =\u003e process.send({ cmd: \"reload\" }));\napp.get(\"/reload-reset\", (req, res) =\u003e process.send({ cmd: \"reload-reset\" }));\n\ncluster.startCluster(() =\u003e app.listen(8080));\n```\n\n## output\n\n```shell\n\u003e sysctl -n hw.ncpu\n8\n\n\u003e start\n\u003e node server.js\n\nmaster starting 8 workers 🌎\nworker up 77653\nworker up 77655\nworker up 77652\nworker up 77654\nworker up 77658\nworker up 77657\nworker up 77656\nworker up 77659\n```\n\n## request restart\n\n```shell\n\u003e curl http://localhost:8080/reload\n```\n\n## output\n\n```shell\nreload requested 👍\nworker down 77659\nworker up 77662\nworker down 77658\nworker up 77663\nworker down 77657\nworker up 77664\nworker down 77656\nworker up 77665\nworker down 77655\nworker up 77666\nworker down 77654\nworker up 77667\nworker down 77653\nworker up 77668\nworker down 77652\nworker up 77669\nreload complete ✅\n```\n\n## Using Cache Synchronization\n\nTo 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.\n\nSend data to save to master...\n\n```js\n async save(id, data) {\n    if (clusterEnabled) {\n      process.send({\n        cmd: \"saveBroadcast\",\n        pid: process.pid,\n        id,\n        data,\n        name: this.name,\n      });\n    }\n    return this.dataSource.set(id, data).get(id);\n  }\n```\n\nListen for save event and update cache...\n\n```js\nprocess.on(\"message\", ({ cmd, id, pid, data, name }) =\u003e {\n  if (cmd \u0026\u0026 id \u0026\u0026 data \u0026\u0026 process.pid !== pid) {\n    if (cmd === \"saveCommand\") {\n      const ds = DataSourceFactory.getDataSource(name);\n      ds.clusterSave(id, ModelFactory.loadModel(observer, ds, data, name));\n      return;\n    }\n    if (cmd === \"deleteCommand\") {\n      const ds = DataSourceFactory.getDataSource(name);\n      ds.clusterDelete(id);\n      return;\n    }\n  }\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftysonrm%2Fcluster-rolling-restart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftysonrm%2Fcluster-rolling-restart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftysonrm%2Fcluster-rolling-restart/lists"}