node.js - Modeling time-based application in NodeJs -
im developing auction style web app, products available period of time.
i know how model that.
so far, i've done storing products in db:
{ ... id: p001, name: product 1, expire_date: 'mon oct 7 2013 01:23:45 utc', ... }
whenever client requests product, test *current_date < expire_date*.
if true, show product data and, client side, countdown timer. if timer reaches 0, disable related controls.
but, server side, there operations needs done if nobody has requested product, example, notify owner product has ended.
i scan whole collection of products on each request, seems cumbersome me.
i thought on triggering routine cron every n minutes, know if can think on better solutions.
thank you!
some thoughts:
- index
expire_date
field. you'll want if you're scanning auction items older date. - consider adding second field
expired
(oractive
) can other types of non-date searches (as can always, , should anyway, reject auctions have expired). - assuming add second field
active
example, can further limit scans auction items active , beyond expiration date. consider compound index cases. (as on time, you'll have more more expired items don't need scan through example). - yes, should add timed task using favorite technique scan expired auctions. there lots of ways -- infrastructure determine makes sense.
- keep local cache of current auction items in memory if possible make scanning efficient possible. there's no reason hit database if nothing expiring.
- again, check when retrieving database confirm items still active -- there race conditions expired items may expire while being retrieved display.
- you'll possible want store state of status e-mails, etc. in database server restarts, etc. handled.
it might like:
{ ... id: p001, name: "product 1", expire_date: isodate("mon oct 7 2013 01:23:45 utc"), active: true, ... } // console db.auctions.esureindex({expire_date: -1, active: 1}) // javascript idea: var theexpirationdate = new date(2013, 10, 06, 0, 0, 0); db.auctions.find({ expire_date : { "$lte" : theexpirationdate }, active: true })
Comments
Post a Comment