views:

4579

answers:

5

For ad hoc Rails tasks we have a few implementation alternatives, chief among which would seem to be

script/runner some_useful_thing

and

rake some:other_useful_thing

Which option should I prefer? If there's a clear favourite then when, if ever, should I consider using the other? If never, then why would you suppose it's still present in the framework without deprecation warnings?

Confused of London

+2  A: 

For one off commands script/runner can be fine. For anything repeated, a rake task is easier in the long-run, and has a summary if you forget what it does.

danieljohnmorris
+1  A: 

I got the impression script/runner was primarily for periodic tasks. E.g., a cron job that runs:

SomeClass.update_from_web('http://www.sourcefordata.gov/')
jlc
+10  A: 

The difference between them is that script/runner boots Rails whereas a Rake task doesn't unless you tell it to by making the task depend on :environment, like this:

task :some_useful_task => :environment do
  # do some useful task
end

Since booting Rails is expensive, it might be worth skipping if you can avoid it.

Other than that, they are roughly equivalent. I use both, but lately I've used script/runner executing a script separately more.

Luke Francl
+2  A: 

One thing I've done is just write normal ruby scripts and put them in the script/maintenance directory.

All you need to do to load rails and get access to all your models, etc, is put require '../../config/environment.rb' at the top of your file, then you're away.

Orion Edwards
Wouldn't it make more sense to put these into rake tasks?
danieljohnmorris
Why bother with the added complexity of a rake task. This way we just have a file that can get run like any other script on a *nix system
Orion Edwards
^^ to clarify: Rake is great, and I'd use it if you needed dependencies, etc, but if you don't, it's just one more thing to think about for no reason
Orion Edwards
+3  A: 

FWIW there seems to be some movement away from using script runner in favor or rake:

Update (4/25/2009): I recommend using rake tasks as opposed to script/runner for recurring tasks.

Also, as per this post you can use rake for recurring tasks just fine:

If I then wanted this to run nightly on my production database at midnight, I might write a cronjob that looks something like this:

0 0 * * * cd /var/www/apps/rails_app/ && /usr/local/bin/rake RAILS_ENV=production utils:send_expire_soon_emails

esilver