Skip to content

Instantly share code, notes, and snippets.

@googya
Created September 8, 2018 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save googya/884d1bf4a76a89442928f31a2bebd813 to your computer and use it in GitHub Desktop.
Save googya/884d1bf4a76a89442928f31a2bebd813 to your computer and use it in GitHub Desktop.
##### producer #####
require 'bunny'
require 'pry'
require 'json'
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.on('-r', '--routing_key key', 'Routing key for message') do |key|
options[:routing_key] = key
end
opts.on('-m', '--message CONTENT', 'Message text for alert') do |content|
options[:message] = content
end
end.parse!
conn = Bunny.new(user: 'alert_user', pass: 'alertme')
conn.start
ch = conn.create_channel
msg = options[:message].to_json
exchange_name = "alerts"
properties = { content_type: "application/json", durable: false }
ch.basic_publish(msg, exchange_name, options[:routing_key], properties)
puts "sent message #{JSON(msg)} with routing key #{options[:routing_key]} to exchange"
#### consumer #####
require 'bunny'
require 'pry'
conn = Bunny.new(user: "alert_user", pass: "alertme")
conn.start
critical = 'critical'
rate_limit = 'rate_limit'
ch = conn.create_channel
ch.exchange_declare("alerts", "topic", auto_delete: false)
ch.queue_declare(critical, auto_delete: false)
ch.queue_bind(critical, "alerts", routing_key: "critical.*" )
ch.queue_declare(rate_limit, auto_delete: false)
ch.queue_bind(rate_limit, "alerts", routing_key: "*.rate_limit")
ch.basic_consume(critical, critical, false) do |delivery_info, properties, payload|
puts payload
puts "sending email to admin about critical event"
ch.basic_ack(delivery_info.delivery_tag.to_i)
end
ch.basic_consume(rate_limit, rate_limit, false) do |delivery_info, c, payload|
puts payload
puts "sending email to admin about rate_limit"
ch.basic_ack(delivery_info.delivery_tag.to_i)
end
puts "Ready for alerts"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment