Archiving a bunch of old stuff

This commit is contained in:
Dan Buch
2015-06-22 13:15:42 -05:00
parent a6ec1d560e
commit bd1abd8734
395 changed files with 1 additions and 76 deletions

View File

@@ -0,0 +1,60 @@
require 'sylvilagus/init'
require 'sylvilagus/ch04'
require 'json'
require 'rabbitmq-client.jar'
class Sylvilagus::Ch04::AddPointsConsumer
class Consumer < Java::ComRabbitmqClient::DefaultConsumer
def handleDelivery(consumer_tag, envelope, properties, body)
message = Java::OrgJruby::RubyString.bytes_to_string(body)
if message == 'quit'
channel.basic_cancel(consumer_tag)
@done = true
return
end
add_points_to_user(JSON.parse(message).fetch('user_id'))
channel.basic_ack(envelope.delivery_tag, false)
rescue StandardError
channel.basic_nack(envelope.delivery_tag, false)
end
def add_points_to_user(user_id)
puts "Adding points to user: #{user_id}"
end
def done?
!!@done
end
end
def main
factory = Java::ComRabbitmqClient::ConnectionFactory.new
factory.uri = ENV.fetch('SYLVILAGUS_AMQP_URI')
@conn = factory.new_connection
channel = @conn.create_channel
channel.exchange_declare(
'upload-pictures', 'fanout', false, true, false, nil
)
channel.queue_declare('add-points', false, false, false, nil)
channel.queue_bind('add-points', 'upload-pictures', '')
consumer = Consumer.new(channel)
puts "Consuming from 'upload-pictures' exchange"
channel.basic_consume('add-points', false, 'add-points-consumer',
false, false, nil, consumer)
loop do
break if consumer.done?
sleep 1
end
return 0
ensure
@conn.close if @conn
end
end
if $0 == __FILE__
exit Sylvilagus::Ch04::AddPointsConsumer.new.main
end

View 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

View File

@@ -0,0 +1,49 @@
require 'sylvilagus/init'
require 'sylvilagus/ch04'
require 'optparse'
require 'json'
require 'rabbitmq-client.jar'
class Sylvilagus::Ch04::AlertProducer
def main
options = {}
OptionParser.new do |opts|
opts.on('-rROUTING_KEY', '--routing-key=ROUTING_KEY',
'Routing key for message (e.g. myalert.im)') do |r|
options[:routing_key] = r
end
opts.on('-mMESSAGE', '--message=MESSAGE',
'Message text for alert.') do |m|
options[:message] = m
end
end.parse!
unless options[:message] && options[:routing_key]
STDERR.puts 'Need both message and routing_key!'
exit 1
end
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)
props = Java::ComRabbitmqClient::AMQP::BasicProperties.new
props.content_type = 'application/json'
json_msg = JSON.dump({'message' => options[:message]})
channel.basic_publish('alerts', options[:routing_key],
true, true, props, json_msg.to_java_bytes)
puts "Sent message #{json_msg.inspect} tagged " <<
"with routing key #{options[:routing_key].inspect} to " <<
'exchange "alerts" on vhost "/".'
ensure
@conn.close if @conn
end
end
if $0 == __FILE__
Sylvilagus::Ch04::AlertProducer.new.main
end

View File

@@ -0,0 +1,43 @@
require 'sylvilagus/init'
require 'sylvilagus/ch04'
require 'json'
require 'rabbitmq-client.jar'
class Sylvilagus::Ch04::FanoutPublisher
def main(argv = ARGV.clone)
unless argv.length == 3
STDERR.puts "Usage: #{File.basename($0)} <image-id> <user-id> <image-path>"
return 1
end
message = {
'image_id' => Integer(argv.fetch(0)),
'user_id' => Integer(argv.fetch(1)),
'image_path' => argv.fetch(2)
}
factory = Java::ComRabbitmqClient::ConnectionFactory.new
factory.uri = ENV.fetch('SYLVILAGUS_AMQP_URI')
@conn = factory.new_connection
channel = @conn.create_channel
channel.exchange_declare(
'upload-pictures', 'fanout', false, true, false, nil
)
props = Java::ComRabbitmqClient::AMQP::BasicProperties.new
props.content_type = 'application/json'
props.delivery_mode = 2
json_msg = JSON.dump(message)
channel.basic_publish('upload-pictures', '', props, json_msg.to_java_bytes)
puts "Sent message #{json_msg.inspect} tagged " <<
'exchange "upload-pictures" on vhost "/".'
return 0
ensure
@conn.close if @conn
end
end
if $0 == __FILE__
exit Sylvilagus::Ch04::FanoutPublisher.new.main
end

View File

@@ -0,0 +1,62 @@
require 'sylvilagus/init'
require 'sylvilagus/ch04'
require 'json'
require 'rabbitmq-client.jar'
class Sylvilagus::Ch04::ResizePictureConsumer
class Consumer < Java::ComRabbitmqClient::DefaultConsumer
def handleDelivery(consumer_tag, envelope, properties, body)
message = Java::OrgJruby::RubyString.bytes_to_string(body)
if message == 'quit'
channel.basic_cancel(consumer_tag)
@done = true
return
end
message_hash = JSON.parse(message)
resize_picture(message_hash.fetch('image_id'),
message_hash.fetch('image_path'))
channel.basic_ack(envelope.delivery_tag, false)
rescue StandardError
channel.basic_nack(envelope.delivery_tag, false)
end
def resize_picture(image_id, image_path)
puts "Resizing picture: #{image_id} #{image_path}"
end
def done?
!!@done
end
end
def main
factory = Java::ComRabbitmqClient::ConnectionFactory.new
factory.uri = ENV.fetch('SYLVILAGUS_AMQP_URI')
@conn = factory.new_connection
channel = @conn.create_channel
channel.exchange_declare(
'upload-pictures', 'fanout', false, true, false, nil
)
channel.queue_declare('resize-picture', false, false, false, nil)
channel.queue_bind('resize-picture', 'upload-pictures', '')
consumer = Consumer.new(channel)
puts "Consuming from 'upload-pictures' exchange"
channel.basic_consume('resize-picture', false, 'resize-picture-consumer',
false, false, nil, consumer)
loop do
break if consumer.done?
sleep 1
end
return 0
ensure
@conn.close if @conn
end
end
if $0 == __FILE__
exit Sylvilagus::Ch04::ResizePictureConsumer.new.main
end