Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

duplicate cron jobs for apps in cluster mode #393

Open
StefanNedelchev opened this issue Jan 7, 2023 · 6 comments
Open

duplicate cron jobs for apps in cluster mode #393

StefanNedelchev opened this issue Jan 7, 2023 · 6 comments

Comments

@StefanNedelchev
Copy link

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?

@hhong0326
Copy link

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

@StefanNedelchev
Copy link
Author

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

@Hexagon
Copy link

Hexagon commented Mar 5, 2023

See Hexagon/croner#86 for a similar problem with solution

@gitnlsn
Copy link

gitnlsn commented Mar 26, 2023

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.

@ramit-mitra
Copy link

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());
}

@StefanNedelchev
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants