How to schedule/queue a task based on saved mongodb document data [NodeJS]

Viewed 1662

I want to have a function in my server which a user can create a 'jobScan' from an API route for example '/create-scan-job which basically on the creation of a scanJob it will be saved in the mongodb database with a 'scanDueDate' which will be used to queue a task that will change the status of the scan to 'completed' on the dueDate that was inputted by the user.

How can I build something like this? I have no problem in saving the data in the database but how do I 'queue' it to fire on the dueDate that was given as input?

2 Answers

You can use agenda or bull packages.

  1. Agenda saves jobs in MongoDB database
  2. Bull saves jobs in Redis.

Both support cron jobs.

Agenda is using MongoDB for queue scheduling, but it's a bit unstable, as you may see on the issues page. And you may also use Bree as well, but it does store job queue in-memory.

As for the Bull or BullMQ, that have been notified here, truly it does use Redis, as queue storage. But its docs descriptions don't seem full. And as I remember, Redis doesn't support Windows out-of-the-box. So if you want to run it locally, I highly recommend you use the Redis Docker image for that.

Also, don't forget about GUI, for task-queues. You could use:

  • Agendash, (with Agenda) which allows to create jobs manually from the interface.
  • Bull-board (with Bull or BullMQ) which provides only read-only data about tasks.
Related