Finishing the consumer side of the alert example from chapter 4
This commit is contained in:
parent
4acbd0a911
commit
91f9d80e16
@ -1,6 +1,7 @@
|
||||
source :rubygems
|
||||
|
||||
gem 'json_pure'
|
||||
gem 'jruby-openssl'
|
||||
|
||||
group :development, :test do
|
||||
gem 'rspec'
|
||||
|
@ -1,7 +1,10 @@
|
||||
GEM
|
||||
remote: http://rubygems.org/
|
||||
specs:
|
||||
bouncy-castle-java (1.5.0146.1)
|
||||
diff-lcs (1.1.3)
|
||||
jruby-openssl (0.7.7)
|
||||
bouncy-castle-java (>= 1.5.0146.1)
|
||||
json_pure (1.7.5)
|
||||
rspec (2.12.0)
|
||||
rspec-core (~> 2.12.0)
|
||||
@ -16,5 +19,6 @@ PLATFORMS
|
||||
java
|
||||
|
||||
DEPENDENCIES
|
||||
jruby-openssl
|
||||
json_pure
|
||||
rspec
|
||||
|
93
sylvilagus/jruby/lib/sylvilagus/ch04/alert_consumer.rb
Normal file
93
sylvilagus/jruby/lib/sylvilagus/ch04/alert_consumer.rb
Normal file
@ -0,0 +1,93 @@
|
||||
require 'sylvilagus/init'
|
||||
require 'sylvilagus/ch04'
|
||||
require 'json'
|
||||
require 'net/smtp'
|
||||
require 'rabbitmq-client.jar'
|
||||
|
||||
class Sylvilagus::Ch04::AlertProducer
|
||||
OPS_EMAILS = ['me@localhost'].freeze
|
||||
ADMIN_EMAILS = ['me@localhost'].freeze
|
||||
|
||||
def self.send_email(recipients, subject, body)
|
||||
Net::SMTP.start('localhost', 25) do |smtp|
|
||||
msgstr = <<-EOM.gsub(/^ /, '')
|
||||
To: #{recipients.join(', ')}
|
||||
From: alerts@sylvilagus.local
|
||||
Subject: #{subject}
|
||||
Date: #{Time.now}
|
||||
|
||||
#{body}
|
||||
EOM
|
||||
smtp.send_message(msgstr, 'alerts@sylvilagus.local', recipients)
|
||||
end
|
||||
end
|
||||
|
||||
class CriticalNotifier < Java::ComRabbitmqClient::DefaultConsumer
|
||||
def handleDelivery(consumer_tag, envelope, properties, body)
|
||||
message = JSON.parse(
|
||||
Java::OrgJruby::RubyString.bytes_to_string(body)
|
||||
).fetch('message')
|
||||
|
||||
Sylvilagus::Ch04::AlertProducer.send_email(
|
||||
OPS_EMAILS, 'CRITICAL ALERT!', message
|
||||
)
|
||||
puts "Sent alert via email! Alert text: #{message}"
|
||||
puts "Recipients: #{OPS_EMAILS}"
|
||||
|
||||
channel.basic_ack(envelope.delivery_tag, false)
|
||||
end
|
||||
end
|
||||
|
||||
class RateLimitNotifier < Java::ComRabbitmqClient::DefaultConsumer
|
||||
def handleDelivery(consumer_tag, envelope, properties, body)
|
||||
message = JSON.parse(
|
||||
Java::OrgJruby::RubyString.bytes_to_string(body)
|
||||
).fetch('message')
|
||||
|
||||
Sylvilagus::Ch04::AlertProducer.send_email(
|
||||
ADMIN_EMAILS, 'RATE LIMIT ALERT!', message
|
||||
)
|
||||
puts "Sent alert via email! Alert text: #{message}"
|
||||
puts "Recipients: #{ADMIN_EMAILS}"
|
||||
|
||||
channel.basic_ack(envelope.delivery_tag, false)
|
||||
end
|
||||
end
|
||||
|
||||
def main
|
||||
factory = Java::ComRabbitmqClient::ConnectionFactory.new
|
||||
factory.uri = ENV.fetch('SYLVILAGUS_ALERT_AMQP_URI')
|
||||
@conn = factory.new_connection
|
||||
channel = @conn.create_channel
|
||||
|
||||
channel.exchange_declare('alerts', 'topic', true, false, false, nil)
|
||||
channel.queue_declare('critical', true, false, false, nil)
|
||||
channel.queue_bind('critical', 'alerts', 'critical.*', nil)
|
||||
channel.queue_declare('rate_limit', true, false, false, nil)
|
||||
channel.queue_bind('rate_limit', 'alerts', '*.rate_limit', nil)
|
||||
|
||||
critical_notifier = CriticalNotifier.new(channel)
|
||||
rate_limit_notifier = RateLimitNotifier.new(channel)
|
||||
|
||||
channel.basic_consume(
|
||||
'critical', false, 'critical', critical_notifier
|
||||
)
|
||||
channel.basic_consume(
|
||||
'rate_limit', false, 'rate_limit', rate_limit_notifier
|
||||
)
|
||||
|
||||
trap(:INT) { exit 0 }
|
||||
|
||||
puts 'Starting consumer loop...'
|
||||
loop { sleep 1 }
|
||||
ensure
|
||||
begin
|
||||
@conn.close if @conn
|
||||
rescue NativeException
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if $0 == __FILE__
|
||||
Sylvilagus::Ch04::AlertProducer.new.main
|
||||
end
|
Loading…
Reference in New Issue
Block a user