class Delayed::Priority

Constants

DEFAULT_ALERTS

Priorities can be mapped to alerting thresholds for job age (time since run_at), runtime, and attempts. These thresholds can be used to emit events or metrics. Here are the default values (for the default priorities):

Age Alerts ==========

 interactive: 1 minute
user_visible: 3 minutes
    eventual: 1.5 hours
   reporting: 4 hours

Run Time Alerts ======

 interactive: 30 seconds
user_visible: 90 seconds
    eventual: 5 minutes
   reporting: 10 minutes

Attempts Alerts =====

 interactive: 3 attempts
user_visible: 5 attempts
    eventual: 8 attempts
   reporting: 8 attempts

Alerting thresholds can be customized. The keys must match `Delayed::Priority.names`.

Delayed::Priority.alerts = {

high: { age: 30.seconds, run_time: 15.seconds, attempts: 3 },
medium: { age: 2.minutes, run_time: 1.minute, attempts: 6 },
low: { age: 10.minutes, run_time: 2.minutes, attempts: 9 },

}

DEFAULT_NAMES

A Delayed::Priority represents a value that exists within a named range. Here are the default ranges and their names:

0-9: interactive

10-19: user_visible 20-29: eventual

30+: reporting

Ranges can be customized. They must be positive and must include a name for priority >= 0. The following config will produce ranges 0-99 (high), 100-499 (medium) and 500+ (low):

> Delayed::Priority.names = { high: 0, medium: 100, low: 500 }

Attributes

value[R]

Public Class Methods

alerts() click to toggle source
# File lib/delayed/priority.rb, line 64
def alerts
  @alerts || default_alerts
end
alerts=(alerts) click to toggle source
# File lib/delayed/priority.rb, line 76
def alerts=(alerts)
  if alerts
    unknown_names = alerts.keys - names.keys
    raise "unknown priority name(s): #{unknown_names}" if unknown_names.any?
  end

  @alerts = alerts&.sort_by { |k, _| names.keys.index(k) }&.to_h
end
names() click to toggle source
# File lib/delayed/priority.rb, line 60
def names
  @names || default_names
end
names=(names) click to toggle source
# File lib/delayed/priority.rb, line 68
def names=(names)
  raise "must include a name for priority >= 0" if names && !names.value?(0)

  @ranges = nil
  @alerts = nil
  @names = names&.sort_by(&:last)&.to_h&.transform_values { |v| new(v) }
end
new(value) click to toggle source
Calls superclass method
# File lib/delayed/priority.rb, line 119
def initialize(value)
  super()
  value = self.class.names[value] if value.is_a?(Symbol)
  @value = value.to_i
end
ranges() click to toggle source
# File lib/delayed/priority.rb, line 85
def ranges
  @ranges ||= names.zip(names.except(names.keys.first)).each_with_object({}) do |((name, lower), (_, upper)), obj|
    obj[name] = (lower...(upper || Float::INFINITY))
  end
end

Private Class Methods

default_alerts() click to toggle source
# File lib/delayed/priority.rb, line 97
def default_alerts
  @names ? {} : DEFAULT_ALERTS
end
default_names() click to toggle source
# File lib/delayed/priority.rb, line 93
def default_names
  @default_names ||= DEFAULT_NAMES.transform_values { |v| new(v) }
end
method_missing(method_name, *args) click to toggle source
Calls superclass method
# File lib/delayed/priority.rb, line 105
def method_missing(method_name, *args)
  if names.key?(method_name) && args.none?
    names[method_name]
  else
    super
  end
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/delayed/priority.rb, line 101
def respond_to_missing?(method_name, include_private = false)
  names.key?(method_name) || super
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/delayed/priority.rb, line 145
def <=>(other)
  other = other.to_i if other.is_a?(self.class)
  to_i <=> other
end
alert_age() click to toggle source
# File lib/delayed/priority.rb, line 129
def alert_age
  self.class.alerts.dig(name, :age)
end
alert_attempts() click to toggle source
# File lib/delayed/priority.rb, line 137
def alert_attempts
  self.class.alerts.dig(name, :attempts)
end
alert_run_time() click to toggle source
# File lib/delayed/priority.rb, line 133
def alert_run_time
  self.class.alerts.dig(name, :run_time)
end
coerce(other) click to toggle source
# File lib/delayed/priority.rb, line 141
def coerce(other)
  [self.class.new(other), self]
end
name() click to toggle source
# File lib/delayed/priority.rb, line 125
def name
  @name ||= self.class.ranges.find { |(_, r)| r.include?(to_i) }&.first
end

Private Instance Methods

method_missing(method_name, *args) click to toggle source
Calls superclass method
# File lib/delayed/priority.rb, line 156
def method_missing(method_name, *args)
  if method_name.to_s.end_with?('?') && self.class.names.key?(method_name.to_s[0..-2].to_sym)
    method_name.to_s[0..-2] == to_s
  else
    super
  end
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/delayed/priority.rb, line 152
def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.end_with?('?') && self.class.names.key?(method_name.to_s[0..-2].to_sym) || super
end