On Wed, Oct 13, 2010 at 12:38 PM, Mattias Linnap <matt...@linnap.com> wrote: > Hi Django users, > > I'm building an application with Django, and I need some database > changes to occur at certain times in the future - independently of any > web request. Things like cleaning up expired sessions, deleting > expired user actions, etc. > > What is the best way to implement those? > 1) Make a special view, like /cron/, that performs all the scheduled > actions, and use an external script to poll it over HTTP regularly. > Cons: there might be a request timeout in the web server, so only a > small number of very fast actions are possible. > 2) Use standard linux cron, and write a script that calls ./manage.py > with custom commands. Cons: is it a good idea to use manage.py > commands in production? > 3) Try to avoid any time-based database changes, and rewrite the app > so that data objects know if they are "too old". Cons: makes the rest > of the app more complex. > 4) Something else? > > If a bit of background might be useful, I'm building an online sales > app where users can reserve products for up to 2 hours. When the 2 > hours is over, the product is marked as available again. It would be > possible to compute the availability of each product based on the list > of reservations instead of caching it at the object, but this would > require looking through all reservations and sales ever made to know > the current state of a product. > > Thanks for any advice, > > Mattias >
Definitely not #1! I use #2 for periodic scripts, eg a script runs at 4 AM +? minutes to clean expired sessions and transient objects. I don't think it would suit your purposes though, you would have to be periodically running scripts to see if anything needs going away - not exactly ideal. #3 is useful in certain scenarios, in this though you are trying to mimic availability of an item within a stock system. It might work if you are selling one off items (like a seat in a cinema), but if you are selling multiples of the same item it could get a little tricky! I would recommend #4 'something else' :) The something else being celeryd + rabbitmq + django-celery. RabbitMQ is an enterprise message queue system that allows you to do awesomeness, like saying 'deliver this message in 2 hours'. It's a complex system, but it gives you the power to do complex things. Cheers Tom Celery: http://ask.github.com/celery/getting-started/introduction.html django-celery: http://celeryproject.org/docs/django-celery/ RabbitMQ: http://www.rabbitmq.com/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.