Class: SuccessTracker::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/success_tracker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis, options = {}) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/success_tracker.rb', line 11

def initialize(redis, options={})
  @redis = redis

  @rules = {
    :percent_10 => self.class.ratio_rule(0.1),
    :sequence_of_5 => self.class.sequence_rule(5),
  }.merge(options.delete(:rules) || {})
  @callbacks = options.delete(:callbacks) || {}
  raise ArgumentError unless options.empty?

  @list_length = 100
end

Instance Attribute Details

#callbacksObject

Returns the value of attribute callbacks.



9
10
11
# File 'lib/success_tracker.rb', line 9

def callbacks
  @callbacks
end

#list_lengthObject

Returns the value of attribute list_length.



9
10
11
# File 'lib/success_tracker.rb', line 9

def list_length
  @list_length
end

#redisObject

Returns the value of attribute redis.



9
10
11
# File 'lib/success_tracker.rb', line 9

def redis
  @redis
end

#rulesObject

Returns the value of attribute rules.



9
10
11
# File 'lib/success_tracker.rb', line 9

def rules
  @rules
end

Class Method Details

.ratio_rule(ratio = 0.1, minimum = 10) ⇒ Object

returns true if the failure ratio is higher than x (with a minimum of 10 records)



60
61
62
# File 'lib/success_tracker.rb', line 60

def self.ratio_rule(ratio=0.1, minimum=10)
  lambda { |list| list.length >= minimum and list.select(&:empty?).length.to_f / list.length >= ratio }
end

.sequence_rule(elements = 5) ⇒ Object

returns true if the last x elements have failed



65
66
67
# File 'lib/success_tracker.rb', line 65

def self.sequence_rule(elements=5)
  lambda { |list| list.length >= elements && list[0..elements-1].reject(&:empty?).length == 0 }
end

Instance Method Details

#failure(identifier, notify_rule, options = {}) ⇒ Object

identifier is the key used for grouping success and failure cases together. notify_rule is a symbol identifying the code block which is evaluated to know if the error is significant or not. + options+ is a hash which currently can only contain the list of exceptions which should be tagged with the NonSignificantError module the given block is always evaluated and the resulting errors are tagged with the NonSignificantError and reraised



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/success_tracker.rb', line 35

def failure(identifier, notify_rule, options={})
  callbacks[:failure].call(identifier) if callbacks[:failure]
  store(identifier, nil)

  redis.del(prefix(identifier)) if notify = rules[notify_rule].call(redis.lrange(prefix(identifier), 0,-1))

  begin
    yield if block_given?
  rescue *(options[:exceptions] || [StandardError]) => error
    error.extend(NonSignificantError) unless notify
    raise
  end

  return notify
end

#prefix(identifier) ⇒ Object



24
# File 'lib/success_tracker.rb', line 24

def prefix(identifier); "success_tracker:#{identifier}" end

#success(identifier) ⇒ Object



26
27
28
29
# File 'lib/success_tracker.rb', line 26

def success(identifier)
  callbacks[:success].call(identifier) if callbacks[:success]
  store(identifier, "1")
end

#track(identifier, notify_rule, options = {}) ⇒ Object

yields the given code block and then marks success. In case a exception was triggered it marks a failure and reraises the exception (for the arguments see the #failure method)



52
53
54
55
56
# File 'lib/success_tracker.rb', line 52

def track(identifier, notify_rule, options={})
  yield.tap { success(identifier) }
rescue => exception
  failure(identifier, notify_rule, options) { raise exception }
end