Class: Supervisor

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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)
  before_dying { cleanup! }
end

Instance Attribute Details

#cleanup(&hook) ⇒ Object

Returns self.

Parameters:

  • hook

    The hook

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

#fibersObject

Returns the value of attribute fibers.



30
31
32
# File 'lib/Olib/supervisor/supervisor.rb', line 30

def fibers
  @fibers
end

#nameObject

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

Returns:

  • (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.

Parameters:

  • name

    The name

  • hook

    The hook

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.

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.

Returns:

  • self



68
69
70
71
# File 'lib/Olib/supervisor/supervisor.rb', line 68

def debug!
  @debug = true
  self
end

#debug?Boolean

Returns Boolean.

Returns:

  • (Boolean)

    Boolean



60
61
62
# File 'lib/Olib/supervisor/supervisor.rb', line 60

def debug?
  @debug
end

#join(*supervisors) ⇒ Object

Returns self.

Parameters:

  • supervisor

    The supervisor

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.

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.

Parameters:

  • name

    The name

  • hook

    The hook

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.

Parameters:

  • name

    The name

  • hook

    The hook

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.

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_procObject

Returns Proc.

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