开发者

Best way to update DB (mongo) every hour?

I am preparing a small app that will aggregate data on users on my website (via socket.io). I want to insert all data to my monogDB every hour.

What is the best way 开发者_StackOverflow中文版to do that? setInterval(60000) seems to be a lil bit lame :)


You can use cron for example and run your node.js app as scheduled job.

EDIT:

In case where the program have to run continuously, then probably setTimeout is one of the few possible choices (which is quite simple to implement). Otherwise you can offload your data to some temporary storage system, for example redis and then regularly run other node.js program to move your data, however this may introduce new dependency on other DB system and increase complexity depending on your scenario. Redis can also be in this case as some kind of failsafe solution in case when your main node.js app will unexpectedly be terminated and lose part or all of your data batch.


You should aggregate in real time, not once per hour.

I'd take a look at this presentation by BuddyMedia to see how they are doing real time aggregation down to the minute. I am using an adapted version of this approach for my realtime metrics and it works wonderfully.

http://www.slideshare.net/pstokes2/social-analytics-with-mongodb


Why not just hit the server with a curl request that triggers the database write? You can put the command on an hourly cron job and listen on a local port.


You could have mongo store the last time you copied your data and each time any request comes in you could check to see how long it's been since you last copied your data.

Or you could try a setInterval(checkRestore, 60000) for once a minute checks. checkRestore() would query the server to see if the last updated time is greater than an hour old. There are a few ways to do that.

An easy way to store the date is to just store it as the value of Date.now() (https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) and then check for something like db.logs.find({lastUpdate:{$lt:Date.now()-6000000}}).

I think I confused a few different solutions there, but hopefully something like that will work!


If you're using Node, a nice CRON-like tool to use is Forever. It uses to same CRON patterns to handle repetition of jobs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜