Class: Resque::Plugins::DisableJob::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/resque/plugins/disable_job/rule.rb

Overview

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:

  • ‘main_set` - is Redis Set where we store all the jobs that are disabled at one time

  • ‘all_rules_key` - is the Redis Hash that stores all the arguments we disable for one job

  • ‘rule_key` - a Redis Counter that stores how many times we disabled that job with the specific parameters

    This key has a TTL equal with the timeout set, so if it's gone we won't disable the job
    
  • ‘serialized_arguments` - the job’s specified arguments in JSON format

  • ‘arguments` - the job’s specified arguments

  • ‘digest` - the job’s specified arguments as a digest; used to identify the job rules in the key

Constant Summary collapse

JOBS_SET =
'disabled_jobs'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_name, arguments = [], digest = '') ⇒ Rule

Returns a new instance of Rule.



27
28
29
30
31
32
33
34
35
# 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

Instance Attribute Details

#countObject

Returns the value of attribute count.



25
26
27
# File 'lib/resque/plugins/disable_job/rule.rb', line 25

def count
  @count
end

#job_nameObject (readonly)

Returns the value of attribute job_name.



24
25
26
# File 'lib/resque/plugins/disable_job/rule.rb', line 24

def job_name
  @job_name
end

Instance Method Details

#all_rules_keyObject



41
42
43
# File 'lib/resque/plugins/disable_job/rule.rb', line 41

def all_rules_key
  @all_rules_key ||= "#{main_set}:#{@job_name}"
end

#argumentsObject



53
54
55
# File 'lib/resque/plugins/disable_job/rule.rb', line 53

def arguments
  @arguments ||= JSON.parse(@serialized_args)
end

#digestObject



57
58
59
# File 'lib/resque/plugins/disable_job/rule.rb', line 57

def digest
  @rule_digest ||= Digest::SHA1.hexdigest(serialized_arguments) # rubocop:disable Naming/MemoizedInstanceVariableName
end

#main_setObject



37
38
39
# File 'lib/resque/plugins/disable_job/rule.rb', line 37

def main_set
  JOBS_SET
end

#match?(args) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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_keyObject



45
46
47
# File 'lib/resque/plugins/disable_job/rule.rb', line 45

def rule_key
  @rule_key ||= "#{all_rules_key}:#{digest}"
end

#serialized_argumentsObject



49
50
51
# File 'lib/resque/plugins/disable_job/rule.rb', line 49

def serialized_arguments
  @serialized_args ||= @arguments.to_json # rubocop:disable Naming/MemoizedInstanceVariableName
end