0

I am attempting to write a test-script, as part of a larger project which will allow me to schedule future text message alerts. In my script, I try to use both node packages cron and node-schedule. I configure a date object set to three minutes in the future. I use both packages to schedule the job. However, the messages are not sent at the right time, but are sent immediately.

I want my code to execute at a set time in the future, does anyone know why the following code would not work?

var CronJob = require('cron').CronJob;
var twilSms = require('./TwilSms');
var schedule = require('node-schedule');

var sendInThree = function(to,message) {
    var threeMinutes = 180000;
    var inThreeMinutes = new Date(Date.now()+threeMinutes);
    process.stdout.write("A text message should be sent at: \n" + inThreeMinutes.toString() + '\n');

    //TRIGGERS INSTANTLY
    var sched = schedule.scheduleJob(inThreeMinutes,twilSms.sendSms(to,
                   ('Did this message arrive at: ' + inThreeMinutes.toString() + '?')));


    //TRIGGERS INSTANTLY
    var cronSMS = new CronJob(inThreeMinutes,
                twilSms.sendSms(to,
                    ('Did this message arrive at: ' + inThreeMinutes.toString() + '?')),
                null, true);
}


sendInThree('13125555555');

NOTES:

function twilSms.sendSms() workes as expected in the Node REPL.
I used a fake number only for uploading to SO.

Thanks for any help.

1 Answer 1

4

You should pass in a callable, e.g. an anonymous function as the second parameter to CronJob constructor. In your code, you are passing in the return value of twilSms.sendSms() in which case sendSms() executes immediately (because it's called by the () at the end). It doesn't matter that a function call is a parameter to another function, it executes immediately when the interpreter processes that line.

var cronSMS = new CronJob(inThreeMinutes, function() {
                twilSms.sendSms(to,
                    ('Did this message arrive at: ' + inThreeMinutes.toString() + '?')) },
                null, true);

Update 1

If you don't want to use an anonymous function when specifying the parameters, you can assign a function to a named variable - but it still has to be a callable as CronJob expects it to be. E.g.

var sendSmsFunc = function() {
    twilSms.sendSms(to,
         ('Did this message arrive at: ' + inThreeMinutes.toString() + '?'))
}

var cronSMS = new CronJob(inThreeMinutes, sendSmsFunc, null, true);

The example you have in your 2nd comment doesn't seem right, because although sendSMS is a callable in that case, it expects two parameters at runtime. CronJob will not pass any parameters so it must be a callable that doesn't expect parameters just simply wants to be called (or executed) at runtime to do its job.

Update 2

If you want to be able to pass in parameters to sendSms() at the same time when specifying parameters to CronJob(), you can use a function that returns a function, e.g.

var sendSmsFunc = function(to, message) {
    return function() {
        twilSms.sendSms(to, message);
    }
}

var cronSMS = new CronJob(inThreeMinutes, sendSmsFunc("123", "test message"), null, true);
3
  • Thanks so much Macell! Your suggestion did work. Is there a way to avoid wrapping my function within an anonymous function? Any syntactic sugar? Do you have any reading to recommend which explains the difference between a standard function, and making it callable in an anonymous function?
    – Blake G
    Dec 13, 2014 at 22:07
  • For example, do you think this should be refactored: var sendSMS = function(to,message) { client.messages.create(smsFactoryFromDefault(to,message)); } module.exports.sendSms = sendSMS;
    – Blake G
    Dec 13, 2014 at 22:08
  • Thanks again Marcell, that was all very helpful. I really appreciate your time.
    – Blake G
    Dec 13, 2014 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.