Search code examples
ruby-on-railsrubyrakeresque

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

Solution

  • 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