decisionMaker
decisionMaker

Reputation: 101

Multiple cron jobs using percolate:synced-cron

Hey guys i try to send emails to some users in my collection after a specific time. The user as to fill a form:

  Template.decisionSetUp.events({
    'submit form':function(event,t){
     var closeDateDB = moment($('[name="date"]').val(), 'DD.MM.YYYY HH:mm').toDate();
    Questions.insert({closeDateDB: closeDateDB},
          function (error,results){
            Router.go('decision', {_id:results});
            var decisionId = results;
            console.log(decisionId);
            Meteor.call('decisionEndMail', decisionId);
         }
   );
});

After this i implemented a cron job:

Meteor.methods({'decisionEndMail':function(decisionId) {
      var t = Questions.findOne({_id:decisionId}).closeDateDB;
      console.log(t);

    SyncedCron.add({
    name: 'demo',
    schedule: function (parser) {
    return parser.recur().on(t).fullDate();
    },
    job: function () {
    // job code
    console.log('job function just ran');
    }
    });
    SyncedCron.start();
    }

  });

The problem is, that it only accept the first meteor.call. For example if i set an closeDateDB at 13:30 and i set another one at 13:40 the job only occurs for the first one. The second one at 13:40 gets never executed. What am i missing here?

Upvotes: 1

Views: 566

Answers (1)

Michael Mason
Michael Mason

Reputation: 932

The issue is that you're calling SyncedCron.add() twice with the same name. Have a look at the code for adding a cron, you'll see it silently ignores any job added with the same name.

https://github.com/percolatestudio/meteor-synced-cron/blob/master/synced-cron-server.js#L122-L129

if (!this._entries[entry.name]) {
  this._entries[entry.name] = entry;

  // If cron is already running, start directly.
  if (this.running) {
    scheduleEntry(entry);
  }
}

One option would be to set name: 'decision_'+decisionId in your call to synced cron which will ensure the name is unique (assuming you're only calling SyncedCron.add once per 'decision').

Upvotes: 4

Related Questions