Class: Pazuzu::Utility::RunnablePool

Inherits:
Object
  • Object
show all
Includes:
Runnable
Defined in:
lib/pazuzu/utility/runnable_pool.rb

Overview

Runnable which manages a pool of child runnables that reflect its parent’s running state.

Instance Attribute Summary

Attributes included from Runnable

#started_at, #stopped_at

Instance Method Summary collapse

Methods included from Runnable

#run_state, #start!, #stop!, #wait_for_state_change!

Constructor Details

#initializeRunnablePool

Returns a new instance of RunnablePool.



10
11
12
13
14
# File 'lib/pazuzu/utility/runnable_pool.rb', line 10

def initialize
  super
  @children_mutex = Mutex.new
  @children = Set.new
end

Instance Method Details

#childrenObject



39
40
41
# File 'lib/pazuzu/utility/runnable_pool.rb', line 39

def children
  @children_mutex.synchronize { @children.to_a }
end

#lengthObject



16
17
18
# File 'lib/pazuzu/utility/runnable_pool.rb', line 16

def length
  children.length
end

#register(child) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
# File 'lib/pazuzu/utility/runnable_pool.rb', line 20

def register(child)
  raise ArgumentError, 'Child must be runnable' unless child.is_a?(Runnable)
  @children_mutex.synchronize do
    @children.add(child)
  end
  if [:running, :starting].include?(run_state)
    child.start!
  end
end

#unregister(child) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/pazuzu/utility/runnable_pool.rb', line 30

def unregister(child)
  if [:running, :starting].include?(child.run_state)
    child.stop!
  end
  @children_mutex.synchronize do
    @children.delete(child)
  end
end