Munna
Munna

Reputation: 109

how to write synced-cron job that executes 20 seconds later - SyncedCron Meteor

I want to create a synced-cron job which runs only once and the execution time will be 20 seconds later as soon as the job is created. How would I write the parser?

Upvotes: 0

Views: 2458

Answers (2)

Yulong
Yulong

Reputation: 1719

According to the doc:

To schedule a once off (i.e not recurring) event, create a job with a schedule like this parser.recur().on(date).fullDate();

So you can set something like parser.recur().on(new Date()+1000*20).fullDate() to execute the job once.

Upvotes: 3

Lucas Blancas
Lucas Blancas

Reputation: 561

Meteor.setTimeout can be run on the server

If you want something that runs 20 seconds after server startup, you can do this

if(Meteor.isServer){

    var job = function(){};

    Meteor.startup(function(){
        Meteor.setTimeout(job, 20000);
    });
}

If you are set on using synced-cron, you can use SyncedCron.remove(jobName). See Advanced section of Synced Cron Docs

if(Meteor.isServer){

    SyncedCron.add({
        name: 'Run in 20 seconds only once',
        schedule: function(parser) {
            // parser is a later.parse object
            return parser.text('every 20 seconds');
        },
        job: function() {
            // do something important here

            SyncedCron.remove('Run in 20 seconds only once');
        }
    });

    Meteor.startup(function(){
        SyncedCron.start();
    });

}

Upvotes: 4

Related Questions