Boris Daka
Boris Daka

Reputation: 603

Meteor.setInterval on server side, Performance?

I was looking for a way to clean up some of the database entries I might not need after 24hours or something like that, I was looking into meteor code: https://github.com/meteor/meteor/blob/832e6fe44f3635cae060415d6150c0105f2bf0f6/packages/oauth/pending_credentials.js

I found this part:

// Periodically clear old entries that were never retrieved
var _cleanStaleResults = function() {
  // Remove credentials older than 1 minute
  var timeCutoff = new Date();
  timeCutoff.setMinutes(timeCutoff.getMinutes() - 1);
  OAuth._pendingCredentials.remove({ createdAt: { $lt: timeCutoff } });
};
var _cleanupHandle = Meteor.setInterval(_cleanStaleResults, 60 * 1000);

I couldn't find any where the exectuion of the function. I was wondering if var _cleanupHandle means it will execute it every 60 seconds? and if it's it seems strange an endless function run every 60 seconds? It might be a general javascript question but I really trying to understand the performance of this kind of thing and if I can safely re use this technique? My main goal is to store temporary data on the server and remove it after it's not needed.

Upvotes: 1

Views: 198

Answers (1)

Xinzz
Xinzz

Reputation: 2242

I recommend using a task queue library such as https://atmospherejs.com/percolate/synced-cron.

For this approach, you would just specify your task and start it. You can also end the task whenever you want. Note that this is all server-side.

Adding a task

SyncedCron.add({
  name: 'Crunch some important numbers for the marketing department',
  schedule: function(parser) {
    // parser is a later.parse object. NOTE THAT THIS IS WHERE YOU SPECIFY THE FREQUENCY
    return parser.text('every 2 hours');
  },
  job: function() {
    var numbersCrunched = CrushSomeNumbers();
    return numbersCrunched;
  }
});

Starting the tasks

SyncedCron.start();

Stopping all tasks

SyncedCron.stop()

There are a lot more options so I recommend checking out the documentation.

Upvotes: 2

Related Questions