Forum: Ruby on Rails Execute Action every x minutes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
00197f7c716dba70bbe3dd1c68ba4404?d=identicon&s=25 Stephan Meier (pure)
on 2009-01-22 19:28
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!
B54c73767addd2a75cf005e3941b7829?d=identicon&s=25 Aldo Sarmiento (sarmiena)
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
00197f7c716dba70bbe3dd1c68ba4404?d=identicon&s=25 Stephan Meier (pure)
on 2009-01-23 08:15
> 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!
B54c73767addd2a75cf005e3941b7829?d=identicon&s=25 Aldo Sarmiento (sarmiena)
on 2009-01-23 09:30
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
00197f7c716dba70bbe3dd1c68ba4404?d=identicon&s=25 Stephan Meier (pure)
on 2009-01-23 09:58
Thanks a lot, sound like the perfect solution for me :)
This topic is locked and can not be replied to.