Skip to content

Commit

Permalink
Added error handling when job fails permanently and failure callback …
Browse files Browse the repository at this point in the history
…raises an exception
  • Loading branch information
denisahearn committed May 23, 2014
1 parent 62fd8ab commit 4f7563a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/delayed/backend/shared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,13 @@ def create_job(opts = {})
expect(@job.payload_object).to receive(:failure)
worker.reschedule(@job)
end

it "handles error in hook" do
Delayed::Worker.destroy_failed_jobs = false
@job.payload_object.raise_error = true
expect{worker.reschedule(@job)}.not_to raise_error
expect(@job.failed_at).to_not be_nil
end
end

context "when the job's payload has no #failure hook" do
Expand Down
10 changes: 8 additions & 2 deletions lib/delayed/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,14 @@ def reschedule(job, time = nil)

def failed(job)
self.class.lifecycle.run_callbacks(:failure, self, job) do
job.hook(:failure)
self.class.destroy_failed_jobs ? job.destroy : job.fail!
begin
job.hook(:failure)
rescue Exception => error
say "Error when running failure callback: #{error}", Logger::ERROR
say error.backtrace.join("\n"), Logger::ERROR
ensure
self.class.destroy_failed_jobs ? job.destroy : job.fail!
end
end
end

Expand Down
4 changes: 3 additions & 1 deletion spec/sample_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def perform; sleep 250; end
end

class OnPermanentFailureJob < SimpleJob
def failure; end
def initialize; @raise_error = false; end
def raise_error=(raise_error); @raise_error = raise_error; end
def failure; raise 'did not work' if @raise_error; end
def max_attempts; 1; end
end

Expand Down

0 comments on commit 4f7563a

Please sign in to comment.