Archiving a bunch of old stuff
This commit is contained in:
4
oldstuff/sylvilagus/.example.env
Normal file
4
oldstuff/sylvilagus/.example.env
Normal file
@@ -0,0 +1,4 @@
|
||||
export SYLVILAGUS_ROOT="$HOME/src/box-o-sand/src/sylvilagus"
|
||||
export SYLVILAGUS_AMQP_URI="amqp://sylvilagus:$(cat $SYLVILAGUS_ROOT/.sylvilagus-passwd)@localhost:5672/warren"
|
||||
export SYLVILAGUS_ALERT_AMQP_URI="amqp://alert_user:alertme@localhost:5672"
|
||||
export SYLVILAGUS_ROOT_ADMIN_AMQP_URI="amqp://admin:$(cat $SYLVILAGUS_ROOT/.admin-passwd)@localhost:5672"
|
5
oldstuff/sylvilagus/.gitignore
vendored
Normal file
5
oldstuff/sylvilagus/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
/.installation/log/
|
||||
/.installation/mnesia/
|
||||
/.env
|
||||
/.sylvilagus-passwd
|
||||
/.admin-passwd
|
4
oldstuff/sylvilagus/.installation/.example.env
Normal file
4
oldstuff/sylvilagus/.installation/.example.env
Normal file
@@ -0,0 +1,4 @@
|
||||
export SYLVILAGUS_ROOT=$HOME/src/box-o-sand/src/sylvilagus
|
||||
export RABBITMQ_MNESIA_BASE=$SYLVILAGUS_ROOT/.installation/mnesia
|
||||
export RABBITMQ_LOG_BASE=$SYLVILAGUS_ROOT/.installation/log
|
||||
export PATH="$SYLVILAGUS_ROOT/.installation/rabbitmq_server-2.7.0/sbin:$PATH"
|
3
oldstuff/sylvilagus/.installation/.gitignore
vendored
Normal file
3
oldstuff/sylvilagus/.installation/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
/rabbitmq-server-generic-unix-2.7.0.tar.gz
|
||||
/rabbitmq_server-2.7.0/
|
||||
/.env
|
8
oldstuff/sylvilagus/.installation/setup
Executable file
8
oldstuff/sylvilagus/.installation/setup
Executable file
@@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
|
||||
if [ ! -f rabbitmq-server-generic-unix-2.7.0.tar.gz ]
|
||||
then
|
||||
wget http://www.rabbitmq.com/releases/rabbitmq-server/v2.7.0/rabbitmq-server-generic-unix-2.7.0.tar.gz
|
||||
fi
|
||||
|
||||
tar -xzvf rabbitmq-server-generic-unix-2.7.0.tar.gz
|
6
oldstuff/sylvilagus/admin/ch04-alert-user-setup
Executable file
6
oldstuff/sylvilagus/admin/ch04-alert-user-setup
Executable file
@@ -0,0 +1,6 @@
|
||||
#!/bin/bash
|
||||
|
||||
if [ -z "$(rabbitmqctl -q list_users | grep ^alert_user)" ] ; then
|
||||
rabbitmqctl add_user alert_user alertme
|
||||
fi
|
||||
rabbitmqctl set_permissions -p '/' alert_user '.*' '.*' '.*'
|
2
oldstuff/sylvilagus/go/.gitignore
vendored
Normal file
2
oldstuff/sylvilagus/go/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/bin
|
||||
/.env
|
18
oldstuff/sylvilagus/go/Makefile
Normal file
18
oldstuff/sylvilagus/go/Makefile
Normal file
@@ -0,0 +1,18 @@
|
||||
REPO_BASE := github.com/meatballhat/box-o-sand/sylvilagus/go
|
||||
PACKAGES := \
|
||||
$(REPO_BASE)/sylvilagus-conntest \
|
||||
$(REPO_BASE)/sylvilagus-ch02-hello-world-consumer \
|
||||
$(REPO_BASE)/sylvilagus-ch02-hello-world-producer \
|
||||
$(REPO_BASE)/sylvilagus-ch04-rpc-server \
|
||||
$(REPO_BASE)/sylvilagus-ch04-rpc-client
|
||||
|
||||
test: build
|
||||
go test -x -v $(PACKAGES)
|
||||
|
||||
build: deps
|
||||
go install -x $(PACKAGES)
|
||||
|
||||
deps:
|
||||
go get -x $(PACKAGES)
|
||||
|
||||
.PHONY: test build deps
|
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/meatballhat/box-o-sand/sylvilagus/go/sylvilagus"
|
||||
"github.com/streadway/amqp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
connection, err := amqp.Dial(sylvilagus.AMQP_URI)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to connect!: ", err)
|
||||
}
|
||||
|
||||
defer connection.Close()
|
||||
|
||||
channel, err := sylvilagus.CreateHelloTopology(connection)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to build topology!: ", err)
|
||||
}
|
||||
|
||||
hellos, err := channel.Consume("hello-queue", "hello-consumer", false, false, false, false, nil)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to start consuming!:", err)
|
||||
}
|
||||
|
||||
quit := make(chan bool)
|
||||
|
||||
go func(quit chan bool) {
|
||||
log.Println("Consuming...")
|
||||
for hello := range hellos {
|
||||
log.Printf("hello -> %v\n", string(hello.Body))
|
||||
hello.Ack(false)
|
||||
if string(hello.Body) == "quit" {
|
||||
quit <- true
|
||||
return
|
||||
}
|
||||
}
|
||||
}(quit)
|
||||
|
||||
<-quit
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/meatballhat/box-o-sand/sylvilagus/go/sylvilagus"
|
||||
"github.com/streadway/amqp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
log.Fatal("You must provide a message as first arg!")
|
||||
}
|
||||
|
||||
msgBody := string(os.Args[1])
|
||||
|
||||
connection, err := amqp.Dial(sylvilagus.AMQP_URI)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to connect!: ", err)
|
||||
}
|
||||
|
||||
defer connection.Close()
|
||||
|
||||
channel, err := sylvilagus.CreateHelloTopology(connection)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to build topology!: ", err)
|
||||
}
|
||||
|
||||
msg := amqp.Publishing{
|
||||
DeliveryMode: amqp.Persistent,
|
||||
Timestamp: time.Now(),
|
||||
ContentType: "text/plain",
|
||||
Body: []byte(msgBody),
|
||||
}
|
||||
|
||||
err = channel.Publish("hello-exchange", "hola", false, false, msg)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to publish message!: ", err)
|
||||
} else {
|
||||
log.Printf("Published '%v'\n", msgBody)
|
||||
}
|
||||
}
|
63
oldstuff/sylvilagus/go/sylvilagus-ch04-rpc-client/main.go
Normal file
63
oldstuff/sylvilagus/go/sylvilagus-ch04-rpc-client/main.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/meatballhat/box-o-sand/sylvilagus/go/sylvilagus"
|
||||
"github.com/streadway/amqp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
connection, err := amqp.Dial(sylvilagus.AMQP_URI)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to connect!:", err)
|
||||
}
|
||||
|
||||
defer connection.Close()
|
||||
|
||||
channel, err := sylvilagus.CreateRPCTopology(connection)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to build topology!:", err)
|
||||
}
|
||||
|
||||
qResult, err := channel.QueueDeclare("", false, false, true, false, nil)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to build topology!:", err)
|
||||
}
|
||||
|
||||
msgBody := &sylvilagus.Ping{
|
||||
ClientName: "RPC Client 1.0",
|
||||
Time: time.Now(),
|
||||
}
|
||||
|
||||
msgBytes, err := json.Marshal(msgBody)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to json.Marshal the ping!:", err)
|
||||
}
|
||||
|
||||
msg := amqp.Publishing{
|
||||
ContentType: "application/json",
|
||||
DeliveryMode: amqp.Persistent,
|
||||
Timestamp: time.Now(),
|
||||
Body: msgBytes,
|
||||
ReplyTo: qResult.Name,
|
||||
}
|
||||
|
||||
channel.Publish("rpc", "ping", false, false, msg)
|
||||
log.Println("Sent 'ping' RPC call:", string(msg.Body))
|
||||
log.Println("Waiting for reply...")
|
||||
|
||||
pongs, err := channel.Consume(qResult.Name, qResult.Name, false, false, false, false, nil)
|
||||
for pong := range pongs {
|
||||
log.Println("RPC Reply ---", string(pong.Body))
|
||||
if err = channel.Close(); err != nil {
|
||||
log.Fatal("Failed to close channel!:", err)
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
63
oldstuff/sylvilagus/go/sylvilagus-ch04-rpc-server/main.go
Normal file
63
oldstuff/sylvilagus/go/sylvilagus-ch04-rpc-server/main.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/meatballhat/box-o-sand/sylvilagus/go/sylvilagus"
|
||||
"github.com/streadway/amqp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
connection, err := amqp.Dial(sylvilagus.AMQP_URI)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to connect!:", err)
|
||||
}
|
||||
|
||||
defer connection.Close()
|
||||
|
||||
channel, err := sylvilagus.CreateRPCTopology(connection)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to build topology!:", err)
|
||||
}
|
||||
|
||||
pings, err := channel.Consume("ping", "ping", false, false, false, false, nil)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to start consuming!:", err)
|
||||
}
|
||||
|
||||
quit := make(chan bool)
|
||||
|
||||
go func(quit chan bool) {
|
||||
log.Println("Waiting for RPC calls...")
|
||||
|
||||
for ping := range pings {
|
||||
if err = ping.Ack(false); err == nil {
|
||||
log.Println("Received API call... replying...")
|
||||
|
||||
pingInst := &sylvilagus.Ping{}
|
||||
err = json.Unmarshal(ping.Body, pingInst)
|
||||
if err == nil {
|
||||
msg := amqp.Publishing{
|
||||
DeliveryMode: amqp.Persistent,
|
||||
Timestamp: time.Now(),
|
||||
ContentType: "text/plain",
|
||||
Body: []byte(fmt.Sprintf("Pong! %s", pingInst.Time)),
|
||||
}
|
||||
|
||||
channel.Publish("", ping.ReplyTo, false, false, msg)
|
||||
} else {
|
||||
log.Println("Failed to json.Unmarshal the ping!:", err)
|
||||
}
|
||||
} else {
|
||||
log.Println("Failed to ACK the ping!:", err)
|
||||
}
|
||||
}
|
||||
}(quit)
|
||||
|
||||
<-quit
|
||||
}
|
41
oldstuff/sylvilagus/go/sylvilagus-conntest/main.go
Normal file
41
oldstuff/sylvilagus/go/sylvilagus-conntest/main.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/streadway/amqp"
|
||||
)
|
||||
|
||||
var (
|
||||
connUri = flag.String("connuri", "amqp://guest:guest@localhost:5672/sylvilagus", "Connection URI for AMQP thingy")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
fmt.Printf("Connecting to %v...\n", *connUri)
|
||||
conn, err := amqp.Dial(*connUri)
|
||||
defer conn.Close()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal("err:", err)
|
||||
}
|
||||
|
||||
if conn != nil {
|
||||
log.Println("conn:", conn)
|
||||
}
|
||||
|
||||
channel, err := conn.Channel()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal("err:", err)
|
||||
}
|
||||
|
||||
if channel != nil {
|
||||
log.Println("channel:", channel)
|
||||
}
|
||||
}
|
1
oldstuff/sylvilagus/go/sylvilagus.go
Normal file
1
oldstuff/sylvilagus/go/sylvilagus.go
Normal file
@@ -0,0 +1 @@
|
||||
package sylvilagus
|
37
oldstuff/sylvilagus/go/sylvilagus/hello.go
Normal file
37
oldstuff/sylvilagus/go/sylvilagus/hello.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package sylvilagus
|
||||
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/streadway/amqp"
|
||||
)
|
||||
|
||||
func CreateHelloTopology(connection *amqp.Connection) (*amqp.Channel, error) {
|
||||
channel, err := connection.Channel()
|
||||
if err != nil {
|
||||
log.Println("Failed to get channel!: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = channel.ExchangeDeclare("hello-exchange", "direct", true, false, false, false, nil)
|
||||
if err != nil {
|
||||
log.Println("Failed to declare exchange!: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = channel.QueueDeclare("hello-queue", false, false, false, false, nil)
|
||||
if err != nil {
|
||||
log.Println("Failed to declare queue!: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = channel.QueueBind("hello-queue", "hola", "hello-exchange", false, nil)
|
||||
if err != nil {
|
||||
log.Println("Failed to bind to queue!: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return channel, nil
|
||||
}
|
39
oldstuff/sylvilagus/go/sylvilagus/rpc.go
Normal file
39
oldstuff/sylvilagus/go/sylvilagus/rpc.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package sylvilagus
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/streadway/amqp"
|
||||
)
|
||||
|
||||
type Ping struct {
|
||||
ClientName string `json:"client_name"`
|
||||
Time time.Time `json:"time"`
|
||||
}
|
||||
|
||||
func CreateRPCTopology(connection *amqp.Connection) (channel *amqp.Channel, err error) {
|
||||
if channel, err = connection.Channel(); err != nil {
|
||||
log.Println("Failed to get channel!: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = channel.ExchangeDeclare("rpc", "direct", true, false, false, false, nil); err != nil {
|
||||
log.Println("Failed to declare exchange!: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err = channel.QueueDeclare("ping", false, false, false, false, nil); err != nil {
|
||||
log.Println("Failed to declare queue!: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = channel.QueueBind("ping", "ping", "rpc", false, nil); err != nil {
|
||||
log.Println("Failed to bind to queue!: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return channel, nil
|
||||
}
|
14
oldstuff/sylvilagus/go/sylvilagus/uri.go
Normal file
14
oldstuff/sylvilagus/go/sylvilagus/uri.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package sylvilagus
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var AMQP_URI = os.Getenv("SYLVILAGUS_AMQP_URI")
|
||||
|
||||
func init() {
|
||||
if len(AMQP_URI) < 1 {
|
||||
log.Fatal("SYLVILAGUS_AMQP_URI is not defined!")
|
||||
}
|
||||
}
|
2
oldstuff/sylvilagus/jruby/.gitignore
vendored
Normal file
2
oldstuff/sylvilagus/jruby/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/tmp/
|
||||
/lib/java/*.jar
|
1
oldstuff/sylvilagus/jruby/.rbenv-version
Normal file
1
oldstuff/sylvilagus/jruby/.rbenv-version
Normal file
@@ -0,0 +1 @@
|
||||
jruby-1.6.7.2
|
9
oldstuff/sylvilagus/jruby/Gemfile
Normal file
9
oldstuff/sylvilagus/jruby/Gemfile
Normal file
@@ -0,0 +1,9 @@
|
||||
source :rubygems
|
||||
|
||||
gem 'json_pure'
|
||||
gem 'jruby-openssl'
|
||||
|
||||
group :development, :test do
|
||||
gem 'awesome_print'
|
||||
gem 'rspec'
|
||||
end
|
26
oldstuff/sylvilagus/jruby/Gemfile.lock
Normal file
26
oldstuff/sylvilagus/jruby/Gemfile.lock
Normal file
@@ -0,0 +1,26 @@
|
||||
GEM
|
||||
remote: http://rubygems.org/
|
||||
specs:
|
||||
awesome_print (1.1.0)
|
||||
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)
|
||||
rspec-expectations (~> 2.12.0)
|
||||
rspec-mocks (~> 2.12.0)
|
||||
rspec-core (2.12.0)
|
||||
rspec-expectations (2.12.0)
|
||||
diff-lcs (~> 1.1.3)
|
||||
rspec-mocks (2.12.0)
|
||||
|
||||
PLATFORMS
|
||||
java
|
||||
|
||||
DEPENDENCIES
|
||||
awesome_print
|
||||
jruby-openssl
|
||||
json_pure
|
||||
rspec
|
43
oldstuff/sylvilagus/jruby/Rakefile
Normal file
43
oldstuff/sylvilagus/jruby/Rakefile
Normal file
@@ -0,0 +1,43 @@
|
||||
RMQ_URI_BASE = 'http://www.rabbitmq.com/releases/rabbitmq-java-client'
|
||||
RMQV = '2.8.7'
|
||||
GSONV = '2.2.2'
|
||||
|
||||
directory './tmp'
|
||||
directory './lib/java'
|
||||
|
||||
file "./tmp/google-gson-#{GSONV}-release.zip" => ['./tmp'] do |t|
|
||||
sh "curl -s -o #{t.name} " <<
|
||||
"http://google-gson.googlecode.com/files/google-gson-#{GSONV}-release.zip"
|
||||
end
|
||||
|
||||
file "./tmp/google-gson-#{GSONV}" => ["./tmp/google-gson-#{GSONV}-release.zip"] do
|
||||
Dir.chdir('./tmp') do
|
||||
sh "unzip -f -o google-gson-#{GSONV}-release.zip"
|
||||
end
|
||||
end
|
||||
|
||||
file "./lib/java/gson.jar" => ["./tmp/google-gson-#{GSONV}", './lib/java'] do |t|
|
||||
sh "cp ./tmp/google-gson-#{GSONV}/gson-#{GSONV}.jar #{t.name}"
|
||||
end
|
||||
|
||||
file "./tmp/rabbitmq-java-client-bin-#{RMQV}.tar.gz" => ['./tmp'] do |t|
|
||||
sh "curl -s -o #{t.name} " <<
|
||||
"#{RMQ_URI_BASE}/v#{RMQV}/rabbitmq-java-client-bin-#{RMQV}.tar.gz"
|
||||
end
|
||||
|
||||
file "./tmp/rabbitmq-java-client-bin-#{RMQV}/rabbitmq-client.jar" =>
|
||||
["./tmp/rabbitmq-java-client-bin-#{RMQV}.tar.gz"] do
|
||||
Dir.chdir('./tmp') do
|
||||
sh "tar xzf rabbitmq-java-client-bin-#{RMQV}.tar.gz"
|
||||
end
|
||||
end
|
||||
|
||||
file './lib/java/rabbitmq-client.jar' =>
|
||||
["./tmp/rabbitmq-java-client-bin-#{RMQV}/rabbitmq-client.jar", './lib/java'] do
|
||||
sh "cp ./tmp/rabbitmq-java-client-bin-#{RMQV}/rabbitmq-client.jar ./lib/java/"
|
||||
end
|
||||
|
||||
desc 'Do everything to set up the things'
|
||||
task :setup => ['./lib/java/rabbitmq-client.jar', './lib/java/gson.jar']
|
||||
|
||||
task :default => :setup
|
0
oldstuff/sylvilagus/jruby/lib/java/.gitkeep
Normal file
0
oldstuff/sylvilagus/jruby/lib/java/.gitkeep
Normal file
2
oldstuff/sylvilagus/jruby/lib/sylvilagus.rb
Normal file
2
oldstuff/sylvilagus/jruby/lib/sylvilagus.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module Sylvilagus
|
||||
end
|
4
oldstuff/sylvilagus/jruby/lib/sylvilagus/ch02.rb
Normal file
4
oldstuff/sylvilagus/jruby/lib/sylvilagus/ch02.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
module Sylvilagus
|
||||
module Ch02
|
||||
end
|
||||
end
|
23
oldstuff/sylvilagus/jruby/lib/sylvilagus/ch02/hello_world.rb
Normal file
23
oldstuff/sylvilagus/jruby/lib/sylvilagus/ch02/hello_world.rb
Normal 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
|
@@ -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
|
@@ -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
|
4
oldstuff/sylvilagus/jruby/lib/sylvilagus/ch03.rb
Normal file
4
oldstuff/sylvilagus/jruby/lib/sylvilagus/ch03.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
module Sylvilagus
|
||||
module Ch03
|
||||
end
|
||||
end
|
@@ -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
|
4
oldstuff/sylvilagus/jruby/lib/sylvilagus/ch04.rb
Normal file
4
oldstuff/sylvilagus/jruby/lib/sylvilagus/ch04.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
module Sylvilagus
|
||||
module Ch04
|
||||
end
|
||||
end
|
@@ -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
|
@@ -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
|
@@ -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
|
@@ -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
|
@@ -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
|
9
oldstuff/sylvilagus/jruby/lib/sylvilagus/init.rb
Normal file
9
oldstuff/sylvilagus/jruby/lib/sylvilagus/init.rb
Normal 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
|
1
oldstuff/sylvilagus/php/.gitignore
vendored
Normal file
1
oldstuff/sylvilagus/php/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/php-amqplib/
|
32
oldstuff/sylvilagus/php/rabbitmqctl-examples.php
Normal file
32
oldstuff/sylvilagus/php/rabbitmqctl-examples.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
define('AMQP_DEBUG', true);
|
||||
require_once(__DIR__ . '/php-amqplib/vendor/autoload.php');
|
||||
|
||||
use PhpAmqpLib\Connection\AMQPConnection;
|
||||
use PhpAmqpLib\Message\AMQPMessage;
|
||||
|
||||
$uri = parse_url(getenv('SYLVILAGUS_AMQP_URI'));
|
||||
|
||||
$conn = new AMQPConnection(
|
||||
$uri['host'],
|
||||
$uri['port'],
|
||||
$uri['user'],
|
||||
$uri['pass'],
|
||||
$uri['path']
|
||||
);
|
||||
$channel = $conn->channel();
|
||||
|
||||
$channel->exchange_declare('logs-exchange', 'topic', false, true, false);
|
||||
|
||||
$channel->queue_declare('msg-inbox-errors', false, true, false, false);
|
||||
|
||||
$channel->queue_declare('msg-inbox-logs', false, true, false, false);
|
||||
|
||||
$channel->queue_declare('all-logs', false, true, false, false);
|
||||
|
||||
$channel->queue_bind('msg-inbox-errors', 'logs-exchange', 'error.msg-inbox');
|
||||
|
||||
$channel->queue_bind('msg-inbox-logs', 'logs-exchange', '*.msg-inbox');
|
||||
|
||||
?>
|
19
oldstuff/sylvilagus/php/setup
Executable file
19
oldstuff/sylvilagus/php/setup
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -x
|
||||
|
||||
if [ ! -d php-amqplib/.git ] ; then
|
||||
git clone git://github.com/videlalvaro/php-amqplib.git
|
||||
else
|
||||
pushd php-amqplib
|
||||
git fetch
|
||||
get reset --hard origin/master
|
||||
popd
|
||||
fi
|
||||
|
||||
pushd php-amqplib
|
||||
if [ ! -f composer.phar ] ; then
|
||||
curl -s https://getcomposer.org/installer | php
|
||||
fi
|
||||
php composer.phar install
|
||||
popd
|
2
oldstuff/sylvilagus/python/.gitignore
vendored
Normal file
2
oldstuff/sylvilagus/python/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/.env
|
||||
/*.egg-info/
|
13
oldstuff/sylvilagus/python/setup.py
Normal file
13
oldstuff/sylvilagus/python/setup.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name='sylvilagus',
|
||||
version='0.1.0',
|
||||
author='Dan Buch',
|
||||
author_email='dan@meatballhat.com',
|
||||
description='crap accumulated while reading through RabbitMQ in Action',
|
||||
packages=['sylvilagus'],
|
||||
install_requires=[
|
||||
'pika == 0.9.6'
|
||||
]
|
||||
)
|
0
oldstuff/sylvilagus/python/sylvilagus/__init__.py
Normal file
0
oldstuff/sylvilagus/python/sylvilagus/__init__.py
Normal file
37
oldstuff/sylvilagus/python/sylvilagus/ch02/hello_world.py
Normal file
37
oldstuff/sylvilagus/python/sylvilagus/ch02/hello_world.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import os
|
||||
|
||||
import pika
|
||||
|
||||
|
||||
def get_conn_params():
|
||||
return pika.URLParameters(os.environ['SYLVILAGUS_AMQP_URI'])
|
||||
|
||||
|
||||
def get_nonblocking_channel(declare_exchange=True):
|
||||
channel = pika.SelectConnection(get_conn_params()).channel()
|
||||
|
||||
if declare_exchange:
|
||||
channel.exchange_declare(
|
||||
exchange='hello-exchange',
|
||||
exchange_type='direct',
|
||||
passive=False,
|
||||
durable=True,
|
||||
auto_delete=False
|
||||
)
|
||||
|
||||
return channel
|
||||
|
||||
|
||||
def get_channel(declare_exchange=True):
|
||||
channel = pika.BlockingConnection(get_conn_params()).channel()
|
||||
|
||||
if declare_exchange:
|
||||
channel.exchange_declare(
|
||||
exchange='hello-exchange',
|
||||
exchange_type='direct',
|
||||
passive=False,
|
||||
durable=True,
|
||||
auto_delete=False
|
||||
)
|
||||
|
||||
return channel
|
@@ -0,0 +1,39 @@
|
||||
from __future__ import print_function
|
||||
|
||||
import sys
|
||||
|
||||
import pika
|
||||
|
||||
from sylvilagus.ch02 import hello_world
|
||||
|
||||
|
||||
def msg_consumer(channel, method, header, body):
|
||||
channel.basic_ack(delivery_tag=method.delivery_tag)
|
||||
if body == 'quit':
|
||||
channel.basic_cancel(consumer_tag='hello-consumer')
|
||||
channel.stop_consuming()
|
||||
else:
|
||||
print(body)
|
||||
return
|
||||
|
||||
|
||||
def main():
|
||||
channel = hello_world.get_channel()
|
||||
|
||||
channel.queue_declare(queue='hello-queue')
|
||||
channel.queue_bind(queue='hello-queue',
|
||||
exchange='hello-exchange',
|
||||
routing_key='hola')
|
||||
|
||||
channel.basic_consume(msg_consumer,
|
||||
queue='hello-queue',
|
||||
consumer_tag='hello-consumer')
|
||||
|
||||
print('consuming...')
|
||||
channel.start_consuming()
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
@@ -0,0 +1,27 @@
|
||||
from __future__ import print_function
|
||||
|
||||
import sys
|
||||
|
||||
import pika
|
||||
|
||||
from sylvilagus.ch02 import hello_world
|
||||
|
||||
|
||||
def main(args=sys.argv[:]):
|
||||
channel = hello_world.get_channel()
|
||||
|
||||
msg = args[1]
|
||||
msg_props = pika.BasicProperties()
|
||||
msg_props.content_type = 'text/plain'
|
||||
|
||||
channel.basic_publish(body=msg,
|
||||
exchange='hello-exchange',
|
||||
properties=msg_props,
|
||||
routing_key='hola')
|
||||
print('published {!r}'.format(msg))
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
@@ -0,0 +1,37 @@
|
||||
from __future__ import print_function
|
||||
|
||||
import logging
|
||||
import sys
|
||||
|
||||
import pika
|
||||
|
||||
from sylvilagus.ch02 import hello_world
|
||||
|
||||
|
||||
def main(sysargs=sys.argv[:]):
|
||||
msg = sysargs[1]
|
||||
|
||||
logging.basicConfig()
|
||||
|
||||
channel = hello_world.get_channel()
|
||||
|
||||
if channel.basic_publish(
|
||||
body=msg,
|
||||
exchange='hello-exchange',
|
||||
properties=pika.BasicProperties(
|
||||
content_type='text/plain',
|
||||
delivery_mode=1
|
||||
),
|
||||
routing_key='hola',
|
||||
mandatory=True):
|
||||
print('Message delivered!')
|
||||
else:
|
||||
print('Message returned!')
|
||||
|
||||
channel.close()
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
100
oldstuff/sylvilagus/python/sylvilagus/ch04/alert_consumer.py
Normal file
100
oldstuff/sylvilagus/python/sylvilagus/ch04/alert_consumer.py
Normal file
@@ -0,0 +1,100 @@
|
||||
from __future__ import print_function
|
||||
|
||||
import json
|
||||
import smtplib
|
||||
import sys
|
||||
|
||||
import pika
|
||||
|
||||
|
||||
AMQP_SERVER = 'localhost'
|
||||
AMQP_USER = 'alert_user'
|
||||
AMQP_PASS = 'alertme'
|
||||
AMQP_VHOST = '/'
|
||||
AMQP_EXCHANGE = 'alerts'
|
||||
OPS_EMAILS = ['me@localhost']
|
||||
ADMIN_EMAILS = ['me@localhost']
|
||||
|
||||
|
||||
def main():
|
||||
creds_broker = pika.PlainCredentials(AMQP_USER, AMQP_PASS)
|
||||
conn_params = pika.ConnectionParameters(AMQP_SERVER,
|
||||
virtual_host=AMQP_VHOST,
|
||||
credentials=creds_broker)
|
||||
conn_broker = pika.BlockingConnection(conn_params)
|
||||
|
||||
channel = conn_broker.channel()
|
||||
|
||||
channel.exchange_declare(exchange=AMQP_EXCHANGE,
|
||||
exchange_type='topic',
|
||||
auto_delete=False)
|
||||
channel.queue_declare(queue='critical', auto_delete=False)
|
||||
channel.queue_bind(queue='critical',
|
||||
exchange='alerts',
|
||||
routing_key='critical.*')
|
||||
channel.queue_declare(queue='rate_limit', auto_delete=False)
|
||||
channel.queue_bind(queue='rate_limit',
|
||||
exchange='alerts',
|
||||
routing_key='*.rate_limit')
|
||||
|
||||
channel.basic_consume(critical_notify,
|
||||
queue='critical',
|
||||
no_ack=False,
|
||||
consumer_tag='critical')
|
||||
channel.basic_consume(rate_limit_notify,
|
||||
queue='rate_limit',
|
||||
no_ack=False,
|
||||
consumer_tag='rate_limit')
|
||||
|
||||
try:
|
||||
print('Ready for alerts!')
|
||||
channel.start_consuming()
|
||||
except KeyboardInterrupt:
|
||||
conn_broker.close()
|
||||
return 0
|
||||
|
||||
|
||||
def send_mail(recipients, subject, message):
|
||||
"""Email generator for received alerts."""
|
||||
headers = '\r\n'.join([
|
||||
'From: alerts@sylvilagus.local',
|
||||
'To: ',
|
||||
'Date: ',
|
||||
'Subject: {}'.format(subject)
|
||||
]) + '\r\n\r\n'
|
||||
smtp_server = smtplib.SMTP()
|
||||
smtp_server.connect('localhost', 25)
|
||||
smtp_server.sendmail('alerts@sylvilagus.local',
|
||||
recipients,
|
||||
headers + str(message))
|
||||
smtp_server.close()
|
||||
|
||||
|
||||
def critical_notify(channel, method, header, body):
|
||||
"""Sends CRITICAL alerts to administrators via email."""
|
||||
message = json.loads(body)
|
||||
|
||||
send_mail(OPS_EMAILS, 'CRITICAL ALERT', message)
|
||||
print('Sent alert via email! Alert Text: {} \nRecipients: {}'.format(
|
||||
message, OPS_EMAILS
|
||||
)
|
||||
)
|
||||
|
||||
channel.basic_ack(delivery_tag=method.delivery_tag)
|
||||
|
||||
|
||||
def rate_limit_notify(channel, method, header, body):
|
||||
"""Sends the message to the administrators via email."""
|
||||
message = json.loads(body)
|
||||
|
||||
send_mail(ADMIN_EMAILS, 'RATE LIMIT ALERT!', message)
|
||||
print('Sent alert via email! Alert Text: {} \nRecipients: {}'.format(
|
||||
message, ADMIN_EMAILS
|
||||
)
|
||||
)
|
||||
|
||||
channel.basic_ack(delivery_tag=method.delivery_tag)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
51
oldstuff/sylvilagus/python/sylvilagus/ch04/alert_producer.py
Normal file
51
oldstuff/sylvilagus/python/sylvilagus/ch04/alert_producer.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from __future__ import print_function
|
||||
|
||||
import json
|
||||
import sys
|
||||
|
||||
import pika
|
||||
|
||||
from argparse import ArgumentParser
|
||||
|
||||
|
||||
def main(sysargs=sys.argv[:]):
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument('-r',
|
||||
'--routing-key',
|
||||
help='Routing key for message (e.g. myalert.im)')
|
||||
parser.add_argument('-m',
|
||||
'--message',
|
||||
help='Message text for alert.')
|
||||
|
||||
args = parser.parse_args(sysargs[1:])
|
||||
|
||||
creds_broker = pika.PlainCredentials('alert_user', 'alertme')
|
||||
conn_params = pika.ConnectionParameters('localhost',
|
||||
virtual_host='/',
|
||||
credentials=creds_broker)
|
||||
conn_broker = pika.BlockingConnection(conn_params)
|
||||
|
||||
channel = conn_broker.channel()
|
||||
|
||||
msg = json.dumps({'message': args.message})
|
||||
msg_props = pika.BasicProperties()
|
||||
msg_props.content_type = 'application/json'
|
||||
msg_props.durable = False
|
||||
|
||||
channel.basic_publish(body=msg,
|
||||
exchange='alerts',
|
||||
properties=msg_props,
|
||||
routing_key=args.routing_key)
|
||||
|
||||
print(
|
||||
('Sent message {} tagged with routing key {!r} to ' +
|
||||
'exchange "alerts" on vhost "/".').format(msg, args.routing_key)
|
||||
)
|
||||
|
||||
conn_broker.close()
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
43
oldstuff/sylvilagus/scala/.gitignore
vendored
Normal file
43
oldstuff/sylvilagus/scala/.gitignore
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
# use glob syntax.
|
||||
syntax: glob
|
||||
*.ser
|
||||
*.class
|
||||
*~
|
||||
*.bak
|
||||
#*.off
|
||||
*.old
|
||||
|
||||
.env
|
||||
|
||||
# eclipse conf file
|
||||
.settings
|
||||
.classpath
|
||||
.project
|
||||
.manager
|
||||
.scala_dependencies
|
||||
|
||||
# idea
|
||||
.idea
|
||||
*.iml
|
||||
|
||||
# building
|
||||
target
|
||||
build
|
||||
null
|
||||
tmp*
|
||||
temp*
|
||||
dist
|
||||
test-output
|
||||
build.log
|
||||
|
||||
# other scm
|
||||
.svn
|
||||
.CVS
|
||||
.hg*
|
||||
|
||||
# switch to regexp syntax.
|
||||
# syntax: regexp
|
||||
# ^\.pc/
|
||||
|
||||
#SHITTY output not in target directory
|
||||
build.log
|
3
oldstuff/sylvilagus/scala/bin/sylvilagus-ch02-hello-world-producer
Executable file
3
oldstuff/sylvilagus/scala/bin/sylvilagus-ch02-hello-world-producer
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
cd $(dirname $(dirname $0))
|
||||
mvn scala:run -Dlauncher=ch02-hello-world-producer -DaddArgs="$1"
|
11
oldstuff/sylvilagus/scala/build.sbt
Normal file
11
oldstuff/sylvilagus/scala/build.sbt
Normal file
@@ -0,0 +1,11 @@
|
||||
import AssemblyKeys._
|
||||
|
||||
assemblySettings
|
||||
|
||||
name := "sylvilagus"
|
||||
|
||||
version := "0.1.0"
|
||||
|
||||
scalaVersion := "2.9.2"
|
||||
|
||||
libraryDependencies += "com.rabbitmq" % "amqp-client" % "2.8.7"
|
116
oldstuff/sylvilagus/scala/pom.xml
Normal file
116
oldstuff/sylvilagus/scala/pom.xml
Normal file
@@ -0,0 +1,116 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.meatballhat.sylvilagus</groupId>
|
||||
<artifactId>sylvilagus</artifactId>
|
||||
<version>0.1.0</version>
|
||||
<name>${project.artifactId}</name>
|
||||
<description>Scala stuff accumulated for RabbitMQ in Action</description>
|
||||
<inceptionYear>2012</inceptionYear>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.5</maven.compiler.source>
|
||||
<maven.compiler.target>1.5</maven.compiler.target>
|
||||
<encoding>UTF-8</encoding>
|
||||
<scala.version>2.9.2</scala.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>scala-tools.org</id>
|
||||
<name>Scala-Tools Maven2 Repository</name>
|
||||
<url>http://scala-tools.org/repo-releases</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>scala-tools.org</id>
|
||||
<name>Scala-Tools Maven2 Repository</name>
|
||||
<url>http://scala-tools.org/repo-releases</url>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
-->
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.scala-lang</groupId>
|
||||
<artifactId>scala-library</artifactId>
|
||||
<version>${scala.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.rabbitmq</groupId>
|
||||
<artifactId>amqp-client</artifactId>
|
||||
<version>2.8.7</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Test -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!--<dependency>-->
|
||||
<!--<groupId>org.scala-tools.testing</groupId>-->
|
||||
<!--<artifactId>specs_${scala.version}</artifactId>-->
|
||||
<!--<version>1.6.5</version>-->
|
||||
<!--<scope>test</scope>-->
|
||||
<!--</dependency>-->
|
||||
<dependency>
|
||||
<groupId>org.scalatest</groupId>
|
||||
<artifactId>scalatest</artifactId>
|
||||
<version>1.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src/main/scala</sourceDirectory>
|
||||
<testSourceDirectory>src/test/scala</testSourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.scala-tools</groupId>
|
||||
<artifactId>maven-scala-plugin</artifactId>
|
||||
<version>2.15.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
<goal>testCompile</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<args>
|
||||
<arg>-make:transitive</arg>
|
||||
<arg>-dependencyfile</arg>
|
||||
<arg>${project.build.directory}/.scala_dependencies</arg>
|
||||
</args>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<launchers>
|
||||
<launcher>
|
||||
<id>ch02-hello-world-producer</id>
|
||||
<mainClass>com.meatballhat.sylvilagus.ch02.HelloWorldProducer</mainClass>
|
||||
</launcher>
|
||||
</launchers>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
<configuration>
|
||||
<useFile>false</useFile>
|
||||
<disableXmlReport>true</disableXmlReport>
|
||||
<!-- If you have classpath issue like NoDefClassError,... -->
|
||||
<!-- useManifestOnlyJar>false</useManifestOnlyJar -->
|
||||
<includes>
|
||||
<include>**/*Test.*</include>
|
||||
<include>**/*Suite.*</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
3
oldstuff/sylvilagus/scala/project/plugins.sbt
Normal file
3
oldstuff/sylvilagus/scala/project/plugins.sbt
Normal file
@@ -0,0 +1,3 @@
|
||||
resolvers += Resolver.url("Artifactory", url("http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-releases"))(Resolver.ivyStylePatterns)
|
||||
|
||||
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.8.5")
|
@@ -0,0 +1,25 @@
|
||||
package com.meatballhat.sylvilagus.ch02
|
||||
|
||||
import com.rabbitmq.client._
|
||||
|
||||
object HelloWorldConsumer extends App {
|
||||
val factory = new ConnectionFactory()
|
||||
factory.setUri(sys.env("SYLVILAGUS_AMQP_URI"))
|
||||
val connection = factory.newConnection
|
||||
val channel = connection.createChannel
|
||||
|
||||
channel.exchangeDeclare("hello-exchange", "direct", true)
|
||||
channel.queueDeclare("hello-queue", false, false, false, null)
|
||||
|
||||
var consumer = new QueueingConsumer(channel)
|
||||
channel.basicConsume("hello-queue", true, consumer)
|
||||
|
||||
println("Waiting for messages...")
|
||||
|
||||
while (true) {
|
||||
new String(consumer.nextDelivery.getBody) match {
|
||||
case "quit" => println("Exiting..") ; connection.close ; exit
|
||||
case msg@_ => println("Received '%s'".format(msg))
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
package com.meatballhat.sylvilagus.ch02
|
||||
|
||||
import com.rabbitmq.client._
|
||||
|
||||
object HelloWorldProducer extends App {
|
||||
if (args.length < 1) {
|
||||
println("You must provide a message argument")
|
||||
exit
|
||||
}
|
||||
var messageBody = args(0)
|
||||
|
||||
var factory = new ConnectionFactory()
|
||||
factory.setUri(sys.env("SYLVILAGUS_AMQP_URI"))
|
||||
var connection = factory.newConnection()
|
||||
var channel = connection.createChannel()
|
||||
|
||||
channel.exchangeDeclare("hello-exchange", "direct", true)
|
||||
channel.queueDeclare("hello-queue", false, false, false, null)
|
||||
|
||||
printf("Publishing '%s'\n", messageBody)
|
||||
channel.basicPublish("hello-exchange", "hola", null, messageBody.getBytes())
|
||||
|
||||
connection.close()
|
||||
}
|
Reference in New Issue
Block a user