Skip to content

Instantly share code, notes, and snippets.

@kennethkalmer
Created September 15, 2009 16:37
Show Gist options
  • Save kennethkalmer/187435 to your computer and use it in GitHub Desktop.
Save kennethkalmer/187435 to your computer and use it in GitHub Desktop.
# Run an event-loop for processing
DaemonKit::AMQP.run do
# Inside this block we're running inside the reactor setup by the
# amqp gem. Any code in the examples (from the gem) would work just
# fine here.
MQ.prefetch( 10 )
# Uncomment this for connection keep-alive
AMQP.conn.connection_status do |status|
DaemonKit.logger.debug("AMQP connection status changed: #{status}")
if status == :disconnected
AMQP.conn.reconnect(true)
end
end
amq = ::MQ.new
amq.queue('no-ack').subscribe do |msg|
EM.defer {
DaemonKit.logger.debug "[no-ack] Received message: #{msg.inspect}"
sleep rand(10)
DaemonKit.logger.debug "[no-ack] Slept over: #{msg.inspect}"
}
end
amq.queue('ack').subscribe(:ack => true) do |header, msg|
EM.defer {
DaemonKit.logger.debug "[ack] Received message: #{msg.inspect}"
sleep rand(10)
header.ack
DaemonKit.logger.debug "[ack] Acked: #{msg.inspect}"
}
end
end
require 'rubygems'
require 'mq'
Signal.trap('INT') { AMQP.stop { EM.stop } }
Signal.trap('TERM') { AMQP.stop { EM.stop } }
AMQP.start do
amq = MQ.new
c1 = 0
EM.add_periodic_timer(0.25) {
amq.queue('no-ack').publish( c1 )
c1 += 1
}
amq = MQ.new
c2 = 0
EM.add_periodic_timer(0.25) {
amq.queue('ack').publish( c2 )
c2 += 1
}
end
initializer.rb:170: DaemonKit (0.1.7.10) booting in development mode
initializer.rb:349: Setting up trap for USR1
initializer.rb:349: Setting up trap for USR2
initializer.rb:349: Setting up trap for HUP
initializer.rb:349: Setting up trap for INT
initializer.rb:349: Setting up trap for TERM
initializer.rb:113: DaemonKit (0.1.7.10) booted, now running flood
amqp.rb:34: AMQP.start({:vhost=>"/", :pass=>"guest", :host=>"localhost", :user=>"guest"})
flood-daemon.rb:38: [no-ack] Received message: "0"
flood-daemon.rb:38: [no-ack] Received message: "1"
flood-daemon.rb:38: [no-ack] Received message: "2"
flood-daemon.rb:46: [ack] Received message: "0"
flood-daemon.rb:49: [ack] Acked: "0"
flood-daemon.rb:38: [no-ack] Received message: "3"
flood-daemon.rb:38: [no-ack] Received message: "4"
flood-daemon.rb:38: [no-ack] Received message: "5"
flood-daemon.rb:46: [ack] Received message: "1"
flood-daemon.rb:38: [no-ack] Received message: "6"
flood-daemon.rb:38: [no-ack] Received message: "7"
flood-daemon.rb:38: [no-ack] Received message: "8"
flood-daemon.rb:46: [ack] Received message: "2"
flood-daemon.rb:38: [no-ack] Received message: "9"
flood-daemon.rb:38: [no-ack] Received message: "10"
flood-daemon.rb:38: [no-ack] Received message: "11"
flood-daemon.rb:46: [ack] Received message: "3"
flood-daemon.rb:40: [no-ack] Slept over: "0"
flood-daemon.rb:38: [no-ack] Received message: "12"
flood-daemon.rb:38: [no-ack] Received message: "13"
flood-daemon.rb:38: [no-ack] Received message: "14"
flood-daemon.rb:40: [no-ack] Slept over: "3"
flood-daemon.rb:46: [ack] Received message: "4"
flood-daemon.rb:38: [no-ack] Received message: "15"
flood-daemon.rb:38: [no-ack] Received message: "16"
flood-daemon.rb:49: [ack] Acked: "1"
flood-daemon.rb:38: [no-ack] Received message: "17"
flood-daemon.rb:46: [ack] Received message: "5"
^Cinitializer.rb:342: Running signal traps for INT
initializer.rb:71: Running shutdown hooks
initializer.rb:83: Shutting down flood
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment