본문 바로가기

express

Excuting some work on same time everyday. [Scheduling]

Scheduled work

Situation

: Need to do some work on same time for every day, In our site, we need to bring upcomming match datas everyday

How to

: use "node-schedule"

What is node-schedule

Node Schedule is for time-based scheduling, not interval-based scheduling.
There is another tool can use for scheduling. That is "setInterval".
"setInterval" is used for example: "run this function every 5 minutes". while if you need run process at exact time like "at the :20 and :50 of every hour on the third Tueseday of every month", "node-schedule" module is better choice for you.

Jobs and Scheduling

Every scheduled job in Node Schedule is represented by a "Job". and You can create jobs manually, then execute the "schedule()"" method to apply a schedule, or use the convenience function "scheduleJob()".

Cron-style Scheduling

* * * * * *
1st * :day of week (0 - 7)
2st * :month (1 -12)
3"    :day of month(1 - 31)
4"    :hour (0 - 23)
5"    :minute(0 - 59)
6"    :second (0 - 59)

Below excutes when the minute is 42(e.g. 19:42, 20:42, etc.).

var j = schedule.scheduleJob('42 * * * *', function() {
    console.log('~');
});

Data-based Scheduleing

Say you want a function to execute at 5:30am on December 21,2012.

var schedule = require('node-schedule');
var date = new Date(2012, 11, 21, 5, 30, 0);

var j = schedule.scheduleJob(date, function() {
    console.log('~');
});

Recurrence Rule Scheduling

When you want to execute function every hour at 42 minutes

var schedule = require('node-schdule');

var rule = new schedule.RecurrenceRule();
rule.minute = 42;

var j = schedule.scheduleJob(rule, function() {
    console.log('~');
})

RecurrenceRule properties

  • second (0-59)
  • minute (0-59)
  • hour (0-23)
  • date (1-31)
  • month (0-11)
  • year
  • dayOfWeek (0-6) Starting with Sunday

Cancle job

j.cancle();

'express' 카테고리의 다른 글

Divide DB by the 'env'  (0) 2020.08.27
What is node.js?[not done]  (0) 2020.07.14
passport-facebook with JWT [Express]  (0) 2020.07.03
body-parser[Middleware]  (0) 2020.07.03
How to test passport-facebook /Social login test [TDD]  (0) 2020.06.28