【问题标题】:Load resque worker without rails environment?在没有rails环境的情况下加载resque worker?
【发布时间】:2012-03-22 14:11:57
【问题描述】:

我所拥有的 resque 工作不依赖于 Rails 中的任何内容,但我很难在没有 rails env 的情况下开始工作。我看过这篇文章,但没有帮助 (ruby resque without loading rails environment)

这是我当前的 rake 文件:

require "resque/tasks"

task "resque:setup" do
  root_path = "#{File.dirname(__FILE__)}/../.."

  require "#{root_path}/app/workers/myworker.rb"
end

#task "resque:setup" => :environment

注释的任务将加载 Rails 环境并且一切正常,但这不是我想要的。运行 rake resque:work 时出现此错误:

rake aborted!
No such file to load -- application_controller

Tasks: TOP => resque:work => resque:preload

【问题讨论】:

    标签: ruby-on-rails ruby rake resque


    【解决方案1】:

    如果你只添加了一个 lib/tasks/resque.rake 文件并且没有修改你的 Rakefile,当你调用 rake resque:work 时你仍然会加载你的 Rails 环境。试试这个 Rakefile:

    unless ENV['RESQUE_WORKER'] == 'true'
      require File.expand_path('../config/application', __FILE__)
      My::Application.load_tasks 
    else
      ROOT_PATH = File.expand_path("..", __FILE__)
      load File.join(ROOT_PATH, 'lib/tasks/resque.rake')
    end
    

    然后是你的 resque.rake 文件:

    require "resque/tasks"
    
    task "resque:setup" do
      raise "Please set your RESQUE_WORKER variable to true" unless ENV['RESQUE_WORKER'] == "true"
      root_path = "#{File.dirname(__FILE__)}/../.."
      require "#{root_path}/app/workers/myworker.rb"
    end
    

    然后拨打rake resque:work RESQUE_WORKER=true

    【讨论】:

      【解决方案2】:

      我参考了链接here 它对我来说非常有用:

      此错误已通过运行解决

      $> QUEUE=* rake environment resque:work
      

      更简洁的解决方案是定义一个 rake 任务:

      task "resque:setup" => :environment do
        ENV['QUEUE'] ||= '*'
        #for redistogo on heroku http://stackoverflow.com/questions/2611747/rails-resque-workers-fail-with-pgerror-server-closed-the-connection-unexpectedl
        Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection }
      end
      

      现在

      rake resque:work
      

      完美运行

      谢谢。

      【讨论】:

      • 这确保了 Rails 环境被加载。 OP 询问是否在 没有 导轨的情况下运行它。
      猜你喜欢
      • 2011-10-17
      • 1970-01-01
      • 2016-12-29
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 2015-08-30
      • 1970-01-01
      相关资源
      最近更新 更多