Hey all, i want to execute a action (located in lib) every 5 minutes. For now i use periodically_call_remote. But every time i navigate on my site, the countdown is reseting to zero and starts again. I want a countdown which is counting autonomus and starts if the user logged in. Thanks for your help!
on 2009-01-22 19:28
on 2009-01-22 23:56
Hi Stephan, I am not too sure where exactly you are trying to execute this time based procedure. It can be done on several different levels, each of which would result in a different solution. However, from the following statement I will assume that you might be wanting to perform some duties on the model level: "I want a countdown which is counting autonomus and starts if the user logged in." Say that you are simply wanting to save an active record object every 5 minutes after the user has logged in. If the lifespan of an active record object will be persistent throughout a user's session, which I would NOT advise, there is a really neat gem called the rufus scheduler that might do the trick. http://rufus.rubyforge.org/rufus-scheduler/ You could use it in your model like so: class Fish < ActiveRecord::Base require 'rubygems' require 'rufus/scheduler' SCHEDULER = Rufus::Scheduler.start_new def initialize super SCHEDULER.every "5m" do #might want to consider the fact that this object could be in a state where it can't save due to validation constraints self.save end end end Otherwise, you could simply check the "updated_at" timestamp for the active record in the controller and run some code every 5 minutes (current_time - updated_at > 5 minutes). Of course the drawback here is that it is completely contingent upon the user making requests to the server. HTH, Aldo Sarmiento
on 2009-01-23 08:15
on 2009-01-23 09:30
on 2009-01-23 09:58
Thanks a lot, sound like the perfect solution for me :)