2

I've been successfully using delayed_job for a couple of years now but recently I have a need to implement some kind of success/failure callback/hooks.

Following the delayed_job guide on github i've set up the following custom job:

class XmlImportJob < Struct.new(:file, :supplier_id, :user_id, :directory)
  def perform
    Product.xml_import(file, supplier_id, user, directory)
  end

  def success(job)
    ProductMailer.xml_import_complete.deliver
  end

  def failure(job)
    ProductMailer.xml_import_failed.deliver
  end
end

When running this with Delayed::Job.enqueue XmlImportJob.new(secure_url, 1, 1, directory) for example, I get a Job failed to load: uninitialized constant XmlImportJob. error.

I've tried saving my custom job which is in a file named xml_import.rb under app/jobs and lib and I get the same error.

At the moment i've only tried running this via rails console. Even when explicitly calling require 'xml_import' which returns true I get the same error.

Does anyone with experience of using custom delayed_jobs successfully have any idea what I'm doing wring here?

Raoot
  • 1,751
  • 1
  • 25
  • 51
  • We've used DelayedJobs successfully - works very similarly to Resque. I think your issue is that `XMLImportJob` is not a queue object? I could be completely wrong here, but I'm sure you have to have to create some sort of queue, which you can then add to DelayedJob? – Richard Peck Dec 05 '13 at 10:24
  • Thanks, I'll give that a look. I'm thinking is may also be that the class is `XmlImportJob' but the file is just `xml_import.rb' also, that app/jobs isn't being autoloaded. – Raoot Dec 05 '13 at 10:27
  • Yeah - gut feeling is that because it's not in the typical "model" scope (I.E you're manipulating a file), perhaps DelayedJob needs something to call the `XmlImportJob` file :) – Richard Peck Dec 05 '13 at 10:29
  • It was as I thought! It's funny how writing the problem out on here can sometime make you see what's wrong yourself. – Raoot Dec 05 '13 at 10:42

1 Answers1

6

To answer my own question;

Any custom directories with classes and modules you want to be autoloadable must be added to config/application.rb like so:

config.autoload_paths += %W(
  #{config.root}/app/jobs
)

The files contained within these folders must be named according to rails' conventions so XmlImportJob resides in xml_import_job.rb.

Raoot
  • 1,751
  • 1
  • 25
  • 51