Module: Amigo::QueueBackoffJob

Defined in:
lib/amigo/queue_backoff_job.rb

Defined Under Namespace

Modules: InstanceMethods, PrependedMethods

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cache_queue_namesObject

Whether all_queue_names should be cached.



89
90
91
# File 'lib/amigo/queue_backoff_job.rb', line 89

def cache_queue_names
  @cache_queue_names
end

.enabledObject

Return true if backoff checks are enabled.



74
75
76
# File 'lib/amigo/queue_backoff_job.rb', line 74

def enabled
  @enabled
end

.latency_cache_durationObject

Return how long queue latencies should be cached before they are re-fetched from Redis. Avoids hitting Redis to check latency too often. Default to 5 seconds. Set to 0 to avoid caching.



99
100
101
# File 'lib/amigo/queue_backoff_job.rb', line 99

def latency_cache_duration
  @latency_cache_duration
end

.max_backoffObject

Maximum time into the future a job will reschedule itself for. Ie, if latency is 30s, and max_backoff is 10, the job will be scheduled for 10s into the future if it finds backoff pressure.



71
72
73
# File 'lib/amigo/queue_backoff_job.rb', line 71

def max_backoff
  @max_backoff
end

Class Method Details

.all_queue_namesObject

Cached value of all Sidekiq queues, since they rarely change. If your queue names change at runtime, set cache_queue_names to false.



82
83
84
85
86
# File 'lib/amigo/queue_backoff_job.rb', line 82

def all_queue_names
  return @all_queue_names if @cache_queue_names && @all_queue_names
  @all_queue_names = ::Sidekiq::Queue.all.map(&:name)
  return @all_queue_names
end

.check_latency(qname, now: Time.now) ⇒ Object

Check the latency of the queue with the given now. If the queue has been checked more recently than latency_cache_duration specified, return the cached value.



104
105
106
107
108
109
110
111
# File 'lib/amigo/queue_backoff_job.rb', line 104

def check_latency(qname, now: Time.now)
  return ::Sidekiq::Queue.new(qname).latency if self.latency_cache_duration.zero?
  cached = @latency_cache[qname]
  if cached.nil? || (cached[:at] + self.latency_cache_duration) < now
    @latency_cache[qname] = {at: now, value: ::Sidekiq::Queue.new(qname).latency}
  end
  return @latency_cache[qname][:value]
end

.enabled?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/amigo/queue_backoff_job.rb', line 76

def enabled?
  return @enabled
end

.included(cls) ⇒ Object



50
51
52
53
# File 'lib/amigo/queue_backoff_job.rb', line 50

def self.included(cls)
  cls.include InstanceMethods
  cls.prepend PrependedMethods
end

.resetObject

Reset class state. Mostly used just for testing.



57
58
59
60
61
62
63
64
65
66
# File 'lib/amigo/queue_backoff_job.rb', line 57

def reset
  @max_backoff = 10
  is_testing = defined?(::Sidekiq::Testing) && ::Sidekiq::Testing.enabled?
  @enabled = !is_testing
  @cache_queue_names = true
  @cache_latencies = true
  @all_queue_names = nil
  @latency_cache_duration = 5
  @latency_cache = {}
end