duplicate cron jobs for apps in cluster mode
StefanNedelchev opened this issue · comments
I'm running a node app with PM2 with cluster mode. I have set up my PM2 config to run 2 instances of the app and as a result, my scheduled cron jobs run twice (once per instance). Is there any way to avoid this?
I think It is necessary to distinguish between instances. Have you tried this content?
https://anjarulrobin.medium.com/pm2-run-cron-job-from-single-process-in-cluster-mode-35f44ace9e4d
I think It is necessary to distinguish between instances. Have you tried this content? https://anjarulrobin.medium.com/pm2-run-cron-job-from-single-process-in-cluster-mode-35f44ace9e4d
the link doesn't lead to anywhere
See Hexagon/croner#86 for a similar problem with solution
If creating a separated service is an option, you could create a simple cron service in single instance and integrate it with a message-broker like RabbitMQ.
While running using PM2, you should only invoke the cron tasks on a single instance; else it will run on each instance and you will see the same cron running multiple times. The easiest way to resolve this is to conditionally bind the crons like below (am running a 3 node cluster using PM2) using the env variable NODE_APP_INSTANCE
which returns the instance ID from PM2 (instance number starts with 0).
if (process.env.NODE_APP_INSTANCE === "0") {
// run every 30s
cron.schedule("0,30 * * * * *", fn1());
// run every 5 minutes
cron.schedule("*/5 * * * *", fn2());
// run every hour
cron.schedule("5 * * * *", fn3());
// run every 12 hours
cron.schedule("23 0,12 * * *", fn4());
}
While running using PM2, you should only invoke the cron tasks on a single instance; else it will run on each instance and you will see the same cron running multiple times. The easiest way to resolve this is to conditionally bind the crons like below (am running a 3 node cluster using PM2) using the env variable
NODE_APP_INSTANCE
which returns the instance ID from PM2 (instance number starts with 0).if (process.env.NODE_APP_INSTANCE === "0") { // run every 30s cron.schedule("0,30 * * * * *", fn1()); // run every 5 minutes cron.schedule("*/5 * * * *", fn2()); // run every hour cron.schedule("5 * * * *", fn3()); // run every 12 hours cron.schedule("23 0,12 * * *", fn4()); }
Yes, that's the approach I'm currently using. It works fine but my question was more like if there's an official way to do it probided by the library without doing workarounds. For example, the rate limiter library for node has the ability to work with PM2 in cluster mode.