How to maintain node-cron in my NestJS project when setting more replicas

Hi! I have a project deployed in railway in NestJS and it has some cron jobs on it. I was just wondering once I scale in more replicas in a future, how should I treat those cronjobs? Is there any way I can know for example the replica number in an env variable so I can only run the cron on one replica? There's any walkaround rather than doing another service? Thank you πŸ™‚
8 Replies
Percy
Percyβ€’5mo ago
Project ID: N/A
Percy
Percyβ€’5mo ago
Project ID: N/A
alfonsotrocoli
alfonsotrocoliβ€’5mo ago
N/A
Brody
Brodyβ€’5mo ago
a separate single service that runs the cron job is the way to go
alfonsotrocoli
alfonsotrocoliβ€’5mo ago
Hmm I guess then if there are some shared modules the only way is to use nestJS microservices in a monorepo or smth like this.. The env thing sound easier haha
Brody
Brodyβ€’5mo ago
maybe that way you could even use railways cron scheduler so that there would be zero resources when the task isn't running
alfonsotrocoli
alfonsotrocoliβ€’5mo ago
Hmm maybe its a worst option, but for the beginning until separating the services I could have a cron that just calls an endpoint in my service and then it would be done in one of the replicas. Pretty ugly but until we separate services could work
Brody
Brodyβ€’5mo ago
well separating the cron service out if definitely the best way to go about this problem!