#Single cron task execution with multiple nest app instances
1 messages · Page 1 of 1 (latest)
I think I would keep it simple and create a separate cron microservice that sends a request to the microservice you're currently running cron from
Yea that makes sense.. hell have it make an http request from the cron server heh
If you want resiliency and scalability then you'd want to setup a queueing mechanism with a message broker like AWS SQS and have each of your instances listen to a queue(s) for new instruction messages.
That's exactly what I do, except I use rabbitmq
My team is facing the same issue, and we plan to move to queues, but for now I have one instance of my api running separately, with and ENV var allowing it to run the cron jobs. Ugly but works
Hi! We are doing something similar. In our case, we are working with the built-in module "@nestjs/bull" so we init the cron job adding it to a queue "onApplicationBootstrap". Another service is listening to this job so every time the cron job will execute, this service will do what you want. To see more information: https://docs.nestjs.com/techniques/queues
PS: A job has an option "repeat" when you can indicate the cron specification.
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Progamming), FP (Functional Programming), and FRP (Functional Reactive Programming).
BONUS: "Bull is smart enough not to add the same repeatable job if the repeat options are the same.
If there are no workers running, repeatable jobs will not accumulate next time a worker is online." To see more information: https://docs.bullmq.io/guide/jobs/repeatable
Yes bulljs is the way to go. Don’t elect 1 node process or server to be the leader
Repeatable is used to repeat the job.. not guarantee concurrency.
pattern: 'RRULE:FREQ=SECONDLY;INTERVAL=;WKST=MO',
wth is this syntax .. never used bull .. but that just looks like trouble lol
I've used bull and I'd never seen this syntax either xD There are many other options to get the same result at least, cool
¯_(ツ)_/¯