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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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
-- 
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
> 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.Hey Aldo, i ll try it with your controller solution, sounds great! So my last problem is how can i display the user the countdown? As i wrote i do it with some javascript countdown, but everytime the user navigate on my site the countdown is reset. Thanks a lot! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ok i think i know what you are doing now. I would suggest writing 
something like this in your ./lib directory (named timer.rb in this 
case):
class Timer
    @end_time
    attr_writer :time
    def initialize(end_time)
        @end_time = end_time
    end
    def time_remaining
        @end_time - Time.now
    end
    def time_up?
        time_remaining <= 0
    end
end
Of course this is really rough, and there are no date conversions from 
milliseconds to other increments. However it is a good starting point.
Now you can simply create a session that holds an instance of the timer 
object. Something like this in your controller:
def authorize
    #your authorization code
    session[:timer] = Timer.new(Time.now + 5.minutes)
end
Then you can use this session in your views to set the countdown times 
in javascript:
<% javascript_tag do -%>
    function show_timer(){
        var start_time = <%= session[:timer].time_remaining %>;
        //rest of your display code here
    }
<% end -%>
This way, you will be able to access the time across posts!
HTH,
Aldo Sarmiento
-- 
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Thanks a lot, sound like the perfect solution for me :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---