Published on

How to create a scheduler service in node.js ?

Authors

What is a scheduler service ?

Scheduler service provides the ability to execute tasks at specific times or intervals.

How to write Node.js scheduler service ?

You can write a scheduler service mostly in any modern frameworks such as .net core, elixir, node.js, go language etc.

In this example i use node.js. To follow this example you need to have basic knowledge about javascript.

Take the following steps to create basic scheduler service:

1 - Create project folder for your service

mkdir scheduler-service && cd scheduler-service

2 - init npm

npm init -y

3 - install the following packages:

  • node-schedule
  • log4js

i chose node-schedule package for cron-like scheduler functionalitiy and log4js for logging.

4 - Create a javascript file

  • ex: schedule.js

5 - Import node-schedule library and assign a job to scheduleJob function

const schedule = require('node-schedule')

const job = schedule.scheduleJob('*/1 * * * *', function () {
  console.log('every minute you will see this log')
})

as you can see in the example above scheduleJob has 2 parameters:

1 - cron expression

  • Cron expressions are used for initialize a repetitive time for a job. It is a string consisting of six or seven subexpressions (fields) that describe individual details of the schedule.
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    │
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59)

2 - a function

  • this function defines a task that we want to schedule

Complete js code that uses log4js to log:

const log4js = require('log4js')
log4js.configure({
  appenders: { log: { type: 'file', filename: 'schedule.log' } },
  categories: { default: { appenders: ['log'], level: 'info' } },
})

const logger = log4js.getLogger('log')

logger.info('scheduler started.')
const job = schedule.scheduleJob('*/1 * * * *', function () {
  logger.warn('every minute you will see this log')
})

How to write systemd service file?

To register our service to linux service we need to create a systemd service file.

Take the following steps to create systemd service file:

1 - type following command to linux terminal

  • vi /etc/systemd/system/schedule.service

2 - put the following content in it

[Unit]
Description=node service

[Service]
WorkingDirectory=/home/emin/node/
User=ubuntu
ExecStart=/usr/bin/node /home/ubuntu/hello_env.js
Restart=on-failure
RestartSec=10

3 - sudo systemctl daemon-reload

  • You have to do this whenever any of the service files change at all so that system picks up the new info.

4 - sudo systemctl start schedule

5 - sudo systemctl status schedule

  • this command shows the status of a service.

After starting your service you should see a schedule.log file in location of the service.

Resources: