I was lucky to break the error return line into an email, which at least gives me some context of when / how the task worked with a delay:
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 Delayed :: Job tasks, here is my approach. I will stop the task with expectations from various scenarios, and then check how Delayed :: Job handles these results. Here is an example of how I used Delayed :: Job to synchronize with a remote nightly CMS.
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)
end
source share