Script/Runner in Rails 3

script/runner in rails 3

Use rails runner

$ rails -h
Usage: rails COMMAND [ARGS]
...
runner Run a piece of code in the application environment

All commands can be run with -h for more information.

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.

script/runner:3:in `require': cannot load such file -- commands/runner (LoadError)

Problem was that myapp was recently updated from Rails 2.3 to Rails 4.1 and, as a legacy, /script folder was still used for starting runner.

Proper way to start runner in Rails 4 would be to use bin/rails script like this:

bin/rails runner "Model.method"

Another problem I had was that I had bin/* specified in my .gitignore file so bin/rails script wasn't even in the server.

I got help from this stackoverflow post and from RailsGuides page.

Running rails runner with some parameters

script/runner doesn't take a path to a file, instead it takes some Ruby that it will execute:

script/runner "MyClass.do_something('my_arg')"

You can always set the Rails environment using an environment variable, for example:

RAILS_ENV=production script/runner "MyClass.do_something('my_arg')"

If you want to run some complex task, you may be better off writing it as a Rake task. For example, you could create the file lib/tasks/foo.rake:

namespace :foo do
desc 'Here is a description of my task'
task :bar => :environment do
# Your code here
end
end

You would execute this with:

rake foo:bar

And as with script/runner you can set the environment using an environment variable:

RAILS_ENV=production rake foo:bar

It's also possible to pass arguments to a Rake task.

Ruby on Rails: How can I specify runner script environment

The help on the command line for script/runner gives you your answer.

script/runner -e production Model.method

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.

Runner in Ruby on Rails

From the Rails Guides:

1.7 rails runner

runner runs Ruby code in the context of Rails non-interactively. For
instance:

$ rails runner "Model.long_running_method"

You can also use the alias “r” to invoke the runner: rails r.

You can specify the environment in which the runner command should
operate using the -e switch.

$ rails runner -e staging "Model.long_running_method"

Any code to be run must be loaded as a part of your Rails app, i.e. either in app/ or lib/, among other places.



Related Topics



Leave a reply



Submit