在没有rails环境的情况下加载resque worker?

Bri*_*ian 4 ruby rake ruby-on-rails resque

我所拥有的resque工作并不依赖于Rails中的任何东西,但是我很难在不使用rails的情况下启动工作.我看过这篇文章,但它没有帮助(红宝石resque没有加载rails环境)

这是我目前的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
Run Code Online (Sandbox Code Playgroud)

评论的任务将加载Rails环境,一切正常,但这不是我想要的.运行时rake resque:work我收到此错误:

rake aborted!
No such file to load -- application_controller

Tasks: TOP => resque:work => resque:preload
Run Code Online (Sandbox Code Playgroud)

小智 6

如果你只添加了一个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
Run Code Online (Sandbox Code Playgroud)

然后这是你的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
Run Code Online (Sandbox Code Playgroud)

然后打电话 rake resque:work RESQUE_WORKER=true