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,2 @@
module Sylvilagus
end

View File

@@ -0,0 +1,4 @@
module Sylvilagus
module Ch02
end
end

View File

@@ -0,0 +1,23 @@
require 'sylvilagus/init'
require 'sylvilagus/ch02'
require 'rabbitmq-client.jar'
import com.rabbitmq.client.ConnectionFactory
module Sylvilagus::Ch02::HelloWorld
module ClassMethods
def with_hello_world_channel(&block)
factory = ConnectionFactory.new
factory.uri = ENV.fetch('SYLVILAGUS_AMQP_URI')
conn = factory.new_connection
channel = conn.create_channel
channel.exchange_declare('hello-exchange', 'direct', true)
channel.queue_declare('hello-queue', false, false, false, nil)
block.call(channel)
ensure
conn.close
end
end
end

View File

@@ -0,0 +1,41 @@
require 'sylvilagus/ch02/hello_world'
class Sylvilagus::Ch02::HelloWorldConsumer < Java::ComRabbitmqClient::DefaultConsumer
class << self
include Sylvilagus::Ch02::HelloWorld::ClassMethods
def main
with_hello_world_channel do |channel|
consumer = new(channel)
STDERR.puts 'Starting consume loop...'
channel.basic_consume('hello-queue', false, consumer)
loop do
sleep 1
break if consumer.done?
end
end
end
end
def done?
@done ||= false
end
def handleDelivery(consumer_tag, envelope, properties, body)
body_string = Java::OrgJruby::RubyString.bytes_to_string(body)
puts "Consumed #{body_string.inspect}"
channel.basic_ack(envelope.delivery_tag, false)
if body_string == 'quit'
STDERR.puts 'Quitting...'
@done = true
end
end
end
if $0 == __FILE__
Sylvilagus::Ch02::HelloWorldConsumer.main
end

View File

@@ -0,0 +1,20 @@
require 'sylvilagus/ch02/hello_world'
class Sylvilagus::Ch02::HelloWorldProducer
class << self
include Sylvilagus::Ch02::HelloWorld::ClassMethods
def main(args = ARGV.clone)
raise 'Missing message arg!' if args.empty?
message = args.fetch(0)
with_hello_world_channel do |channel|
channel.basic_publish('hello-exchange', 'hola', nil, message.to_java_bytes)
puts "Published #{message.inspect}"
end
end
end
end
if $0 == __FILE__
Sylvilagus::Ch02::HelloWorldProducer.main(ARGV)
end

View File

@@ -0,0 +1,4 @@
module Sylvilagus
module Ch03
end
end

View File

@@ -0,0 +1,74 @@
require 'sylvilagus/init'
require 'sylvilagus/ch03'
require 'rabbitmq-client.jar'
import com.rabbitmq.client.ConnectionFactory
import com.rabbitmq.client.DefaultConsumer
import org.jruby.RubyString
class Sylvilagus::Ch03::LogListeners
class LogConsumer < DefaultConsumer
attr_accessor :level
def handleDelivery(consumer_tag, envelope, properties, body)
body_string = RubyString.bytes_to_string(body)
puts "#{level}: #{body_string}"
channel.basic_ack(envelope.delivery_tag, false)
end
end
def main
factory = ConnectionFactory.new
amqp_uri = ENV.fetch('SYLVILAGUS_ROOT_ADMIN_AMQP_URI')
factory.uri = amqp_uri
puts "Getting connection for #{amqp_uri.inspect}..."
@conn = factory.new_connection
trap :INT do
begin
@conn.close
exit 1
rescue NativeException
exit 2
end
end
puts 'Getting channel...'
channel = @conn.create_channel
puts 'Declaring queues...'
errors_queue = channel.queue_declare.get_queue
warnings_queue = channel.queue_declare.get_queue
info_queue = channel.queue_declare.get_queue
puts "Binding queues to 'amq.rabbitmq.log' exchange..."
channel.queue_bind(errors_queue, 'amq.rabbitmq.log', 'error')
channel.queue_bind(warnings_queue, 'amq.rabbitmq.log', 'warning')
channel.queue_bind(info_queue, 'amq.rabbitmq.log', 'info')
errors_consumer = LogConsumer.new(channel)
errors_consumer.level = 'error'
warnings_consumer = LogConsumer.new(channel)
warnings_consumer.level = 'warning'
info_consumer = LogConsumer.new(channel)
info_consumer.level = 'info'
puts 'Setting up consumers...'
channel.basic_consume(errors_queue, false, errors_consumer)
channel.basic_consume(warnings_queue, false, warnings_consumer)
channel.basic_consume(info_queue, false, info_consumer)
loop do
sleep 1
end
ensure
begin
@conn.close if @conn
rescue NativeException
end
end
end
if $0 == __FILE__
Sylvilagus::Ch03::LogListeners.new.main
end

View File

@@ -0,0 +1,4 @@
module Sylvilagus
module Ch04
end
end

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

View File

@@ -0,0 +1,9 @@
require 'java'
SYLVILAGUS_JAVA_LIBS = File.expand_path('../../java/', __FILE__)
$CLASSPATH << SYLVILAGUS_JAVA_LIBS
Dir["#{SYLVILAGUS_JAVA_LIBS}/*.jar"].each do |jar|
$CLASSPATH << jar
end