# We may be dealing with thousands of requests a second and while Rails can # scale to that, let's save the effort. # # Launch the handler by doing # ./script/smoked # from the applicaiton root. # # TODO: Find out which is preferrable: using active_support for Hash.to_xml or # using Hpricot to parse the XML. require 'active_support' require 'lib/smoke/signal_processor' require 'lib/smoke/server/signal_router' PROCESSOR_DIRECTORIES = [ File.dirname(__FILE__) + '/../../../etc/smoke.d/processors', '/etc/smoke.d/processors' ] PROCESSOR_DIRECTORIES.each_with_index do |directory, index| directory = File.expand_path(directory) if directory =~ /[^\/]$/ directory += '/' end PROCESSOR_DIRECTORIES[index] = directory $: << directory end module Smoke module Server class SignalHandler < Mongrel::HttpHandler def initialize(*args) super @signal_router = Smoke::Server::SignalRouter.new PROCESSOR_DIRECTORIES.each do |directory| Dir[directory + '**/*.rb'].collect do |processor_file| require processor_file processor = processor_file.sub(directory, '').sub('.rb', '').classify begin processor_class = processor.constantize if processor_class.ancestors.include? Smoke::SignalProcessor @signal_router.register(processor_class) else puts "Expected #{processor} (#{processor_file}) to extend Smoke::SignalProcessor." exit 2 end rescue NameError => e puts "Expected #{processor_file} to define #{processor}." puts e.message + ":\n\n" + e.backtrace.join("\n") exit 1 end end end end def process(request, response) signal = Hash.from_xml(request.body)["signal"] @signal_router.route(signal) response.start(201) do |header, body| header["Content-Type"] = "application/xml" end rescue => e response.start(500) do |header, body| STDERR.puts %Q(#{e.message}: #{e.backtrace}) end end end end end