class Resque::Plugins::DisableJob::Rule

Rule

This class handles the references (aka redis key names) and some logic to interact with the DisableJob rules data structures

We use a few Redis structures:

Constants

JOBS_SET

Attributes

count[RW]
job_name[R]

Public Class Methods

new(job_name, arguments = [], digest = '') click to toggle source
# File lib/resque/plugins/disable_job/rule.rb, line 27
def initialize(job_name, arguments = [], digest = '')
  @job_name = job_name
  if arguments.is_a?(Enumerable)
    @arguments = arguments
  else
    @serialized_args = arguments
  end
  @rule_digest = digest unless digest.empty?
end

Public Instance Methods

all_rules_key() click to toggle source
# File lib/resque/plugins/disable_job/rule.rb, line 41
def all_rules_key
  @all_rules_key ||= main_set + ':' + @job_name
end
arguments() click to toggle source
# File lib/resque/plugins/disable_job/rule.rb, line 53
def arguments
  @arguments ||= JSON.parse(@serialized_args)
end
digest() click to toggle source
# File lib/resque/plugins/disable_job/rule.rb, line 57
def digest
  @rule_digest ||= Digest::SHA1.hexdigest(serialized_arguments)
end
main_set() click to toggle source
# File lib/resque/plugins/disable_job/rule.rb, line 37
def main_set
  JOBS_SET
end
match?(args) click to toggle source
# File lib/resque/plugins/disable_job/rule.rb, line 61
def match?(args)
  job_args = normalize_job_args(args)
  return true if job_args == arguments
  # We check each parameter in the job_args with the rule arguments to be blocked
  # if it's nil, then we match as we handle the 'any' case,
  # if it's specified, we check for equality (65 == 65)
  should_block = if arguments.is_a?(Hash)
    job_args.map { |k, v| match_or_nil(k, v) }
  else
    job_args.map.with_index { |a, i| match_or_nil(i, a) }
  end
  # `!should_block.empty?` handles the edge case of a job with no parameters and the rule args have parameters
  !should_block.empty? && !should_block.include?(false)
end
rule_key() click to toggle source
# File lib/resque/plugins/disable_job/rule.rb, line 45
def rule_key
  @rule_key ||= all_rules_key + ':' + digest
end
serialized_arguments() click to toggle source
# File lib/resque/plugins/disable_job/rule.rb, line 49
def serialized_arguments
  @serialized_args ||= @arguments.to_json
end

Protected Instance Methods

match_or_nil(key, value) click to toggle source
# File lib/resque/plugins/disable_job/rule.rb, line 87
def match_or_nil(key, value)
  arguments[key].nil? || value == arguments[key]
end
normalize_job_args(job_args) click to toggle source
# File lib/resque/plugins/disable_job/rule.rb, line 78
def normalize_job_args(job_args)
  # If the rule arguments is a hash we try to extract the job arguments as a hash to compare apples with apples
  if arguments.is_a?(Hash)
    job_args.size == 1 && job_args.first.is_a?(Hash) ? job_args.first : job_args
  else
    job_args
  end
end