Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Using cron-node, where did I go wrong?

Using cron-node, where did I go wrong?

Problem

I need to update a file at regular interval, and the file will give me other scheduled task.

The only issue I have is in using Cron-node

Here the test code (real code, narrowed to the cron issue, and with some test line):

//Variable of the dayly update hour, with default value
var UPDATE_H=6,
    UPDATE_M=30;

function start(){
    console.log('start');

    //test_
    //Set the cron job at the next two minute following the start of the app
    time=new Date();
    time.setMinutes(time.getMinutes()+2);
    UPDATE_H=time.getHours();
    UPDATE_M=time.getMinutes();
    //_test

    //Start the cron job
    smil_update(UPDATE_H, UPDATE_M);

    //test_
    //Print a dot every minutes
    setInterval(function(){console.log('.');}, 60000);
    //_test

    player();
}

function smil_update(hour, min){
    var cronJob=require('cron').CronJob,
    when='',
    //test_
    time=new Date();
    //_test

    //Creating cron like date struct (cron like because seconds count too here)
    when='00 '+min+' '+hour+' * * *';

    console.log('Will work at:'+when);
    //test_
    console.log('It is '+time.getHours()+' '+time.getMinutes());
    //_test

    var job=new cronJob(when, timeZone='Europe/Paris', function(){
        console.log('UPDATE');
        player();
    });
    job.start();
}

function player(){
    console.log('Player');
}

start();

And I get:

start
Will work at:00 13 17 * * *
It is 17 11
Player
.
.
.

It seems I missed a point somewhere, but even after rereading the docs, I don t get where I did wrong.

Problem courtesy of: DrakaSAN

Solution

I got it.

The mistake was in the when variable.

Instead of

when='00 '+...

It must be

when='0 '+...

Seems that cron don t recognize 00 as 0, for some unknown reason.

Solution courtesy of: DrakaSAN

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Using cron-node, where did I go wrong?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×