Forum: Ruby on Rails Q: Where to put a periodic updater?

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.
Thiago A. (Guest)
on 2006-02-23 20:49
(Received via mailing list)
Hello,

I need to put a script in place for updating a local database with a
remote resource periodically. It should be started alongside the
server (or inside it, whatever) and be run as long as the server
running my rails app is up an running.

Any thoughts on how to accomplish this?

I have thought of editing the environment.rb file and placing the call
to the script trigger there, but I wonder if this is a good design
decision and if it is webserver-dependent.

Cheers,

Thiago A.
Francois B. (Guest)
on 2006-02-23 20:55
(Received via mailing list)
Hi !

2006/2/23, Thiago A. <removed_email_address@domain.invalid>:
> Hello,
>
> I need to put a script in place for updating a local database with a
> remote resource periodically. It should be started alongside the
> server (or inside it, whatever) and be run as long as the server
> running my rails app is up an running.

Take a look at RailsCron:
http://svn.kylemaxwell.com/rails_cron/trunk/README

Of course, cron itself can do that for you.  Just schedule
"script/runner" with some parameters that say what to do:

0 8,20 * * * * ruby script/runner production "ProductUpdator.update"

Hope that helps !
Thiago A. (Guest)
on 2006-02-24 19:00
(Received via mailing list)
Francois,

On 2/23/06, Francois B. <removed_email_address@domain.invalid> wrote:
> Take a look at RailsCron: http://svn.kylemaxwell.com/rails_cron/trunk/README

Thanks for the pointer. But I still miss something.

Where should I place the RailsCron call? For a first test, I have
tried to define a class Printer for  peridiodically printing to the
standard output. It looked like this

class Printer < ActiveRecord::Base
    background :print, :every => 1.minute

    def self.print
        puts "RailsCron called me at #{Time.now}"
    end
end

I did not install anything besides out-of-the-box rails, since the
readme file says it is a standard Rails plugin. As you may see, I have
read 'standard' as 'bundled with the standard installation'. Maybe I
am wrong here?

I started the webrick server with ruby script/server and the class
didn't get called (just like I expected, no magic happened). The
readme file says that cron can be started and stopped by rake, but
what I would really like was to start it whenever the server is
started. Should I modify my environment scripts? Is there any other
(more appropiate) startup script for a Rails app or I am seriously
misguided here?

Thanks in advance,

Thiago A.
Francois B. (Guest)
on 2006-02-24 20:11
(Received via mailing list)
Hi !

2006/2/24, Thiago A. <removed_email_address@domain.invalid>:
> I started the webrick server with ruby script/server and the class
> didn't get called (just like I expected, no magic happened). The
> readme file says that cron can be started and stopped by rake, but
> what I would really like was to start it whenever the server is
> started. Should I modify my environment scripts? Is there any other
> (more appropiate) startup script for a Rails app or I am seriously
> misguided here?

I haven't used the plugin myself.  I just knew about it.

After reading the docs, I guess you need to start the plugin through
Rake.  As you mention, environment.rb seems like the right place to do
it.

Hope that helps,
Kyle M. (Guest)
on 2006-02-24 20:17
(Received via mailing list)
On 2/24/06, Thiago A. <removed_email_address@domain.invalid> wrote:
>
> read 'standard' as 'bundled with the standard installation'. Maybe I
> am wrong here?

You also need to execute the following command from the RAILS_ROOT
folder:
./script/plugin install http://svn.kylemaxwell.com/rails_cron/trunk

>
> I started the webrick server with ruby script/server and the class
> didn't get called (just like I expected, no magic happened). The
> readme file says that cron can be started and stopped by rake, but
> what I would really like was to start it whenever the server is
> started. Should I modify my environment scripts? Is there any other
> (more appropiate) startup script for a Rails app or I am seriously
> misguided here?

1. You need to add

require_dependency "printer"

to your environment.rb.  Otherwise, the action will start the first
time your program calls for the class, as the class is not loaded
until it is needed.

Also, you need to execute the command "rake cron_start"  This
initializes the background process.  Use "rake cron_graceful" to
restart the process.  To automatically execute this at the same time
as the server restarts, I reccommend writing a SwitchTower script.

>
> Thanks in advance,
>
> Thiago A.
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Kyle M.
Chief Technologist
E Factor Media // FN Interactive
removed_email_address@domain.invalid
1-866-263-3261
Thiago A. (Guest)
on 2006-02-25 15:43
(Received via mailing list)
Kyle and Francois,

Thank you for the help. Although I can not test the solution right
now, since I am going off-line for a whole week, your help was really
welcome.

Thanks,

Thiago A.
This topic is locked and can not be replied to.