Class: RedisHealth

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

Constant Summary collapse

VERSION =
'0.0.1'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ RedisHealth

Returns a new instance of RedisHealth.



27
28
29
30
# File 'lib/redis_health.rb', line 27

def initialize(redis)
  @redis = redis
  @triggered_times = {}
end

Class Attribute Details

.notifierObject

Returns the value of attribute notifier.



7
8
9
# File 'lib/redis_health.rb', line 7

def notifier
  @notifier
end

.watchersObject

Returns the value of attribute watchers.



7
8
9
# File 'lib/redis_health.rb', line 7

def watchers
  @watchers
end

Instance Attribute Details

#redisObject

Returns the value of attribute redis.



3
4
5
# File 'lib/redis_health.rb', line 3

def redis
  @redis
end

#triggered_timesObject

Returns the value of attribute triggered_times.



3
4
5
# File 'lib/redis_health.rb', line 3

def triggered_times
  @triggered_times
end

Class Method Details

.configure(&blk) ⇒ Object



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

def configure(&blk)
  instance_eval(&blk)
end

.notify(&blk) ⇒ Object



22
23
24
# File 'lib/redis_health.rb', line 22

def notify(&blk)
  @notifier = blk
end

.watch(label, time = 60, &blk) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/redis_health.rb', line 13

def watch(label, time = 60, &blk)
  w = {
    :time => time,
    :matcher => blk
  }
  watchers[label] = w
  [label, w]
end

Instance Method Details

#execute_watchesObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/redis_health.rb', line 32

def execute_watches
  self.class.watchers.collect do |watcher_array|
    label, watcher_hash = watcher_array
    triggered_times[label] ||= 0

    matcher_result = self.instance_eval(&watcher_hash[:matcher])#.call(@redis)
    triggered = matcher_result[:triggered]
    value = matcher_result[:value]
    notice = append_label_value(label, value, triggered)
    times_was = triggered_times[label]

    if triggered
      triggered_times[label] += 1
      if times_was == watcher_hash[:time]
        notice
      end
    else
      triggered_times[label] = 0
      if times_was > 0 && times_was >= watcher_hash[:time]
        notice
      else
        nil
      end
    end
  end.compact
end

#notifyObject



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

def notify
  notices = execute_watches
  self.class.notifier.call(notices) unless notices.empty?
end

#runObject



64
65
66
67
68
69
# File 'lib/redis_health.rb', line 64

def run
  loop do
    notify
    sleep 1
  end
end