Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running rails runner with some parameters

This command is my problem:

/usr/local/bin/ruby **script/runner** --environment=production app/jobs/**my_job.rb** -t my_arg

`my_job.rb` is my script, which handles command line arguments. In this case it is `-t my_arg`.

my_job.rb also takes `--environment=production' as its argument, which should be script/runner's argument.
I guess this can be solved using some parentheses, but no have an idea.

If the solution doesn't touch (or depends on) Rails or Linux's global environment, it would be much better.

/usr/local/lib/ruby/1.8/optparse.rb:1450:in `complete': invalid option: --environment=production (OptionParser::InvalidOption)
  from /usr/local/lib/ruby/1.8/optparse.rb:1448:in `catch'
  from /usr/local/lib/ruby/1.8/optparse.rb:1448:in `complete'
  from /usr/local/lib/ruby/1.8/optparse.rb:1261:in `parse_in_order'
  from /usr/local/lib/ruby/1.8/optparse.rb:1254:in `catch'
  from /usr/local/lib/ruby/1.8/optparse.rb:1254:in `parse_in_order'
  from /usr/local/lib/ruby/1.8/optparse.rb:1248:in `order!'
  from /usr/local/lib/ruby/1.8/optparse.rb:1339:in `permute!'
  from /usr/local/lib/ruby/1.8/optparse.rb:1360:in `parse!'
  from app/jobs/2new_error_log_rt_report.rb:12:in `execute'
  from app/jobs/2new_error_log_rt_report.rb:102
  from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `eval'
  from /home/www/maldive/admin/releases/20120914030956/vendor/rails/railties/lib/commands/runner.rb:46
  from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
  from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
  from script/runner:3
like image 782
Benjamin Avatar asked Sep 14 '12 06:09

Benjamin


3 Answers

Simply use the below syntax:

rails runner <.rb file path> [..args]

Use ARGV which is an array in your .rb to read the arguments.

like image 152
Saurav Kumar Avatar answered Nov 07 '22 09:11

Saurav Kumar


I assume you're on an older Rails based on script/runner I don't know if this works for older Rails' or not, but in newer Rails, you can just require 'config/environment', and it will load the app. Then you can just write your scripts in there.

For example, I have a script that takes an argument, prints it out if it was provided, and then prints out how many users are in my app:

File: app/jobs/my_job.rb

require 'optparse'

parser = OptionParser.new do |options|
  options.on '-t', '--the-arg SOME_ARG', 'Shows that we can take an arg' do |arg|
    puts "THE ARGUMENT WAS #{arg.inspect}"
  end
end

parser.parse! ARGV

require_relative '../../config/environment'

puts "THERE ARE #{User.count} USERS" # I have a users model

Calling with no args:

$ be ruby app/jobs/my_job.rb 
THERE ARE 2 USERS

Calling with an arg shorthand:

$ be ruby app/jobs/my_job.rb -t my_arg
THE ARGUMENT WAS "my_arg"
THERE ARE 2 USERS

Calling with an arg long-hand:

$ be ruby app/jobs/my_job.rb --the-arg my_arg
THE ARGUMENT WAS "my_arg"
THERE ARE 2 USERS
like image 45
Joshua Cheek Avatar answered Nov 07 '22 11:11

Joshua Cheek


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.

like image 8
georgebrock Avatar answered Nov 07 '22 11:11

georgebrock