-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber.rb
executable file
·67 lines (48 loc) · 1.81 KB
/
subscriber.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
63
64
65
66
67
#!/usr/bin/env ruby
# encoding: utf-8
require 'rubygems'
require 'bundler'
Bundler.require(:default)
module CommandLineFlags extend OptiFlagSet
# (port: 55672) # http://localhost:55672/#/
optional_flag "amqphost"
optional_flag "exchange"
optional_flag "key"
and_process!
end
### INITIALIZE GLOBAL OPAD DEPLOYMENT SETTINGS ###
puts "Starting alerting_queue_reader with $opad_home=#{($opad_home ||= ENV['OPAD_HOME'])}"
begin puts "Please set $OPAD_HOME env var!"
exit end if $opad_home.nil?
require File.join($opad_home, 'common', 'read_config')
host = ARGV.flags.amqphost || $config[:support]['alerting_queue']['host']
exchange_name = ARGV.flags.exchange || $config[:support]['alerting_queue']['exchange']
key = ARGV.flags.key || $config[:opad]['aspect_id']
puts "ALERTING QUEUE CONSOLE WRITER"
puts "============================="
puts "Server: '#{host}'"
puts "Exchange: '#{exchange_name}'"
puts "Listening on key: '#{key}'"
puts "============================="
EventMachine.run do
connection = AMQP.connect(:host => host, :logging => true)
channel = AMQP::Channel.new(connection)
exchange = AMQP::Exchange.new(channel, :topic, exchange_name)
puts "Connecting to AMQP on host #{host}. Running #{AMQP::VERSION} version of the gem. Accessing exchange #{exchange_name}"
# get a new queue with an autogenerated name (first param = '')
queue = channel.queue('', :exclusive => true, :auto_delete => true)
queue.bind(exchange, :routing_key => key).subscribe do |payload|
begin
data = JSON.parse(payload)
data = data.first if data.is_a? Array
puts "#{payload}"
rescue => e
puts "error parsing: #{e}"
end
#connection.close {
# EventMachine.stop { exit }
# outfile.close
#}
end
#exchange.publish '{"count" : 666.666}', :routing_key => key
end