<commit_before>module Adhearsion
  class Dispatcher

    attr_accessor :event_queue

    def initialize(event_queue)
      @event_queue = event_queue
    end

    def start
      Thread.new do
        loop do
          catching_standard_errors { dispatch_event event_queue.pop }
        end
      end
    end

    def dispatch_event(event)
      if event.is_a?(Punchblock::Event::Offer)
        ahn_log.dispatcher.info "Offer received for call ID #{event.call_id}"
        IMPORTANT_THREADS << Thread.new { dispatch_offer event }
      else
        if event.respond_to?(:call_id) && event.call_id
          dispatch_call_event event
        else
          ahn_log.dispatcher.error "Unknown event: #{event.inspect}"
        end
      end
    end

    def dispatch_offer(offer)
      catching_standard_errors do
        DialPlan::Manager.handle Adhearsion.receive_call_from(offer)
      end
    end

    def dispatch_call_event(event)
      if call = Adhearsion.active_calls.find(event.call_id)
        ahn_log.dispatcher.notice "Event received for call #{call.id}: #{event.inspect}"
        call << event<commit_msg>Allow the dispatcher to be shut down<commit_after>module Adhearsion
  class Dispatcher

    attr_accessor :event_queue

    STOP_EVENT = :STOP

    def initialize(event_queue)
      @event_queue = event_queue
    end

    def start
      Events.register_callback(:shutdown) do
        ahn_log.info "Shutting down dispatcher"
        event_queue << STOP_EVENT
      end

      Thread.new do
        catch STOP_EVENT do
          loop do
            catching_standard_errors { dispatch_event event_queue.pop }
          end
        end
      end
    end

    def dispatch_event(event)
      if event == STOP_EVENT
        throw STOP_EVENT
      elsif event.is_a?(Punchblock::Event::Offer)
        ahn_log.dispatcher.info "Offer received for call ID #{event.call_id}"
        IMPORTANT_THREADS << Thread.new { dispatch_offer event }
      else
        if event.respond_to?(:call_id) && event.call_id
          dispatch_call_event event
        else
          ahn_log.dispatcher.error "Unknown event: #{event.inspect}"
        end
      end
    end

    def dispatch_offer(offer)
      catching_standard_errors do
        DialPlan::Manager.handle Adhearsion.receive_call_from(offer)
      end
    end

    def dispatch_call_event(event)
      if call = Adhearsion.active_calls.find(event.call_id)
        ahn_log.dispatcher.notice "Event received for call #{call.id}: #{event.inspect}"
        call << event
{'fraction_non_alphanumeric': 0.07108816521048451, 'fraction_numerical': 0.0, 'mean_word_length': 2.441256830601093, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '897787', 'n_tokens_mistral': 780, 'n_tokens_neox': 752, 'n_words': 173}