$ npm install https://github.com/4000D/node-cluster-utils.git
implemente nodejs app and run with pm2 cluster mode.
const { withLock } = require("node-cluster-utils");
withLock({
// lockFilePath: "path/to/lock/file",
f: () => {
setInterval(() => {
console.log("hello", process.pid);
}, 1000);
return new Promise(() => {}); // run 1 node instance acquire lock forever
},
});
import { withLock } from "node-cluster-utils";
withLock({
// lockFilePath: "path/to/lock/file",
f: () => {
setInterval(() => {
console.log("hello", process.pid);
}, 1000);
return new Promise<void>(() => {}); // run 1 node instance acquire lock forever
},
});