Q: Where to put a periodic updater?

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.

Hi !

2006/2/23, Thiago A. [email protected]:

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 !

Francois,

On 2/23/06, Francois B. [email protected] 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.

Hi !

2006/2/24, Thiago A. [email protected]:

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 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.

On 2/24/06, Thiago A. [email protected] 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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Kyle M.
Chief Technologist
E Factor Media // FN Interactive
[email protected]
1-866-263-3261