Class: Sensu::RabbitMQ

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu/rabbitmq.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRabbitMQ

Returns a new instance of RabbitMQ.



11
12
13
14
15
# File 'lib/sensu/rabbitmq.rb', line 11

def initialize
  @on_error = Proc.new {}
  @before_reconnect = Proc.new {}
  @after_reconnect = Proc.new {}
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



9
10
11
# File 'lib/sensu/rabbitmq.rb', line 9

def channel
  @channel
end

Class Method Details

.connect(options = {}) ⇒ Object



64
65
66
67
68
69
# File 'lib/sensu/rabbitmq.rb', line 64

def self.connect(options={})
  options ||= Hash.new
  rabbitmq = self.new
  rabbitmq.connect(options)
  rabbitmq
end

Instance Method Details

#after_reconnect(&block) ⇒ Object



25
26
27
# File 'lib/sensu/rabbitmq.rb', line 25

def after_reconnect(&block)
  @after_reconnect = block
end

#before_reconnect(&block) ⇒ Object



21
22
23
# File 'lib/sensu/rabbitmq.rb', line 21

def before_reconnect(&block)
  @before_reconnect = block
end

#closeObject



60
61
62
# File 'lib/sensu/rabbitmq.rb', line 60

def close
  @connection.close
end

#connect(options = {}) ⇒ Object



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
# File 'lib/sensu/rabbitmq.rb', line 29

def connect(options={})
  on_failure = Proc.new do
    error = RabbitMQError.new('cannot connect to rabbitmq')
    @on_error.call(error)
  end
  @connection = AMQP.connect(options, {
    :on_tcp_connection_failure => on_failure,
    :on_possible_authentication_failure => on_failure
  })
  @connection.logger = Logger.get
  @connection.on_tcp_connection_loss do |connection, settings|
    unless connection.reconnecting?
      @before_reconnect.call
      connection.periodically_reconnect(5)
    end
  end
  @channel = AMQP::Channel.new(@connection)
  @channel.auto_recovery = true
  @channel.on_error do |channel, channel_close|
    error = RabbitMQError.new('rabbitmq channel closed')
    @on_error.call(error)
  end
  @channel.on_recovery do
    @after_reconnect.call
  end
end

#connected?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/sensu/rabbitmq.rb', line 56

def connected?
  @connection.connected?
end

#on_error(&block) ⇒ Object



17
18
19
# File 'lib/sensu/rabbitmq.rb', line 17

def on_error(&block)
  @on_error = block
end