Synced-Cron help

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?

Hi, run only once/day, week, month, year?

Why not using Meteor.setTimeout ?

// create the job
Meteor.setTimeout(function () {
    // do the job
}, 20000);

@fvilers he might be running more than 1 instance

@faysal you can do something like this

var newCronJobId = Meteor.uuid();
SyncedCron.add({
    name: newCronJobId,
    schedule: function (parser) {
        // whatever logic for +20secs from now
    },
    job: function () {
        SyncedCron.remove(newCronJobId);
        // do stuff
    }
});

I want it run for once

I know how to write cron job. I need know what would the parser parse.

You can use momentjs to calculate 20 secs from now then convert it into a parser.text string (it will be a recurring time). Then you write your cron job like I did above, which removes itself when it runs.

You should definitively read the Later.js docs as the parser you get on the schedule callback is a Later.js parser.

var newCronJobId = Meteor.uuid();
SyncedCron.add({
    name: newCronJobId,
    schedule: function (parser) {
        return parser.text('after 20 secs');
    },
    job: function () {
        SyncedCron.remove(newCronJobId);
        // do stuff
    }
});

thanks for your answer. I faced one problem here
If I create a job at time 1:10:00 (hour:min:sec) , the job executes it at 1:10:20.
AGAIN if i create another job(suppose) at 1:10:08 , this job also executes at 1:10:20.

any idea how to overcome this problem?

Using the Moment library, you can easily manipulate date. This will create a job that run every day at the specified date (now + 20 seconds), don’t forget to remove the job when it’s done.

return parser.text('at ' + moment().add(20, 'seconds').format('HH:mm a'));

Just curious, what are some practical use applications for synced cron?

it can be used for reminder purpose. For example. you have an event to attend tomorrow, you can use synced-cron to email you before 2 hours of the event.

1 Like