Skip to content

Commit

Permalink
Update ruby tutorial five
Browse files Browse the repository at this point in the history
  • Loading branch information
Caique Hitoshi Mitsuoka committed Feb 19, 2018
1 parent 30f0e18 commit d241706
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
22 changes: 10 additions & 12 deletions ruby/emit_log_topic.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#!/usr/bin/env ruby
# encoding: utf-8
require 'bunny'

require "bunny"
connection = Bunny.new(automatically_recover: false)
connection.start

conn = Bunny.new(:automatically_recover => false)
conn.start
channel = connection.create_channel
exchange = channel.topic('topic_logs')
severity = ARGV.shift || 'anonymous.info'
message = ARGV.empty? ? 'Hello World!' : ARGV.join(' ')

ch = conn.create_channel
x = ch.topic("topic_logs")
severity = ARGV.shift || "anonymous.info"
msg = ARGV.empty? ? "Hello World!" : ARGV.join(" ")
exchange.publish(message, routing_key: severity)
puts " [x] Sent #{severity}:#{message}"

x.publish(msg, :routing_key => severity)
puts " [x] Sent #{severity}:#{msg}"

conn.close
connection.close
28 changes: 12 additions & 16 deletions ruby/receive_logs_topic.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
#!/usr/bin/env ruby
# encoding: utf-8
require 'bunny'

require "bunny"
abort "Usage: #{$PROGRAM_NAME} [binding key]" if ARGV.empty?

if ARGV.empty?
abort "Usage: #{$0} [binding key]"
end

conn = Bunny.new(:automatically_recover => false)
conn.start
connection = Bunny.new(automatically_recover: false)
connection.start

ch = conn.create_channel
x = ch.topic("topic_logs")
q = ch.queue("", :exclusive => true)
channel = connection.create_channel
exchange = channel.topic('topic_logs')
queue = channel.queue('', exclusive: true)

ARGV.each do |severity|
q.bind(x, :routing_key => severity)
queue.bind(exchange, routing_key: severity)
end

puts " [*] Waiting for logs. To exit press CTRL+C"
puts ' [*] Waiting for logs. To exit press CTRL+C'

begin
q.subscribe(:block => true) do |delivery_info, properties, body|
queue.subscribe(block: true) do |delivery_info, _properties, body|
puts " [x] #{delivery_info.routing_key}:#{body}"
end
rescue Interrupt => _
ch.close
conn.close
channel.close
connection.close

exit(0)
end

0 comments on commit d241706

Please sign in to comment.