views:

427

answers:

1

I'm trying to get delayed job to work as a rake task, but for the life of me I can't figure out what I'm doing wrong. Given the following setup:

#config/environment.rb
Rails::Initializer.run do |config|
  config.gem 'delayed_job'
end

#Rakefile
begin
    require 'delayed/tasks'
rescue LoadError
    STDERR.puts "Run `rake gems:install` to install delayed_job"
end

#Observer
class SomeObserver < ActiveRecord::Observer
   def foo(bar)
   end
   handle_asynchronously :foo
end

Whenever I run rake jobs:clear I get the following error:

undefined method `handle_asynchronously' for SomeObserver:Class

Even though I don't get this error when I'm running the test cases or it directly. Any ideas?

+3  A: 

DelayedJob defines handle_asynchronously in Delayed::MessageSending::ClassMethods. DelayedJob automatically monkey-patches that module into Module in its lib/delayed_job.rb. You'll likely have better luck requiring that in your Rake task:

#Rakefile
begin
  require 'delayed_job'
  require 'delayed/tasks'
rescue LoadError
  STDERR.puts "Run `rake gems:install` to install delayed_job"
end
James A. Rosen