<commit_before>require 'rubygems'
require 'eventmachine'

module Llama
  def self.start(&block)
    EventMachine::run do
      puts "Starting Llama..."
      block.call
    end
  end 
end

require 'llama/component'
require 'llama/producer'
require 'llama/consumer'

module Llama
  module Message
    class Base
    end

    class JSONMessage < Base
    end

    class XmlMessage < Base
    end
  end

  class Filter
  end

  module Routing 
    class Route
      include EventMachine::Deferrable

      class ComponentNotFoundException < StandardError; end
      class ProducerComponentNotAllowed < StandardError; end

      def initialize(*args)
        @chain = []
      end

      def from(component)
        raise ComponentNotFoundException unless component.kind_of?(Llama::Component) 
        @chain.push(component)
        self
      end

      def to(component)
        raise ComponentNotFoundException unless component.kind_of?(Llama::Component) 
        raise ProducerComponentNotAllowed if component.kind_of?(Llama::Producer::Base)
        @chain.push(component)
        self 
      end

      def run
        set_deferred_status :succeeded
      end

      def to_s
        @chain.to_s
      end

      def inspect
        @chain.inspect
      end
    end

    class Router
      def initialize
        puts "Initializing Router..."
        @routes ||= []
      end

      def setup_routes(&block)
        raise "Subclass this"
      end

      def from(component)
        return Route.new.from(component) 
      end

      def add(built_route)
        @routes.push(built_route)
        puts "added #{built_route.inspect}"
      end

      def run
        @routes.each{|r| 
          EventMachine::spawn do
            r.callback{ puts "done with #{r.inspect}" } 
            r.run
          end.notify
        }
      end

      def self.start
        router = new
        router.setup_routes
        router.run
      end
    end
  end
end

if __FILE__ == $0
  Llama::start do
    class MyRouter < Llama::Routing::Router<commit_msg>first attempt at concurrency using EM::spawn - still executing one-by-one<commit_after>require 'rubygems'
require 'eventmachine'

module Llama
  def self.start(&block)
    EventMachine::run do
      puts "Starting Llama..."
      block.call
    end
  end 
end

require 'llama/component'
require 'llama/producer'
require 'llama/consumer'

module Llama
  module Message
    class Base
    end

    class JSONMessage < Base
    end

    class XmlMessage < Base
    end
  end

  class Filter
  end

  module Routing 
    class Route
      include EventMachine::Deferrable

      class ComponentNotFoundException < StandardError; end
      class ProducerComponentNotAllowed < StandardError; end

      def initialize(*args)
        @chain = []
      end

      def from(component)
        raise ComponentNotFoundException unless component.kind_of?(Llama::Component) 
        @chain.push(component)
        self
      end

      def to(component)
        raise ComponentNotFoundException unless component.kind_of?(Llama::Component) 
        raise ProducerComponentNotAllowed if component.kind_of?(Llama::Producer::Base)
        @chain.push(component)
        self 
      end

      def run
        s = rand(10)
        puts "sleeping for #{s} seconds"
        sleep(s)
        set_deferred_status :succeeded
      end

      def to_s
        @chain.to_s
      end

      def inspect
        @chain.inspect
      end
    end

    class Router
      def initialize
        puts "Initializing Router..."
        @routes ||= []
      end

      def setup_routes(&block)
        raise "Subclass this"
      end

      def from(component)
        return Route.new.from(component) 
      end

      def add(built_route)
        @routes.push(built_route)
        puts "added #{built_route.inspect}"
      end

      def run
        @routes.collect{|r| 
          EventMachine::spawn do
            r.callback{ puts "done with #{r.inspect}" } 
            r.run
          end
        }.each_with_index{|s,i| 
          puts "notifying #{i}"
          s.notify}
      end

      def self.start
        router = new
        router.setup_routes
        router.run
        EventMachine.stop
      end
    end
  end
end

if __FILE__ == $0
  Llama::start do
    class MyRouter < Llama::Routing::Router
{'fraction_non_alphanumeric': 0.0742368177613321, 'fraction_numerical': 0.0009250693802035153, 'mean_word_length': 1.9623287671232876, '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': '857092', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1240, 'n_words': 329}