Skip to content

Instantly share code, notes, and snippets.

@erenfro
Last active August 29, 2015 14:27
Show Gist options
  • Save erenfro/9a3cbe4ac4b1407afd5d to your computer and use it in GitHub Desktop.
Save erenfro/9a3cbe4ac4b1407afd5d to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
class Consumer
attr_reader :active
require "bunny"
def initialize
@active = false
end
def run
if not @active
@active = true
puts "[RMQ] Starting up!"
@rabbitmq_conn = Bunny.new(
host: "localhost",
port: 5672,
vhost: "/testing",
user: "sensu",
password: "secret"
)
@rabbitmq_conn.start
@rabbitmq_channel = @rabbitmq_conn.create_channel
@rabbitmq_exchange = @rabbitmq_channel.direct(
"wizardbus",
auto_delete: false,
durable: true
)
@rabbitmq_queue = @rabbitmq_channel.queue(
"wizardbus.receiver1",
auto_delete: false,
durable: true
).bind(
@rabbitmq_exchange,
routing_key: "queue",
durable: true
)
@rabbitmq_queue.subscribe do |delivery_info, metadata, payload|
puts "[RMQ] Received data..."
puts "[RMQ] Delivery: #{delivery_info}"
puts "[RMQ] Metadata: #{metadata}"
puts "[RMQ] Payload : #{payload}"
sleep 2
end
else
puts "[RMQ] Already running."
end
end
def stop
puts "[RMQ] I'm stopping"
@rabbitmq_conn.close
@active = false
end
def add(endpoint, type, event)
@rabbitmq_exchange.publish("queue",
routing_key: "queue",
persistent: true,
headers: {
endpoint: endpoint,
type: type,
event: event
})
end
end
begin
c = Consumer.new
c.run
Signal.trap("TERM") do
puts "Main thread terminating"
c.stop
end
Signal.trap("USR1") do
c.stop
c.run
end
10.times do |item|
c.add("influxdb", "check", {data: item.to_s, type: "metric"})
end
10.times do |tick|
#if tick == 4
# c.stop
#elsif tick == 6
# c.run
#end
if c.active
puts "Main thread still going #{tick}... Thread running."
else
puts "Main thread still going #{tick}... Thread NOT running."
end
sleep 1
end
c.stop
puts "Main thread ending."
rescue Interrupt => e
c.stop
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment