lib/chook/event/handled_event.rb in chook-1.0.1.b2 vs lib/chook/event/handled_event.rb in chook-1.1.0

- old
+ new

@@ -23,11 +23,10 @@ ### ### require 'chook/event/handled_event/handlers' -# module Chook # Load sample JSON files, one per event type @sample_jsons = {} base_dir = Pathname.new(__FILE__) @@ -60,13 +59,10 @@ # class HandledEvent < Chook::Event #### Class Methods - # Given some raw_json from the jss, create and return the correct - # HandledEvent subclass - # For each event type in Chook::Event::EVENTS # generate a class for it, set its SUBJECT_CLASS constant # and add it to the HandledEvents module. # # @return [void] @@ -97,10 +93,11 @@ # @param [String] raw_event_json The JSON http POST content from the JSS # # @return [JSSWebHooks::Event subclass] the Event subclass matching the event # def self.parse_event(raw_event_json) + return nil if raw_event_json.to_s.empty? event_json = JSON.parse(raw_event_json, symbolize_names: true) event_name = event_json[:webhook][:webhookEvent] Chook::HandledEvents.const_get(event_name).new raw_event_json end @@ -120,34 +117,40 @@ def initialize(raw_event_json) super raw_json: raw_event_json end # init def handle - handlers = Handlers.handlers[self.class.const_get(Chook::Event::EVENT_NAME_CONST)] + handler_key = self.class.const_get(Chook::Event::EVENT_NAME_CONST) + handlers = Handlers.handlers[handler_key] + return 'No handlers loaded' unless handlers.is_a? Array + handlers.each do |handler| case handler when Pathname pipe_to_executable handler - when Proc + when Object handle_with_proc handler end # case end # @handlers.each do |handler| # the handle method should return a string, # which is the body of the HTTP result for # POSTing the event - "Processed by #{handlers.count} handlers\n" + "Processed by #{handlers.count} handlers" end # def handle - # TODO: Add something here that cleans up old threads and forks def pipe_to_executable(handler) - _thread = Thread.new do - IO.popen([handler.to_s], 'w') { |h| h.puts @raw_json } - end + logger.debug "Sending JSON to stdin of '#{handler}'" + IO.popen([handler.to_s], 'w') { |h| h.puts @raw_json } end def handle_with_proc(handler) - _thread = Thread.new { handler.call self } + logger.debug "Running Handler defined in #{handler.handler_file}" + handler.handle self + end + + def logger + @logger ||= Chook::HandledEventLogger.new self end end # class HandledEvent end # module