Skip to content

Instantly share code, notes, and snippets.

@danielsdeleo
Created July 18, 2009 18:06
Show Gist options
  • Save danielsdeleo/149636 to your computer and use it in GitHub Desktop.
Save danielsdeleo/149636 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
Kernel.system("rabbitmq-server -detached")
sleep 4
require "rubygems"
require "eventmachine"
require "mq"
class Borker
def die_gently
AMQP.stop { EM.stop }
Kernel.system("rabbitmqctl stop")
end
def kill_rabbit
EM.add_timer(5) do
Kernel.system("rabbitmqctl stop")
EM.add_timer(5) do
Kernel.system("rabbitmq-server -detached")
end
end
end
def run
EM.run do
kill_rabbit
pub_and_sub
end
end
def pub_and_sub
AMQP.start
index = 0
queue = MQ.new.queue("borken")
EM.add_periodic_timer(0.01) do
index += 1
queue.publish(index.to_s.ljust(2048))
end
queue.subscribe do |msg|
p msg.strip
end
end
end
b = Borker.new
Signal.trap("INT") { b.die_gently }
Signal.trap("TERM") { b.die_gently }
b.run
# RESULT:
# ...
# "52"
# "53"
# "54"
# "55"
# Stopping and halting node rabbit@dirac ...
# ...done.
# CONNECTION_FORCED - broker forced connection closure with reason 'shutdown' in inspect
# "58"
# "59"
# "60"
# "61"
# "62"
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment