2012-03-22 46 views
4

我的工作並不依賴Rails中的任何東西,但我很難在沒有rails env的情況下啓動工作。我已經看到了這個帖子,但它並沒有幫助(ruby resque without loading rails environment加載沒有導軌環境的resque worker?

這是我目前的耙文件:

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的env和一切正常,但是這不是什麼我想要。當運行rake resque:work我得到這個錯誤:

rake aborted! 
No such file to load -- application_controller 

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

回答

6

如果你只加了一個lib /任務/ resque.rake文件並沒有修改過的Rakefile,你仍然可以加載你的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

+0

啊,這個伎倆。謝謝您的幫助! – Brian 2012-03-26 15:47:27

-1

我提到的鏈接here它的工作非常適合我:

通過運行,解決了此錯誤

$> QUEUE=* rake environment resque:work 

一個清潔的解決方案是定義一個耙子任務:

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 

完全

謝謝合作。

+1

這確保了Rails環境_is_已加載。 OP詢問了如何運行_without_rails。 – jwadsack 2015-04-03 18:16:24