Sha256: 6ff5d2b4924ca06f1854f6a0b18fcbcabff6af84421dda966068b2257907b18d

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

module God
  
  class Hub
    # directory to hold conditions and their corresponding metric
    #   key: condition
    #   val: metric
    @@directory = {}
    
    def self.attach(condition, metric)
      # add the condition to the directory
      @@directory[condition] = metric
      
      # schedule poll condition
      # register event condition
      if condition.kind_of?(PollCondition)
        Timer.get.schedule(condition, 0)
      else
        condition.register
      end
    end
    
    def self.detach(condition)
      # remove the condition from the directory
      @@directory.delete(condition)
      
      # unschedule any pending polls
      Timer.get.unschedule(condition)
      
      # deregister event condition
      if condition.kind_of?(EventCondition)
        condition.deregister
      end
    end
    
    def self.trigger(condition)
      if condition.kind_of?(PollCondition)
        self.handle_poll(condition)
      elsif condition.kind_of?(EventCondition)
        self.handle_event(condition)
      end
    end
    
    def self.handle_poll(condition)
      Thread.new do
        metric = @@directory[condition]
        watch = metric.watch
        
        watch.mutex.synchronize do
          result = condition.test
          
          puts watch.name + ' ' + condition.class.name + " [#{result}]"
          
          condition.after
          
          p metric.destination
          
          if dest = metric.destination[result]
            watch.move(dest)
          else
            # reschedule
            Timer.get.schedule(condition)
          end
        end
      end
    end
    
    def self.handle_event(condition)
      Thread.new do
        metric = @@directory[condition]
        watch = metric.watch
        
        watch.mutex.synchronize do
          puts watch.name + ' ' + condition.class.name + " [true]"
          
          p metric.destination
          
          dest = metric.destination[true]
          watch.move(dest)
        end
      end
    end
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
god-0.2.0 lib/god/hub.rb