[Part 2] Executing batch jobs in a multi-container environment using NodeJS and express.

Create a CRON job to be executed at 12 am every day

In this step, we will register a CRON job that executes at 12 am every day. This CRON job will simply console log the time of execution and a static message.

Step 1

Add a new QUEUE_NAME …


This content originally appeared on DEV Community and was authored by DEV Community

Create a CRON job to be executed at 12 am every day

In this step, we will register a CRON job that executes at 12 am every day. This CRON job will simply console log the time of execution and a static message.

Step 1

Add a new QUEUE_NAME called MIDNIGHT_CRON

export const QUEUE_NAMES = {
  SCHEDULE_JOB: 'scheduleJob',
  MIDNIGHT_CRON: 'midnightCron'
};

Step 2

Add a new processor for CRON

const CRON_EXPRESSIONS = {
  MIDNIGHT: '0 0 * * *'
};

export const QUEUE_PROCESSORS = {
   ...,
  [QUEUE_NAMES.MIDNIGHT_CRON]: (job, done) => {
        console.log({ job, done });
    console.log(`${moment()}::The MIDNIGHT_CRON is being executed at 12:00am`);
    done();
  }
};

Step 3

Register the CRON job in the server/utils/queue.js

export const initQueues = () => {
  ...
  queues[QUEUE_NAMES.MIDNIGHT_CRON].add({}, { repeat: { cron: CRON_EXPRESSIONS.MIDNIGHT } });
};

We will invoke the initQueues method from the server/index.js to initialize the queues on startup. After initializing the queues we will add a CRON job to be executed at 12 am.

You should see the below logs at 12 am! Feel free to update the regex and execute the CRON sooner than 12 am to test how it works.

Alt Text

Commit your code using the following git commands

git add .
git commit -m 'Add support to run a CRON job at 12 AM everyday'

Where to go from here

You now have the ability to set up CRON jobs in a multi-container environment.

I hope you enjoyed reading this article as much as I enjoyed writing it. If this peaked your interest stay tuned for the next article in the series where I will take you through how to write GraphQL subscriptions in a multi-container environment using graphql-redis-subscriptions

If you have any questions or comments, please join the forum discussion below.

➤This blog was originally posted on https://wednesday.is To know more about what it’s like to work with Wednesday follow us on: Instagram|Twitter|LinkedIn


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2021-08-19T05:19:30+00:00) [Part 2] Executing batch jobs in a multi-container environment using NodeJS and express.. Retrieved from https://www.scien.cx/2021/08/19/part-2-executing-batch-jobs-in-a-multi-container-environment-using-nodejs-and-express/

MLA
" » [Part 2] Executing batch jobs in a multi-container environment using NodeJS and express.." DEV Community | Sciencx - Thursday August 19, 2021, https://www.scien.cx/2021/08/19/part-2-executing-batch-jobs-in-a-multi-container-environment-using-nodejs-and-express/
HARVARD
DEV Community | Sciencx Thursday August 19, 2021 » [Part 2] Executing batch jobs in a multi-container environment using NodeJS and express.., viewed ,<https://www.scien.cx/2021/08/19/part-2-executing-batch-jobs-in-a-multi-container-environment-using-nodejs-and-express/>
VANCOUVER
DEV Community | Sciencx - » [Part 2] Executing batch jobs in a multi-container environment using NodeJS and express.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/19/part-2-executing-batch-jobs-in-a-multi-container-environment-using-nodejs-and-express/
CHICAGO
" » [Part 2] Executing batch jobs in a multi-container environment using NodeJS and express.." DEV Community | Sciencx - Accessed . https://www.scien.cx/2021/08/19/part-2-executing-batch-jobs-in-a-multi-container-environment-using-nodejs-and-express/
IEEE
" » [Part 2] Executing batch jobs in a multi-container environment using NodeJS and express.." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2021/08/19/part-2-executing-batch-jobs-in-a-multi-container-environment-using-nodejs-and-express/. [Accessed: ]
rf:citation
» [Part 2] Executing batch jobs in a multi-container environment using NodeJS and express. | DEV Community | Sciencx | https://www.scien.cx/2021/08/19/part-2-executing-batch-jobs-in-a-multi-container-environment-using-nodejs-and-express/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.