#Single cron task execution with multiple nest app instances

1 messages · Page 1 of 1 (latest)

alpine pendant
#

Hey, I am researching how I can achieve a single cron executor having multiple nest.js instances in AWS EC2. I found a solution with cron-cluster lib and redis leader election, but it looks unmaintained. I am wondering how are you guys solving the problem to execute a task on time only once having multiple instances.

glass sierra
#

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

silk stump
#

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.

glass sierra
#

That's exactly what I do, except I use rabbitmq

fading shore
#

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

eternal dirge
#

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.

#

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

toxic smelt
#

Yes bulljs is the way to go. Don’t elect 1 node process or server to be the leader

silk stump
#

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

eternal dirge
silk stump
#

¯_(ツ)_/¯