Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda