Class: Daemonic::Pool

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/daemonic/pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(config) ⇒ Pool

Returns a new instance of Pool.



12
13
14
15
16
# File 'lib/daemonic/pool.rb', line 12

def initialize(config)
  @config = config
  @workers = []
  reload_desired_workers
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/daemonic/pool.rb', line 8

def config
  @config
end

#desired_workersObject (readonly)

Returns the value of attribute desired_workers.



10
11
12
# File 'lib/daemonic/pool.rb', line 10

def desired_workers
  @desired_workers
end

#workersObject (readonly)

Returns the value of attribute workers.



10
11
12
# File 'lib/daemonic/pool.rb', line 10

def workers
  @workers
end

Instance Method Details

#countObject



46
47
48
# File 'lib/daemonic/pool.rb', line 46

def count
  workers.count { |worker| worker.running? }
end

#decreaseObject



59
60
61
# File 'lib/daemonic/pool.rb', line 59

def decrease
  workers.pop.stop
end

#decrease!Object



63
64
65
66
# File 'lib/daemonic/pool.rb', line 63

def decrease!
  @desired_workers -= 1
  decrease
end

#hupObject



40
41
42
43
44
# File 'lib/daemonic/pool.rb', line 40

def hup
  reload_desired_workers
  workers.each(&:hup)
  start
end

#increaseObject



50
51
52
# File 'lib/daemonic/pool.rb', line 50

def increase
  workers << start_worker(workers.size)
end

#increase!Object



54
55
56
57
# File 'lib/daemonic/pool.rb', line 54

def increase!
  @desired_workers += 1
  increase
end

#monitorObject



68
69
70
# File 'lib/daemonic/pool.rb', line 68

def monitor
  workers.each(&:monitor)
end

#restartObject



26
27
28
29
30
31
# File 'lib/daemonic/pool.rb', line 26

def restart
  workers.each do |worker|
    worker.restart
    yield worker if block_given?
  end
end

#startObject



18
19
20
21
22
23
24
# File 'lib/daemonic/pool.rb', line 18

def start
  wait_for global_timeout do
    increase if count < desired_workers
    count == desired_workers
  end
  decrease while count > desired_workers
end

#stopObject



33
34
35
36
37
38
# File 'lib/daemonic/pool.rb', line 33

def stop
  workers.each do |worker|
    worker.stop
    yield worker if block_given?
  end
end