1

I am running a MEAN stack application and am struggling with proper implementation of a reoccurring task. I have added the following line to my server.js file:

require('./node_scripts/schedule/node_sched.js');  

It then points to this file:

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

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

var notify = schedule.scheduleJob(rule, function(){
    var notifyScript = require('../arrival_checker/arrival_main.js');
    console.log('Ran notify script ' + new Date());
    notifyScript;
});

The script runs one time successfully but it never runs again. Here are my questions:

  1. Is this the propper way to perform a scheduled task in a MEAN stack environment?
  2. How do I need to change this code so it will run every hour?
Matt Smith
  • 553
  • 7
  • 15

1 Answers1

-2

I think the below code will help you. the below code will be executed every 00:01 (midnight).

 var schedule = require('node-schedule');
 schedule.scheduleJob('1 0 * * *', function () {
       console.log('Ran notify script ' + new Date());
 })

You can also create your own rule to give the time of execution:

var yourRule = {hour: 0, minute: 0, dayOfWeek: 1, month: [0, 3, 6, 9]};
var scheduleFunction = schedule.scheduleJob(yourRule, function(){
  console.log('will execute every quarter'));
});

link to make your own rule: https://www.npmjs.com/package/node-schedule

Shubham Verma
  • 8,783
  • 6
  • 58
  • 79