简体   繁体   中英

Load resque worker without rails environment?

The resque jobs I have do not depend on anything in Rails, but I'm having a hard time starting workers without the rails env. I've seen this post, but it didn't help ( ruby resque without loading rails environment )

Here is my current rake file:

require "resque/tasks"

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

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

#task "resque:setup" => :environment

The commented task would load the Rails env and everything works, but that's not what I want. When running rake resque:work I get this error:

rake aborted!
No such file to load -- application_controller

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

If you've only added a lib/tasks/resque.rake file and haven't modified your Rakefile, you'll still be loading your Rails environment when you call rake resque:work. Try this for 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

And then this for your resque.rake file:

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

Then call rake resque:work RESQUE_WORKER=true

I referred the link here It worked perfectly for me:

This error was resolved, by running

$> QUEUE=* rake environment resque:work

a cleaner solution was to define a rake task:

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

and now

rake resque:work

Worked perfectly

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM