views:

24

answers:

2

I'm looking to debug a delayed jobs class. First off I added the config/initializers/delayed_job_config to move my logging to my log/production.rb file.

Delayed::Job.destroy_failed_jobs = false
Delayed::Worker.logger = Rails.logger

Then in the actual file I'm doing in the actual file

class TestJob < Struct.new()
 logger.debug("test logging")
end

The log isn't showing anything in it. Anyone have any ideas?

A: 

Have you tried to narrow it down to whether this is a problem with the delayed job not getting fired or the logger config not working for you? What if you replace the logger.debug call with a puts?

davidkovsky
The delayed_job is getting fired. Tried puts and it didn't help
Splashlin
How do you know that the delayed_job is getting fired? You said the puts didn't help, but did it even work (did you see its output)?
davidkovsky
A: 

I've had luck with rending the backtrace of the error to an email, which at least gives me some context of when / how the delayed job is failing:

Here is an example:

result = Delayed::Job.work_off
  unless result[1].zero?
    ExceptionMailer.deliver_exception_message("[Delayed Job Failed] Error id: #{Delayed::Job.last.id}", Delayed::Job.last.last_error)
  end

If you just want to write tests for your Delayed::Job tasks here is the approach I have taken. I will stub out the perform task with the expectations from the various scenarios and then test how Delayed::Job handles those results. Here is an example of how I used Delayed::Job to sync with a remote CMS nightly.

it "should sync content from the remote CMS" do
CMSJobs::Sync.module_eval do
  def perform
    url.should == "http://someurl.com/tools/datafeed/resorts/all"
    Resort.sync_resorts!([{'id' => 1, 'name' => 'resort foo'}, { 'id' => 2, 'name' => 'resort bar' }])
  end
end

lambda do
  Resort.sync_all!
end.should change(Delayed::Job, :count)

lambda do
  Delayed::Job.work_off
end.should change(Resort, :count).by(2)

# It should now delete a resort if it doesn't appear in the next CMS feed.
lambda do
  Resort.sync_resorts!([{ 'id' => 2, 'name' => 'resort bar' }])
end.should change(Resort, :count).by(-1)

end

heavysixer
The delayed_Job isn't failing. I just want to log it to help me try writing some new code for it.
Splashlin