class Supervisor

@brief Class for Supervisor.

Constants

TICK

the amount of time in seconds between each yield

TREE

Attributes

cleanup[RW]
debug[RW]
fibers[RW]
name[RW]

Public Class Methods

exists?(name) click to toggle source
# File lib/Olib/supervisor/supervisor.rb, line 26
def self.exists?(name)
  TREE.has_key?(name)
end
fetch(name) click to toggle source
# File lib/Olib/supervisor/supervisor.rb, line 18
def self.fetch(name)
  TREE.fetch(name)
end
new(name) click to toggle source

@brief creates a Supervisor instance

@return self

# File lib/Olib/supervisor/supervisor.rb, line 36
def initialize(name)
  supervisor = self

  if Supervisor.exists?(name)
    raise Exception.new "a Supervisor with the name #{name} already exists.  They are required to be unique"
  end

  @fibers     = []
  @pre_hooks  = []
  @post_hooks = []
  @name       = name
  @debug      = false
  @cleanup    = [Proc.new do
    Supervisor.unregister(supervisor) 
  end]
  
  Supervisor.register(supervisor)
  before_dying { cleanup! }
end
register(supervisor) click to toggle source
# File lib/Olib/supervisor/supervisor.rb, line 14
def self.register(supervisor)
  TREE[supervisor.name] = supervisor
end
unregister(supervisor) click to toggle source
# File lib/Olib/supervisor/supervisor.rb, line 22
def self.unregister(supervisor)
  TREE.delete(supervisor.name)
end

Public Instance Methods

add(name, &hook) click to toggle source

@brief add a hook to the Supervisor tree

@param name The name @param hook The hook

@return self

# File lib/Olib/supervisor/supervisor.rb, line 86
def add(name, &hook)
  @fibers << [name, Fiber.new do
    loop do
      Fiber.yield hook.call
    end        
  end]
  self
end
cleanup!() click to toggle source

@brief cleanup a Supervisor tree

@return self

# File lib/Olib/supervisor/supervisor.rb, line 144
def cleanup!
  @cleanup.each(&:call)
  self
end
debug!() click to toggle source

@brief turn on debug mode for a Supervisor

@return self

# File lib/Olib/supervisor/supervisor.rb, line 68
def debug!
  @debug = true
  self
end
debug?() click to toggle source

@brief returns if the Supervisor is in debug mode

@return Boolean

# File lib/Olib/supervisor/supervisor.rb, line 60
def debug?
  @debug
end
join(*supervisors) click to toggle source

@brief build a supervisor tree by joining two supervisors

@param supervisor The supervisor

@return self

# File lib/Olib/supervisor/supervisor.rb, line 199
def join(*supervisors)
  supervisors.each do |supervisor|
    add supervisor.name, &supervisor.to_proc
  end
  self
end
post_hook(name, &hook) click to toggle source

@brief hooks that run after every tick

@param name The name @param hook The hook

@return self

# File lib/Olib/supervisor/supervisor.rb, line 118
def post_hook(name, &hook)
  @post_hooks << [name, Fiber.new do
    loop do
      Fiber.yield hook.call
    end        
  end]
  self
end
pre_hook(name, &hook) click to toggle source

@brief hooks that run before every tick

@param name The name @param hook The hook

@return self

# File lib/Olib/supervisor/supervisor.rb, line 102
def pre_hook(name, &hook)
  @pre_hooks << [name, Fiber.new do
    loop do
      Fiber.yield hook.call
    end        
  end]
  self
end
run!() click to toggle source

@brief one event loop of the Supervisor

@return nil

# File lib/Olib/supervisor/supervisor.rb, line 165
def run!
  @fibers.map(&:last).each do |fiber|
    run_pre_hooks!
    fiber.resume
    run_post_hooks!
  end
end
run_post_hooks!() click to toggle source
# File lib/Olib/supervisor/supervisor.rb, line 172
def run_post_hooks!
  @post_hooks.map(&:last).map(&:resume)
end
run_pre_hooks!() click to toggle source
# File lib/Olib/supervisor/supervisor.rb, line 176
def run_pre_hooks!
  @pre_hooks.map(&:last).map(&:resume)
end
to_proc() click to toggle source

@brief converts a Supervisor to a Proc, useful for building

trees from Named Supervisors

@return Proc

# File lib/Olib/supervisor/supervisor.rb, line 185
def to_proc
  supervisor = self
  return Proc.new do
    debug "running child supervisor :#{supervisor.name}"
    supervisor.run!
  end
end