tomet
tomet

Reputation: 2556

Meteor: Do a database operation regularly

I have an app containing posts. Users can vote on those posts. But every week, say on Friday 12pm, I want to reset the vote counter so every post gets a fresh start.

I know how to do the database operation:

Meteor.methods(
{
    vote: function(ID){
        Posts.update(
            //Selector
            {_id: ID},

            //Modifiers
            {
                $set: {votes: 0}
            }
        )
    }
}
);

where Posts is a meteor collection.

But I have no idea how to schedule this so it is done regularly every week. Can anyone help me out with that?

Thank you,

Tony

Upvotes: 2

Views: 103

Answers (1)

saimeunt
saimeunt

Reputation: 22696

I would use later.js for this purpose, it lets you define complex schedules and then you can execute arbitrary code using a setInterval like API.

http://bunkat.github.io/later/index.html

There is already an atmosphere package to use later in Meteor :

http://atmospherejs.com/package/later

server/schedule.js :

var schedule=later.parse.text("at 12:00 pm on Fri");
var timer=later.setInterval(function(){
  // your vote reset code goes here
},schedule);

Upvotes: 1

Related Questions