Class: Supervisor
- Inherits:
-
Object
- Object
- Supervisor
- Defined in:
- lib/Olib/supervisor/supervisor.rb
Constant Summary collapse
- TICK =
the amount of time in seconds between each yield
0.1- TREE =
{}
Instance Attribute Summary collapse
-
#cleanup(&hook) ⇒ Object
Self.
-
#debug(msg) ⇒ Object
Returns the value of attribute debug.
-
#fibers ⇒ Object
Returns the value of attribute fibers.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
- .exists?(name) ⇒ Boolean
- .fetch(name) ⇒ Object
- .register(supervisor) ⇒ Object
- .unregister(supervisor) ⇒ Object
Instance Method Summary collapse
-
#add(name, &hook) ⇒ Object
Self.
-
#cleanup! ⇒ Object
Self.
-
#debug! ⇒ Object
Self.
-
#debug? ⇒ Boolean
Boolean.
-
#initialize(name) ⇒ Object
constructor
Self.
-
#join(*supervisors) ⇒ Object
Self.
-
#link! ⇒ Object
Nil.
-
#post_hook(name, &hook) ⇒ Object
Self.
-
#pre_hook(name, &hook) ⇒ Object
Self.
-
#run! ⇒ Object
Nil.
- #run_post_hooks! ⇒ Object
- #run_pre_hooks! ⇒ Object
-
#to_proc ⇒ Object
Proc.
Constructor Details
#initialize(name) ⇒ Object
Returns self.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# 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) { cleanup! } end |
Instance Attribute Details
#cleanup(&hook) ⇒ Object
Returns self.
133 134 135 |
# File 'lib/Olib/supervisor/supervisor.rb', line 133 def cleanup @cleanup end |
#debug(msg) ⇒ Object
Returns the value of attribute debug.
30 31 32 |
# File 'lib/Olib/supervisor/supervisor.rb', line 30 def debug @debug end |
#fibers ⇒ Object
Returns the value of attribute fibers.
30 31 32 |
# File 'lib/Olib/supervisor/supervisor.rb', line 30 def fibers @fibers end |
#name ⇒ Object
Returns the value of attribute name.
30 31 32 |
# File 'lib/Olib/supervisor/supervisor.rb', line 30 def name @name end |
Class Method Details
.exists?(name) ⇒ Boolean
26 27 28 |
# File 'lib/Olib/supervisor/supervisor.rb', line 26 def self.exists?(name) TREE.has_key?(name) end |
.fetch(name) ⇒ Object
18 19 20 |
# File 'lib/Olib/supervisor/supervisor.rb', line 18 def self.fetch(name) TREE.fetch(name) end |
.register(supervisor) ⇒ Object
14 15 16 |
# File 'lib/Olib/supervisor/supervisor.rb', line 14 def self.register(supervisor) TREE[supervisor.name] = supervisor end |
.unregister(supervisor) ⇒ Object
22 23 24 |
# File 'lib/Olib/supervisor/supervisor.rb', line 22 def self.unregister(supervisor) TREE.delete(supervisor.name) end |
Instance Method Details
#add(name, &hook) ⇒ Object
Returns self.
86 87 88 89 90 91 92 93 |
# 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! ⇒ Object
Returns self.
144 145 146 147 |
# File 'lib/Olib/supervisor/supervisor.rb', line 144 def cleanup! @cleanup.each(&:call) self end |
#debug! ⇒ Object
Returns self.
68 69 70 71 |
# File 'lib/Olib/supervisor/supervisor.rb', line 68 def debug! @debug = true self end |
#debug? ⇒ Boolean
Returns Boolean.
60 61 62 |
# File 'lib/Olib/supervisor/supervisor.rb', line 60 def debug? @debug end |
#join(*supervisors) ⇒ Object
Returns self.
199 200 201 202 203 204 |
# File 'lib/Olib/supervisor/supervisor.rb', line 199 def join(*supervisors) supervisors.each do |supervisor| add supervisor.name, &supervisor.to_proc end self end |
#link! ⇒ Object
Returns nil.
153 154 155 156 157 158 159 |
# File 'lib/Olib/supervisor/supervisor.rb', line 153 def link! loop do run! sleep TICK end nil end |
#post_hook(name, &hook) ⇒ Object
Returns self.
118 119 120 121 122 123 124 125 |
# 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) ⇒ Object
Returns self.
102 103 104 105 106 107 108 109 |
# 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! ⇒ Object
Returns nil.
165 166 167 168 169 170 171 |
# 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! ⇒ Object
172 173 174 |
# File 'lib/Olib/supervisor/supervisor.rb', line 172 def run_post_hooks! @post_hooks.map(&:last).map(&:resume) end |
#run_pre_hooks! ⇒ Object
176 177 178 |
# File 'lib/Olib/supervisor/supervisor.rb', line 176 def run_pre_hooks! @pre_hooks.map(&:last).map(&:resume) end |
#to_proc ⇒ Object
Returns Proc.
185 186 187 188 189 190 191 |
# 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 |