Class: Nerve::ServiceWatcher

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

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #log, logger_for

Methods included from Utils

#safe_run

Constructor Details

#initialize(service = {}) ⇒ ServiceWatcher

Returns a new instance of ServiceWatcher.



10
11
12
13
14
15
16
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
# File 'lib/nerve/service_watcher.rb', line 10

def initialize(service={})
  log.debug "nerve: creating service watcher object"

  # check that we have all of the required arguments
  %w{name instance_id host port}.each do |required|
    raise ArgumentError, "missing required argument #{required} for new service watcher" unless service[required]
  end

  @name = service['name']

  # configure the reporter, which we use for talking to zookeeper
  @reporter = Reporter.new_from_service(service)

  # instantiate the checks for this service
  @service_checks = []
  service['checks'] ||= []
  service['checks'].each do |check|
    check['type'] ||= "undefined"
    begin
      service_check_class = ServiceCheck::CHECKS[check['type']]
    rescue
      raise ArgumentError,
        "invalid service check type #{check['type']}; valid types: #{ServiceCheck::CHECKS.keys.join(',')}"
    end

    check['host'] ||= service['host']
    check['port'] ||= service['port']
    check['name'] ||= "#{@name} #{check['type']}-#{check['host']}:#{check['port']}"
    @service_checks << service_check_class.new(check)
  end

  # how often do we initiate service checks?
  @check_interval = service['check_interval'] || 0.5

  log.debug "nerve: created service watcher for #{@name} with #{@service_checks.size} checks"
end

Instance Method Details

#check?Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/nerve/service_watcher.rb', line 83

def check?
  @service_checks.each do |check|
    return false unless check.up?
  end
  return true
end

#runObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nerve/service_watcher.rb', line 47

def run()
  log.info "nerve: starting service watch #{@name}"

  @reporter.start()
  was_up = false

  until $EXIT
    @reporter.ping?

    # what is the status of the service?
    is_up = check?
    log.debug "nerve: current service status for #{@name} is #{is_up.inspect}"

    if is_up != was_up
      if is_up
        @reporter.report_up
        log.info "nerve: service #{@name} is now up"
      else
        @reporter.report_down
        log.warn "nerve: service #{@name} is now down"
      end
      was_up = is_up
    end

    # wait to run more checks
    sleep @check_interval
  end
rescue StandardError => e
  log.error "nerve: error in service watcher #{@name}: #{e.inspect}"
  raise e
ensure
  log.info "nerve: ending service watch #{@name}"
  $EXIT = true
  @reporter.stop
end