Skip to content

Instantly share code, notes, and snippets.

@kennethkalmer
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennethkalmer/8697614 to your computer and use it in GitHub Desktop.
Save kennethkalmer/8697614 to your computer and use it in GitHub Desktop.
# lib/api_server
class ApiServer
def call(env)
[200, {"Content-Type" => "text/html"}, "Hello World"]
end
end
# ... truncated
gem 'thin'
# libexec/rackmq-daemon.rb
# Generated amqp daemon
require 'api_server'
server = Thin::Server.new 4567 do
run ApiServer.new
end
# Do your post daemonization configuration here
# At minimum you need just the first line (without the block), or a lot
# of strange things might start happening...
DaemonKit::Application.running! do |config|
# Trap signals with blocks or procs
# config.trap( 'INT' ) do
# # do something clever
# end
# config.trap( 'TERM', Proc.new { puts 'Going down' } )
config.at_shutdown { server.stop! }
end
# IMPORTANT CONFIGURATION NOTE
#
# Please review and update 'config/amqp.yml' accordingly or this
# daemon won't work as advertised.
# Run an event-loop for processing
DaemonKit::AMQP.run do |connection|
# 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.
# Uncomment this for connection keep-alive
# connection.on_tcp_connection_loss do |client, settings|
# DaemonKit.logger.debug("AMQP connection status changed: #{status}")
# client.reconnect(false, 1)
# end
server.start
amq = AMQP::Channel.new
queue = amq.queue('test')
queue.subscribe do |msg|
DaemonKit.logger.debug "Received message: #{msg.inspect}"
end
EM.add_periodic_timer(5) {
queue.publish "TICK..."
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment