【问题标题】:Running rails runner with some parameters使用一些参数运行 rails runner
【发布时间】:2012-09-07 07:09:52
【问题描述】:

这个命令是我的问题:

/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 也接受 `--environment=production' 作为它的参数,它应该是脚本/运行器的参数。
我想这可以使用一些括号来解决,但没有任何想法。

如果解决方案不涉及(或依赖)Rails 或 Linux 的全局环境,那就更好了。

/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

【问题讨论】:

    标签: ruby-on-rails ruby linux bash command-line


    【解决方案1】:

    只需使用以下语法:

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

    使用ARGV,它是array 中的.rb 来读取参数。

    【讨论】:

      【解决方案2】:

      我假设您使用的是基于 script/runner 的旧版 Rails 我不知道这是否适用于旧版 Rails,但在较新的 Rails 中,您可以只使用 require 'config/environment',它会加载应用程序.然后你就可以在那里写你的脚本了。

      例如,我有一个脚本,它接受一个参数,如果提供了它就打印出来,然后打印出我的应用中有多少用户:

      文件: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
      

      不带参数调用:

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

      使用 arg 速记调用:

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

      使用 arg 长手调用:

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

      【讨论】:

        【解决方案3】:

        script/runner 不使用文件路径,而是使用一些 Ruby 来执行:

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

        您始终可以使用环境变量设置 Rails 环境,例如:

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

        如果你想运行一些复杂的任务,你最好把它写成一个 Rake 任务。例如,您可以创建文件lib/tasks/foo.rake

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

        你可以这样执行:

        rake foo:bar
        

        script/runner 一样,您可以使用环境变量设置环境:

        RAILS_ENV=production rake foo:bar
        

        也可以pass arguments to a Rake task

        【讨论】:

          猜你喜欢
          • 2023-03-12
          • 2021-01-17
          • 2016-06-18
          • 2020-11-24
          • 1970-01-01
          • 2015-04-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多