1

I want to add function which will run in a separate thread once every few seconds on server.

I read This post and find a lot of CRON packages that can help me with that, but I do not know exactly where they API need to be added in code, it's probably because I do not quite understand how they work with Meteor.

I think my question is a little blunt, but maybe someone tell me where i can put CRON packages functionality in code ?

Community
  • 1
  • 1
NlHell
  • 33
  • 6

2 Answers2

3

I'm using https://atmospherejs.com/percolate/synced-cron as an example of a cron job runner.

You would run this package's code on the server (See: https://github.com/percolatestudio/meteor-synced-cron/blob/master/package.js#L13).

For example, you could schedule a background task after calling a Meteor method:

Meteor.methods({
  doCron: function() {
    SyncedCron.add({
      name: 'Crunch some important numbers for the marketing department',
      schedule: function(parser) {
        // parser is a later.parse object
        return parser.text('every 2 hours');
      },
      job: function() {
        var numbersCrunched = CrushSomeNumbers();
        return numbersCrunched;
      }
    });
  }
});

// Somewhere in your code you need this to start processing jobs. Also on server.
Meteor.startup(function () {
  // code to run on server at startup
  SyncedCron.start();
});
Xinzz
  • 2,242
  • 1
  • 13
  • 26
0

Now that SyncedCron is deprecated, I decided to offer a new background jobs package called Steve Jobs. It's production tested and easy to grasp for Meteor developers.

Max Savin
  • 152
  • 4