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
Simply use the below syntax:
rails runner <.rb file path> [..args]
Use ARGV
which is an array
in your .rb
to read the arguments.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With