class Supervisor
@brief Class for Supervisor
.
Constants
- TICK
the amount of time in seconds between each yield
- TREE
Attributes
Public Class Methods
# File lib/Olib/supervisor/supervisor.rb, line 26 def self.exists?(name) TREE.has_key?(name) end
# File lib/Olib/supervisor/supervisor.rb, line 18 def self.fetch(name) TREE.fetch(name) end
@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
# File lib/Olib/supervisor/supervisor.rb, line 14 def self.register(supervisor) TREE[supervisor.name] = supervisor end
# File lib/Olib/supervisor/supervisor.rb, line 22 def self.unregister(supervisor) TREE.delete(supervisor.name) end
Public Instance Methods
@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
@brief cleanup a Supervisor
tree
@return self
# File lib/Olib/supervisor/supervisor.rb, line 144 def cleanup! @cleanup.each(&:call) self end
@brief turn on debug mode for a Supervisor
@return self
# File lib/Olib/supervisor/supervisor.rb, line 68 def debug! @debug = true self end
@brief returns if the Supervisor
is in debug mode
@return Boolean
# File lib/Olib/supervisor/supervisor.rb, line 60 def debug? @debug end
@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
@brief attach a supervisor tree to the current Script
@return nil
# File lib/Olib/supervisor/supervisor.rb, line 153 def link! loop do run! sleep TICK end nil end
@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
@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
@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
# File lib/Olib/supervisor/supervisor.rb, line 172 def run_post_hooks! @post_hooks.map(&:last).map(&:resume) end
# File lib/Olib/supervisor/supervisor.rb, line 176 def run_pre_hooks! @pre_hooks.map(&:last).map(&:resume) end
@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