Class: Nerve::ServiceCheck::HttpServiceCheck

Inherits:
BaseServiceCheck show all
Defined in:
lib/nerve/service_watcher/http.rb

Instance Method Summary collapse

Methods inherited from BaseServiceCheck

#catch_errors, #up?

Methods included from Logging

configure_logger_for, #log, logger_for

Methods included from Utils

#responsive_sleep, #safe_run

Constructor Details

#initialize(opts = {}) ⇒ HttpServiceCheck

Returns a new instance of HttpServiceCheck.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/nerve/service_watcher/http.rb', line 8

def initialize(opts={})
  super

  %w{port uri}.each do |required|
    raise ArgumentError, "missing required argument #{required} in http check" unless
      opts[required]
    instance_variable_set("@#{required}",opts[required])
  end

  @host        = opts['host'] || '127.0.0.1'
  @ssl         = opts['ssl']  || false

  @read_timeout = opts['read_timeout'] || @timeout
  @open_timeout = opts['open_timeout'] || 0.2
  @ssl_timeout  = opts['ssl_timeout']  || 0.2

  @headers     = opts['headers'] || {}

  @expect      = opts['expect']

  @name        = "http-#{@host}:#{@port}#{@uri}"
end

Instance Method Details

#checkObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nerve/service_watcher/http.rb', line 31

def check
  log.debug "running health check #{@name}"

  connection = get_connection
  response = connection.get(@uri, @headers)
  code = response.code.to_i
  body = response.body

  # Any 2xx or 3xx code should be considered healthy. This is standard
  # practice in HAProxy, nginx, etc ...
  if code >= 200 and code < 400 and (@expect == nil || body.include?(@expect))
    log.debug "nerve: check #{@name} got response code #{code} with body \"#{body}\""
    return true
  else
    log.warn "nerve: check #{@name} got response code #{code} with body \"#{body}\""
    return false
  end
end