Permalink
Browse files

Add mixin for making jobs retryable for certain exceptions.

  • Loading branch information...
1 parent f4798be commit 9845ac697580323d7a2106f891b50a85374562da @benkirzhner benkirzhner committed Dec 17, 2012
Showing with 47 additions and 0 deletions.
  1. +17 −0 lib/qless/retry_exceptions.rb
  2. +30 −0 spec/unit/retry_exceptions_spec.rb
@@ -0,0 +1,17 @@
+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) }
+ end
+
+ def retry_on(*exception_classes)
+ self.retryable_exception_classes.push(*exception_classes)
+ end
+ end
+end
@@ -0,0 +1,30 @@
+require 'qless/retry_exceptions'
+
+module Qless
+ describe RetryExceptions do
+ let(:job_class) { Class.new }
+ let(:exception_class) { Class.new(StandardError) }
+
+ before do
+ 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
+ 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 '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
+ end
+ end
+end

0 comments on commit 9845ac6

Please sign in to comment.