BeardMagician
BeardMagician

Reputation: 657

Cron job on NodeJS server runs multiple times simultaneously due to load balancers

I have cron job services on my nodeJS server (part of a React app) that I deploy using Convox to AWS, which has 4 load balancer servers. This means my cron job runs 4 times simultaneously on each server, when I only want it to run once. How can I stop this from happening and have my cron jobs run only once? As far as I know, there is no reliable way to lock my cron to a specific instance, since instances are volatile and may be deleted/recreated as needed.

The cron job services conduct tasks such as querying and updating our database, sending out emails and texts to users, and conducting external API calls. The services are run using the cron npm package, upon the server starting (after server.listen).

Upvotes: 4

Views: 6313

Answers (2)

podeig
podeig

Reputation: 2741

I had the same issue. Se my solution here. Two emails was sent because of two instances on AWS. I lock each sending by unique random number. My example based on MongoDB.

https://forums.meteor.com/t/help-email-sends-emails-twice/50624

Upvotes: 0

Rodrigo Murillo
Rodrigo Murillo

Reputation: 13648

Can you expose these tasks via url? That way you can have an external cron service that requests each job via url against the ELB.

See https://cron-job.org/en/

Another advantage of this approach is you get error reports if a url does not return a 200 status. This could simplify error tracking across all jobs.

Also this provides better redudency and load balancing, as opposed to having a single instance where you run all jobs.

Upvotes: 6

Related Questions