Search code examples
meteorcronlaterjs

synced-cron to run once - Meteor


I want to run a 'scheduled job' in meteor which needs to be run only once at a specified time. I have the access of the date object. i've tried the below cron expression but didn't get the expected behavior.

shows next run @0

here's the code snippet.

schedule: function(parser) {
            var _year = bidStartTime.getFullYear();
            var _month = bidStartTime.getMonth();
            var _date = bidStartTime.getDate();
            var _hours = bidStartTime.getHours();
            var _min = bidStartTime.getMinutes();

            var bidAsCron = _min+' '+_hours+' '+_date+' '+ _month+' ? '+_year;
            console.log('parsed as ' + bidAsCron);

            // parser is a later.parse object

            //  sample parser.cron('25 17 5 10 ? 2015');
           // should 5th October 2015 at 5:25 pm

          return   parser.cron(bidAsCron);

        },

Solution

  • Using synced-cron version 1.3.0 you can specify parser.recur().on(date).fullDate(); to schedule a once off (i.e not recurring) event like this:

    SyncedCron.add({
      name: cron_name,
      schedule: function (parser) {
        // ending_at is a Date object set to some future date
        // there is no recurrence
        return parser.recur().on(ending_at).fullDate();
      },
      job: function () {
        // job code
      }
    });
    

    working example: http://meteorpad.com/pad/mLfyoLnHSECPhQscz/synced-cron%20to%20run%20once