Skip to content

Commit

Permalink
[service_watcher] refactor to make testing easier
Browse files Browse the repository at this point in the history
  • Loading branch information
igor47 committed Jan 16, 2015
1 parent 89c80f6 commit 3b06553
Showing 1 changed file with 42 additions and 30 deletions.
72 changes: 42 additions & 30 deletions lib/nerve/service_watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ def initialize(service={})
@service_checks = []
service['checks'] ||= []
service['checks'].each do |check|
# checks inherit attributes from the service overall
check['host'] ||= service['host']
check['port'] ||= service['port']

# generate a nice readable name for each check
check['name'] ||= "#{@name} #{check['type']}-#{check['host']}:#{check['port']}"

# instantiate the check
check['type'] ||= "undefined"
begin
unless ServiceCheck::CHECKS[check['type']]
Expand All @@ -36,51 +44,26 @@ def initialize(service={})
"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']}"
# save the check object
@service_checks << service_check_class.new(check)
end

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

# assume the services start as down
@was_up = false

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

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 but make sure to exit if $EXIT
# we avoid sleeping for the entire check interval at once
# so that nerve can exit promptly if required
nap_time = @check_interval
while nap_time > 0
break if $EXIT
sleep [nap_time, 1].min
nap_time -= 1
end
check_and_report
end
rescue StandardError => e
log.error "nerve: error in service watcher #{@name}: #{e.inspect}"
Expand All @@ -91,6 +74,35 @@ def run()
@reporter.stop
end

def check_and_report
@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 but make sure to exit if $EXIT
# we avoid sleeping for the entire check interval at once
# so that nerve can exit promptly if required
nap_time = @check_interval
while nap_time > 0
break if $EXIT
sleep [nap_time, 1].min
nap_time -= 1
end
end

def check?
@service_checks.each do |check|
return false unless check.up?
Expand Down

0 comments on commit 3b06553

Please sign in to comment.