2

I'm using node-schedule npm package to schedule the job. I referred below link to set the job name/id for different jobs, Cancel node-schedule event after it has been set

It works well when I directly use the cron expression to schedule the job. But it is not working when I used RecurrenceRule() to schedule the job.

Please help me to solve this problem.

Working code snippet for, scheduling job with job id and cron expression,

    // code that works
function test(fn)
{
   let rule = '* * * * *'
   let jobId = "my_event_1"
   schedule.scheduleJob(jobId,rule,()=>{fn()})
}
test(print)

print function,

function print()
{
   console.log("HELLO",new Date())
}

Code that doesn't work with recurrence rule,

// Recurrence rule used 
function my_scheduleJob(id,tz,cron_expression,function_name)
{
 var mxTimezones = "Asia/Kolkata";
 var interval = parser.parseExpression(cron_expression, options);
 var cronDate = interval.next();

 var rule = new schedule.RecurrenceRule();
 rule.second = cronDate.getSeconds();
 rule.minute = cronDate.getMinutes();
 rule.tz = mxTimezones; // You can specify a timezone!

 schedule.scheduleJob(rule,()=>{
   console.log("Scheduler test-------")
   function_name(rule.tz)
 })
}

my_scheduleJob("job1", "Asia/Kolkata", "* * * * *", print)

The reason I used recurrence rule is to set the timezone. Is it possible to use recurrence rule and job id to schedule the job?

Thanks in advance !!!

Shamili
  • 83
  • 3
  • 11

1 Answers1

1

I got the solution for this question after properly converting cron expression to recurrence rule. Hope the below code helps...

// Recurrence rule used 
function my_scheduleJob(id, mxTimezones, cron_expression, function_name) {
    var options = {
        tz: mxTimezones
    };
    var interval = parser.parseExpression(cron_expression, options);
    var cronDate = interval.next();
    var cron_attr = cron_expression.split(' ');
    var rule = new schedule.RecurrenceRule();

    for (var i in cron_attr) {
        var res = '';

        switch (i) {
            case '0':
                res = getRuleValue(cron_attr[i], 0, 59);
                rule.minute = res == '1' ? cronDate.getMinutes() : res;
                break;
            case '1':
                res = getRuleValue(cron_attr[i], 0, 23);
                rule.hour = res == '1' ? cronDate.getHours() : res;
                break;
            case '2':
                res = getRuleValue(cron_attr[i], 1, 31);
                rule.date = res == '1' ? cronDate.getDate() : res;
                break;
            case '3':
                res = getRuleValue(cron_attr[i], 1, 12);
                rule.month = res == '1' ? cronDate.getMonth() : res;
                break;
            case '4':
                res = getRuleValue(cron_attr[i], 0, 6);
                rule.dayOfWeek = res == '1' ? cronDate.getDay() : res;
                break;
        }


    }

    rule.tz = mxTimezones; // You can specify a timezone!

    let jobId = String(id)

    schedule.scheduleJob(jobId, rule, () => {
        console.log("Scheduler test-------")
        function_name();
    })
}

my_scheduleJob("job1", "Asia/Kolkata", "* * * * *", print)


function getRuleValue(value, start_value, end_value) {
    if (/^[*\d][\/][\d]+$/.test(value)) {
        // console.log('value:', /^[\d][\/][\d]+$/.test(value));
        return new schedule.Range(start_value, end_value, parseInt(value.split('/')[1]))
    }
    else if (value == 'MON-FRI') {
        return new schedule.Range(1, 5);
    }
    else if (value == '*') {
        return null;
    }
    else {
        return '1';
    }
}
Shamili
  • 83
  • 3
  • 11