Reputation: 321
I am building a voting system with Meteor where items can be up- or downvoted. To sort the voting scores more precisely later on, each item holds the fields dailyScore
, monthlyScore
and alltimeScore
, which get incremented or decremented after a vote. I also need to mention, that both registered and unregistered users can vote every 24h (there are two "voters"-arrays containing the userIds of the registered voters and the IP-addresses of the unregistered voters to keep track of the voters and preventing them to vote more than once a day).
The problem I am facing right now is about finding a way to reliably reset
dailyScore
every new day (let's say at UTC-0)monthlyScore
every new month (in addition to (1.) apparently)
My thoughts so far:
lastUpdate
-date of any collection. By using the onConnection
-callback I can check if(currentTime.getDate() != lastUpdate.getDate())
on the server. If true
, I can start the operations performing 1.-3. from above.onConnection
might be "too heavy".Is there a common pattern or best practice for that? Doing periodical database operations (like every fixed 24h or every new onConnection at a new day) should be a well-known problem.
Upvotes: 0
Views: 61
Reputation: 248
percolate:synced-cron package works quite well for this kind of scheduled jobs.
Beware, SyncedCron probably won't work as expected on certain shared hosting providers that shutdown app instances when they aren't receiving requests (like Heroku's free dyno tier or Meteor free galaxy).
Upvotes: 1