forked from airbnb/optica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events_rmq.rb
62 lines (53 loc) · 1.55 KB
/
events_rmq.rb
1
2
3
4
5
6
7
8
9
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require 'stomp'
require 'oj'
class EventsRabbitMQ
def initialize(opts)
@log = opts['log']
%w{rabbit_host rabbit_port}.each do |req|
raise ArgumentError, "missing required argument '#{req}'" unless opts[req]
end
@connect_hash = {
:hosts => [{
:host => opts['rabbit_host'],
:port => opts['rabbit_port'],
:login => opts['rabbit_user'] || 'guest',
:passcode => opts['rabbit_pass'] || 'guest',
}],
:reliable => true,
:autoflush => true,
:connect_timeout => 10,
:logger => @log,
}
@exchange_name = opts['exchange_name'] || 'ops'
@routing = opts['routing'] || 'events.node.converged'
@health_routing = opts['health_routing'] || 'checks.optica'
end
def name
'rabbitmq'
end
def start
@client = Stomp::Client.new(@connect_hash)
end
def send(data)
@client.publish("/exchange/#{@exchange_name}/#{@routing}", Oj.dump(data), {:persistent => true})
rescue Exception => e
@log.error "unexpected error publishing to rabbitmq: #{e.inspect}"
raise e
else
@log.debug "published an event to #{@routing}"
end
def healthy?
@client.publish("/exchange/#{@exchange_name}/#{@health_routing}", '')
rescue StandardError => e
@log.error "events interface failed health check: #{e.inspect}"
false
else
@log.debug "events interface for RabbitMQ healthy"
true
end
def stop
@log.warn "stopping the events interface"
Process.kill("TERM", Process.pid) unless $EXIT
@client.close if @client
end
end