module Qless::Middleware::RequeueExceptions

This middleware is like RetryExceptions, but it doesn't use qless-core's internal retry/retry-tracking mechanism. Instead, it re-queues the job when it fails with a matched error, and increments a counter in the job's data.

This is useful for exceptions for which you want a different backoff/retry strategy. The internal retry mechanism doesn't allow for separate tracking by exception type, and thus doesn't allow you to retry different exceptions a different number of times.

This is particularly useful for handling resource throttling errors, where you may not want exponential backoff, and you may want the error to be retried many times, w/o having other transient errors retried so many times.

Constants

DEFAULT_ON_REQUEUE_CALLBACK
RequeueableException

Public Instance Methods

around_perform(job) click to toggle source
Calls superclass method
# File lib/qless/middleware/requeue_exceptions.rb, line 71
def around_perform(job)
  super
rescue *requeueable_exceptions.keys => e
  handle_exception(job, e)
end
handle_exception(job, error) click to toggle source
# File lib/qless/middleware/requeue_exceptions.rb, line 56
def handle_exception(job, error)
  config = requeuable_exception_for(error)

  requeues_by_exception = (job.data['requeues_by_exception'] ||= {})
  requeues_by_exception[config.klass.name] ||= 0

  config.raise_if_exhausted_requeues(
    error, requeues_by_exception[config.klass.name])

  requeues_by_exception[config.klass.name] += 1
  job.requeue(job.queue_name, delay: config.delay, data: job.data)

  on_requeue_callback.call(error, job)
end
on_requeue_callback() click to toggle source
# File lib/qless/middleware/requeue_exceptions.rb, line 52
def on_requeue_callback
  @on_requeue_callback ||= DEFAULT_ON_REQUEUE_CALLBACK
end
requeuable_exception_for(e) click to toggle source
# File lib/qless/middleware/requeue_exceptions.rb, line 85
def requeuable_exception_for(e)
  requeueable_exceptions.fetch(e.class) do
    requeueable_exceptions.each do |klass, exc|
      break exc if klass === e
    end
  end
end
requeue_on(*exceptions, options) click to toggle source
# File lib/qless/middleware/requeue_exceptions.rb, line 40
def requeue_on(*exceptions, options)
  RequeueableException.from_splat_and_options(
    *exceptions, options).each do |exc|
    requeueable_exceptions[exc.klass] = exc
  end
end
requeueable?(exception) click to toggle source
# File lib/qless/middleware/requeue_exceptions.rb, line 77
def requeueable?(exception)
  requeueable_exceptions.member?(exception)
end
requeueable_exceptions() click to toggle source
# File lib/qless/middleware/requeue_exceptions.rb, line 81
def requeueable_exceptions
  @requeueable_exceptions ||= {}
end
use_on_requeue_callback(&block) click to toggle source
# File lib/qless/middleware/requeue_exceptions.rb, line 48
def use_on_requeue_callback(&block)
  @on_requeue_callback = block if block
end