Last active
August 29, 2015 13:55
-
-
Save kennethkalmer/8697614 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/api_server | |
class ApiServer | |
def call(env) | |
[200, {"Content-Type" => "text/html"}, "Hello World"] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ... truncated | |
gem 'thin' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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