# Q: Where to put a periodic updater?

**URL:** <https://www.ruby-forum.com/t/q-where-to-put-a-periodic-updater/54129>\
**Category:** Rails\
**Created:** [February 23, 2006, 7:49pm UTC](https://www.ruby-forum.com/t/q-where-to-put-a-periodic-updater/54129 "2006-02-23T19:49:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thiagoalessio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@thiagoalessio](https://www.ruby-forum.com/u/thiagoalessio)\
**Post date:** [February 23, 2006, 7:49pm UTC](https://www.ruby-forum.com/t/q-where-to-put-a-periodic-updater/54129/1 "2006-02-23T19:49:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![thiagoalessio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@thiagoalessio](https://www.ruby-forum.com/u/thiagoalessio)\
**Post date:** [February 23, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/q-where-to-put-a-periodic-updater/54129/2 "2006-02-23T19:55:53Z")

</div>

Hi !

2006/2/23, Thiago A. [removed\_email\_address@domain.invalid](mailto: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](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 !

---

<div class="post-metadata">

**Author:** ![thiagoalessio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@thiagoalessio](https://www.ruby-forum.com/u/thiagoalessio)\
**Post date:** [February 24, 2006, 6:00pm UTC](https://www.ruby-forum.com/t/q-where-to-put-a-periodic-updater/54129/3 "2006-02-24T18:00:44Z")

</div>

Francois,

On 2/23/06, Francois B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Take a look at RailsCron: [http://svn.kylemaxwell.com/rails\_cron/trunk/README](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.

---

<div class="post-metadata">

**Author:** ![thiagoalessio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@thiagoalessio](https://www.ruby-forum.com/u/thiagoalessio)\
**Post date:** [February 24, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/q-where-to-put-a-periodic-updater/54129/4 "2006-02-24T19:11:00Z")

</div>

Hi !

2006/2/24, Thiago A. [removed\_email\_address@domain.invalid](mailto: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,

---

<div class="post-metadata">

**Author:** ![thiagoalessio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@thiagoalessio](https://www.ruby-forum.com/u/thiagoalessio)\
**Post date:** [February 25, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/q-where-to-put-a-periodic-updater/54129/5 "2006-02-25T14:43:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![thiagoalessio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@thiagoalessio](https://www.ruby-forum.com/u/thiagoalessio)\
**Post date:** [February 24, 2006, 7:17pm UTC](https://www.ruby-forum.com/t/q-where-to-put-a-periodic-updater/54129/6 "2006-02-24T19:17:08Z")

</div>

On 2/24/06, Thiago A. [removed\_email\_address@domain.invalid](mailto: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](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](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
