class Plugg::Dispatcher

Attributes

registry[R]
timeout[RW]

Public Class Methods

new() click to toggle source

Initialize the dispatcher instance with a default timeout of 5s

@return void

# File lib/plugg.rb, line 173
def initialize
  @timeout = 5
end

Public Instance Methods

on(method, *args, &block) click to toggle source

Loop through all services and fire off the supported messages

@param string method @return void

# File lib/plugg.rb, line 182
  def on(method, *args, &block)

if [:initialize, :before, :setup, :after].include? method
  raise "#{method} should not be called directly"
end

buffer  = [] # Container for the response buffer
threads = [] # Container for the execution threads

          @registry.each do |s|
  if s.respond_to?(method.to_sym, false)
    threads << Thread.new do 
      responder = DispatchResponder.new(s)
    
      responder.trap(@timeout) do 
        if s.method(method.to_sym).arity == 0
          s.send(method, &block)
        else
          s.send(method, *args, &block)
        end
      end

      responder.finalize

      buffer << responder.to_h
    end
                  end
          end

threads.map(&:join)

buffer
  end
set_timeout(t) click to toggle source

Set the the thread execution timeout

@param integer t @return void

# File lib/plugg.rb, line 165
def set_timeout(t)
  @timeout = t
end
start(paths, params = {}) click to toggle source

Assign a path where plugins should be loaded from

@param mixed path @param hash params @return void

# File lib/plugg.rb, line 126
def start(paths, params = {})
  @registry = []

  paths.each do |path|
    if path[-1] == '/'
      path.chop!
    end

    Dir["#{path}/*.rb"].each do |f|

      require File.expand_path(f)

      begin
        instance = Object.const_get(File.basename(f, '.rb')).new

        # `before` event callback
        if instance.respond_to?(:before)
          instance.send(:before)
        end

        # `setup` method
        if instance.respond_to?(:setup)
          instance.send(:setup, params)
        end

        @registry.push(instance)
        
      rescue Exception => e
        puts "#{f} Plugg Initialization Exception: #{e}"
      end
            end
  end
end