Class: OkComputer::DelayedJobBackedUpCheck

Inherits:
SizeThresholdCheck show all
Defined in:
lib/ok_computer/built_in_checks/delayed_job_backed_up_check.rb

Constant Summary

Constants inherited from Check

Check::CheckNotDefined

Instance Attribute Summary collapse

Attributes inherited from SizeThresholdCheck

#name, #size_proc

Attributes inherited from Check

#failure_occurred, #message, #registrant_name, #time

Instance Method Summary collapse

Methods inherited from SizeThresholdCheck

#check

Methods inherited from Check

#<=>, #clear, #mark_failure, #mark_message, #run, #success?, #to_json, #to_text, #with_benchmarking

Constructor Details

#initialize(priority, threshold, options = {}) ⇒ DelayedJobBackedUpCheck

Public: Initialize a check for backed-up Delayed Job jobs

priority - Which priority to check for threshold - An Integer to compare the jobs count against to consider it backed up options - Hash of optional parameters

queue - Used to monitor a specific delayed job queue (default: nil)
include_locked - If true, will include currently locked jobs in the query (default: false)
include_errored - If true, will include currently errored jobs in the query (default: false)
greater_than_priority - If true, will include all jobs with a priority value equal or greater than the set value.

Example:

check = new(10, 50)
# => The check will look for jobs with priority between
# 0 and 10, considering the jobs as backed up if there
# are more than 50 of them


25
26
27
28
29
30
31
32
33
# File 'lib/ok_computer/built_in_checks/delayed_job_backed_up_check.rb', line 25

def initialize(priority, threshold, options = {})
  self.priority = Integer(priority)
  self.threshold = Integer(threshold)
  self.queue = options[:queue]
  self.include_locked = !!options[:include_locked]
  self.include_errored = !!options[:include_errored]
  self.greater_than_priority = !!options[:greater_than_priority]
  self.name = greater_than_priority ? "Delayed Jobs with priority higher than '#{priority}'" : "Delayed Jobs with priority lower than '#{priority}'"
end

Instance Attribute Details

#greater_than_priorityObject

Returns the value of attribute greater_than_priority.



3
4
5
# File 'lib/ok_computer/built_in_checks/delayed_job_backed_up_check.rb', line 3

def greater_than_priority
  @greater_than_priority
end

#include_erroredObject

Returns the value of attribute include_errored.



3
4
5
# File 'lib/ok_computer/built_in_checks/delayed_job_backed_up_check.rb', line 3

def include_errored
  @include_errored
end

#include_lockedObject

Returns the value of attribute include_locked.



3
4
5
# File 'lib/ok_computer/built_in_checks/delayed_job_backed_up_check.rb', line 3

def include_locked
  @include_locked
end

#priorityObject

Returns the value of attribute priority.



3
4
5
# File 'lib/ok_computer/built_in_checks/delayed_job_backed_up_check.rb', line 3

def priority
  @priority
end

#queueObject

Returns the value of attribute queue.



3
4
5
# File 'lib/ok_computer/built_in_checks/delayed_job_backed_up_check.rb', line 3

def queue
  @queue
end

#thresholdObject

Returns the value of attribute threshold.



3
4
5
# File 'lib/ok_computer/built_in_checks/delayed_job_backed_up_check.rb', line 3

def threshold
  @threshold
end

Instance Method Details

#sizeObject

Public: How many delayed jobs are pending within the given priority



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ok_computer/built_in_checks/delayed_job_backed_up_check.rb', line 36

def size
  if defined?(::Delayed::Backend::Mongoid::Job) && Delayed::Worker.backend == Delayed::Backend::Mongoid::Job
    query = greater_than_priority ? Delayed::Job.gte(priority: priority) : Delayed::Job.lte(priority: priority)
  else
    operator = greater_than_priority ? ">=" : "<="
    query = Delayed::Job.where("priority #{operator} ?", priority)
  end
  opts = {}
  opts[:queue] = queue if queue
  opts[:locked_at] = nil unless include_locked
  opts[:last_error] = nil unless include_errored
  query.where(opts).count
end