I Have a Rails Task: Should I Use Script/Runner or Rake

I have a Rails task: should I use script/runner or rake?

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.

Rails Application Performance Rake Task vs Application Method

IMHO, split the rake task to regular runner apps would not improve the performance. It is because you still have to load up all components of rails and that's slow. I'd suggest you look into optimising your algorithmic logic.

If tasks that could not be run faster, you should run them as background jobs with DelayedJobs or Resque or cron.

Run Rails Runner from Ruby Script

What you want to do is write a Rake task instead:

# lib/tasks/foo.rake
namespace :foo do
description "@TODO write a descripion"
task bar: :environment do
# your logic goes here
puts [Rails.env, ApplicationController::CONSTANT_NAME]
end
end

This task can be invoked via bin/rake foo:bar. bar: :environment loads the Rails environment for this task.

This is a lot less hacky/wonky then using the rails runner, and is the defacto way of writing tasks/scripts in Ruby that are meant to invoked from the command line.

How do I run Rake tasks within a Ruby script?

from timocracy.com:

require 'rake'

def capture_stdout
s = StringIO.new
oldstdout = $stdout
$stdout = s
yield
s.string
ensure
$stdout = oldstdout
end

Rake.application.rake_require 'metric_fetcher', ['../../lib/tasks']
results = capture_stdout {Rake.application['metric_fetcher'].invoke}

A cron job for rails: best practices?

I'm using the rake approach (as supported by heroku)

With a file called lib/tasks/cron.rake ..

task :cron => :environment do
puts "Pulling new requests..."
EdiListener.process_new_messages
puts "done."
end

To execute from the command line, this is just "rake cron". This command can then be put on the operating system cron/task scheduler as desired.

Update this is quite an old question and answer! Some new info:

  • the heroku cron service I referenced has since been replaced by Heroku Scheduler
  • for frequent tasks (esp. where you want to avoid the Rails environment startup cost) my preferred approach is to use system cron to call a script that will either (a) poke a secure/private webhook API to invoke the required task in the background or (b) directly enqueue a task on your queuing system of choice

How do I run a ruby script, that I put in my /lib/tasks/ directory in my Rails app, once?

The primary difference between a runner and a rake task is : runner would boot rails while rake task doesn't (you can tell it to do so).

Since rake can do both (boot/no boot), there's no concept of runner in rails-3 anymore.

So, create a rake task: whatever_name.rake

Example:

desc "This task does awesome stuff"
task :do_awesome_stuff do
awesome_method
end

def awesome_method
#put your ruby code here
end

Now from your command prompt, type rake do_awesome_stuff to execute this rake task.

To make it boot Rails, change task definition to this:

task :do_awesome_stuff => :environment do


Related Topics



Leave a reply



Submit