Class: Nerve::ServiceCheck::RabbitMQServiceCheck

Inherits:
BaseServiceCheck show all
Includes:
Socket::Constants
Defined in:
lib/nerve/service_watcher/rabbitmq.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 = {}) ⇒ RabbitMQServiceCheck

Returns a new instance of RabbitMQServiceCheck.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
# File 'lib/nerve/service_watcher/rabbitmq.rb', line 10

def initialize(opts={})
  super

  raise ArgumentError, "missing required argument 'port' in rabbitmq check" unless opts['port']

  @port = opts['port']
  @host = opts['host']     || '127.0.0.1'
  @user = opts['username'] || 'guest'
  @pass = opts['password'] || 'guest'
end

Instance Method Details

#checkObject



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nerve/service_watcher/rabbitmq.rb', line 21

def check
  # the idea for this check was taken from the one in rabbitmq management
  #  -- the aliveness_test:
  # https://github.com/rabbitmq/rabbitmq-management/blob/9a8e3d1ab5144e3f6a1cb9a4639eb738713b926d/src/rabbit_mgmt_wm_aliveness_test.erl
  log.debug "nerve: running rabbitmq health check #{@name}"

  conn = Bunny.new(
    :host => @host,
    :port => @port,
    :user => @user,
    :pass => @pass,
    :log_file => STDERR,
    :continuation_timeout => @timeout,
    :automatically_recover => false,
    :heartbeat => false,
    :threaded => false
  )

  begin
    conn.start
    ch = conn.create_channel

    # create a queue, publish to it
    log.debug "nerve: publishing to rabbitmq"
    ch.queue('nerve')
    ch.basic_publish('nerve test message', '', 'nerve', :mandatory => true, :expiration => 2 * 1000)

    # read and ack the message
    log.debug "nerve: consuming from rabbitmq"
    delivery_info, properties, payload = ch.basic_get('nerve', :ack => true)

    if payload
      ch.acknowledge(delivery_info.delivery_tag)
      return true
    else
      log.debug "nerve: rabbitmq consumption returned no payload"
      return false
    end
  ensure
    conn.close
  end
end