Skip to content

Instantly share code, notes, and snippets.

@celldee
Created August 11, 2009 15:55
Show Gist options
  • Save celldee/165918 to your computer and use it in GitHub Desktop.
Save celldee/165918 to your computer and use it in GitHub Desktop.
Testing heartbeat monitoring with RabbitMQ and Bunny
require 'bunny' # Change this to point to the test version of bunny.rb otherwise you will probably be asking for the gem
require 'timeout'
b = Bunny.new(:logging => true, :heartbeat => 30) # Set heartbeat to 30 seconds
# Create a new heartbeat frame
hb = Qrack::Transport::Heartbeat.new()
b.start
# Change to channel 0
b.channel = b.channels[0]
# Start the subscribe sub-process
pid = fork do
exec("ruby test_sub.rb")
end
while true
# Get the next frame. Wrap in timeout to deal with overdue heartbeat.
begin
Timeout::timeout(45) do # timeout set to 45 seconds
@f = b.next_frame
end
rescue Timeout::Error
puts 'Killing sub-process'
Process.kill("KILL", pid)
puts 'Monitor exiting now'
exit
end
if @f.id == 8
b.send_frame(hb) # Send a heartbeat to server
else
puts 'Cardiac arrest'
exit
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment