Class: Nerve::ServiceCheck::BaseServiceCheck

Inherits:
Object
  • Object
show all
Includes:
Logging, Utils
Defined in:
lib/nerve/service_watcher/base.rb

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #log, logger_for

Methods included from Utils

#safe_run

Constructor Details

#initialize(opts = {}) ⇒ BaseServiceCheck

Returns a new instance of BaseServiceCheck.



7
8
9
10
11
12
13
14
15
# File 'lib/nerve/service_watcher/base.rb', line 7

def initialize(opts={})
  @timeout = opts['timeout'] ? opts['timeout'].to_f : 0.1
  @rise    = opts['rise']    ? opts['rise'].to_i    : 1
  @fall    = opts['fall']    ? opts['fall'].to_i    : 1
  @name    = opts['name']    ? opts['name']         : "undefined"

  @check_buffer = RingBuffer.new([@rise, @fall].max)
  @last_result = nil
end

Instance Method Details

#catch_errors(&block) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/nerve/service_watcher/base.rb', line 49

def catch_errors(&block)
  begin
    return yield
  rescue Object => error
    log.info "nerve: service check #{@name} got error #{error.inspect}"
    return false
  end
end

#up?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nerve/service_watcher/base.rb', line 17

def up?
  # do the check
  check_result = !!catch_errors do
    check
  end

  # this is the first check -- initialize buffer
  if @last_result == nil
    @last_result = check_result
    @check_buffer.size.times {@check_buffer.push check_result}
    log.info "nerve: service check #{@name} initial check returned #{check_result}"
  end

  log.debug "nerve: service check #{@name} returned #{check_result}"
  @check_buffer.push(check_result)

  # we've failed if the last @fall times are false
  unless @check_buffer.last(@fall).reduce(:|)
    log.info "nerve: service check #{@name} transitions to down after #{@fall} failures" if @last_result
    @last_result = false
  end

  # we've succeeded if the last @rise times is true
  if @check_buffer.last(@rise).reduce(:&)
    log.info "nerve: service check #{@name} transitions to up after #{@rise} successes" unless @last_result
    @last_result = true
  end

  # otherwise return the last result
  return @last_result
end