Synced-Cron help


#1

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?


#2

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


#3

Why not using Meteor.setTimeout ?

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

#4

@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
    }
});

#5

I want it run for once


Nested (or child) synced-cron jobs
#6

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


#7

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.


#8

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
    }
});

#9

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?


#10

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'));

#11

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


#12

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.