Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
I want to find central point between clusters

I have project which built by NestJs and I want to change my some datas in every 24 hours, I've decided to use CronJob for that. But when I run my project by pm2(on different clusters), CronJob works once for every cluster and totally it works for cluster count (if I have 8 cluster, it works 8 times). How can I find central point which pm2 doesn't separates that and my codes works only one time?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your take on the problem appears to be correct - if you run many homogenous processes, each executing the same cronjob, then the outcomes of the cronjob will obviously be duplicated.

There are several solutions to the problem. The most obvious is to split the cron logic off into another Node.js process, so that there's only 1 cron process running at any given moment. This can then be implemented using a variety of techniques:

  • A host that's designated as "the place where you run cron", and no other host is allowed to run the cron process until the admin switches the host manually.
  • Kubernetes - a control plane already provides you with means to only run 1 copy of a given process.
  • Leader election - a bunch of processes coordinate over a network, typically calling a majority vote on who is going to be "the Cron" for now.

Alternatively, if you have a central database such as PostgreSQL, you can use it as a point of synchronization - you could insert a new row for each cron invocation, and have consumers (the processes that actually execute tasks) read new rows. If a new row is found, it should be locked via FOR UPDATE, which ensures exclusivity. This is a rudimentary form of network locking. Duplicate invocations can be prevented using a primary key, e.g. based on the date - if you know you're only going to have 1 invocation per day, there can only be one entry with the ID being 2021-11-10.

Finally, look online for existing cron implementations. Some "synchronized cron" libraries already exist that tackle this exact issue, such as https://github.com/percolatestudio/meteor-synced-cron - you may be able to use this as inspiration, or as a starting point for your search.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!