Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Simplify retryable exception logic

  • Loading branch information...
commit 391491dac95666be765fc75329d2c514466d10ef 1 parent 9845ac6
@benkirzhner benkirzhner authored
View
10 lib/qless/retry_exceptions.rb
@@ -1,13 +1,7 @@
module Qless
module RetryExceptions
- attr_accessor :retryable_exception_classes
-
- def self.extended(base)
- base.retryable_exception_classes = []
- end
-
- def retryable_exception?(exception)
- self.retryable_exception_classes.any? { |klass| exception.is_a?(klass) }
+ def retryable_exception_classes
+ @retryable_exception_classes ||= []
end
def retry_on(*exception_classes)
View
14 lib/qless/worker.rb
@@ -99,13 +99,10 @@ def work(interval = 5.0)
def perform(job)
around_perform(job)
+ rescue *retryable_exception_classes(job)
+ job.retry
rescue Exception => error
- if job.klass.respond_to?(:retryable_exception?) &&
- job.klass.retryable_exception?(error)
- job.retry
- else
- fail_job(job, error)
- end
+ fail_job(job, error)
else
job.complete unless job.state_changed?
end
@@ -140,6 +137,11 @@ def unpause_processing
private
+ def retryable_exception_classes(job)
+ return [] unless job.klass.respond_to?(:retryable_exception_classes)
+ job.klass.retryable_exception_classes
+ end
+
# Allow middleware modules to be mixed in and override the
# definition of around_perform while providing a default
# implementation so our code can assume the method is present.
View
2  spec/integration/worker_spec.rb
@@ -41,5 +41,7 @@ def start_worker(run_as_single_process)
it_behaves_like 'a running worker'
it_behaves_like 'a running worker', '1'
+
+ it 'will retry and eventually fail a repeatedly failing job'
end
View
18 spec/unit/retry_exceptions_spec.rb
@@ -9,22 +9,14 @@ module Qless
job_class.extend(RetryExceptions)
end
- it 'defines a retryable_exception? method that returns false by default' do
- job_class.retryable_exception?(exception_class.new).should be_false
+ it 'defines a retryable_exceptions method that returns an empty array by default' do
+ job_class.retryable_exception_classes.should be_empty
end
- describe 'retry_on' do
- it 'defines a retry_on method that makes exception types retryable' do
- job_class.retry_on(exception_class)
- job_class.retryable_exception?(exception_class.new).should be_true
- end
+ it 'defines a retry_on method that makes exception types retryable' do
+ job_class.retry_on(exception_class)
- it 'makes subclasses retryable' do
- subclass = Class.new(exception_class)
-
- job_class.retry_on(exception_class)
- job_class.retryable_exception?(subclass.new).should be_true
- end
+ job_class.retryable_exception_classes.should eq([exception_class])
end
end
end
View
4 spec/unit/worker_spec.rb
@@ -63,7 +63,7 @@ class MyJobClass; end
end
it 'fails the job if performing it raises a non-retryable error' do
- MyJobClass.stub(:retryable_exception?).and_return(false)
+ MyJobClass.stub(:retryable_exception_classes).and_return([])
MyJobClass.stub(:perform) { raise Exception.new("boom") }
expected_line_number = __LINE__ - 1
job.should respond_to(:fail).with(2).arguments
@@ -78,7 +78,7 @@ class MyJobClass; end
end
it 'retries the job if performing it raises a retryable error' do
- MyJobClass.stub(:retryable_exception?).with(an_instance_of(ArgumentError)).and_return(true)
+ MyJobClass.stub(:retryable_exception_classes).and_return([ArgumentError])
MyJobClass.stub(:perform) { raise ArgumentError.new("boom") }
job.should_receive(:retry).with(no_args)
Please sign in to comment.
Something went wrong with that request. Please try again.