Tutorial 4 ported to Bunny

This commit is contained in:
Michael Klishin 2013-07-18 00:16:10 +04:00
parent 8b5971a1eb
commit bd8b41363b
2 changed files with 50 additions and 0 deletions

18
ruby/emit_log_direct.rb Normal file
View File

@ -0,0 +1,18 @@
#!/usr/bin/env ruby
# encoding: utf-8
require "bunny"
conn = Bunny.new
conn.start
ch = conn.create_channel
x = ch.direct("direct_logs")
severity = ARGV.shift || "info"
msg = ARGV.empty? ? "Hello World!" : ARGV.join(" ")
x.publish(msg, :routing_key => severity)
puts " [x] Sent '#{msg}'"
sleep 0.5
conn.close

View File

@ -0,0 +1,32 @@
#!/usr/bin/env ruby
# encoding: utf-8
require "bunny"
if ARGV.empty?
abort "Usage: #{$0} [info] [warning] [error]"
end
conn = Bunny.new
conn.start
ch = conn.create_channel
x = ch.direct("direct_logs")
q = ch.queue("", :exclusive => true)
ARGV.each do |severity|
q.bind(x, :routing_key => severity)
end
puts " [*] Waiting for logs. To exit press CTRL+C"
begin
q.subscribe(:block => true) do |delivery_info, properties, body|
puts " [x] #{body}"
end
rescue Interrupt => _
puts " [*] Shutting down..."
ch.close
conn.close
end