Make scalable Cron jobs in Node js

npm i --save node-schedule dotenv
const myCronJob = async () => {
try {
console.info("myCronJob Start")
/* perform your async operation *
await asyncOperation(2000)
console.info("myCronJob End")}
catch (err) {console.error(`Func.cronJobFunc: ${err}`);}
}
const asyncOperation = (ms)=> new Promise(resolve => setTimeout(resolve, ms));

Cron Expression -

import { scheduleJob } from “node-schedule”;
scheduleJob(‘0 8 * * *’, () => myCronJob);
import { scheduleJob } from “node-schedule”;
require(“dotenv”).config();
scheduleJob(process.env.MY_CRON_EXP, () => myCronJob);

Start Script -

node server.js

Output -

myCronJob Start
myCronJob End

--

--

--

Use code to make beautiful things “

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A BASIC INTRODUCTORY GUIDE TO JAVASCRIPT IN WEB DEVELOPMENT [ Episode I]

JavaScript Tricks

7 Advantages of Using VueJS

What I love about Tailwindcss

Creating a simple API using Node.js, Express and MongoDB and Testing using Postman

Angular — Understanding pipes and Different ways to use them

How to add Icons to your Web Pages

Promise the “Hello, world!” and Await the Response

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Roysukrit

Roysukrit

Use code to make beautiful things “

More from Medium

Advantages and Disadvantages of Node.js: Why to Use Node.js?

Node.js and Express: A Lightweight Minimalist Framework

Integrating Redis into your Nodejs web app

Rate Limiting Calls to External API with Node.js