Class: Spool::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/spool/pool.rb

Constant Summary collapse

SIGNALS =
{
  INT:  :stop!,
  TERM: :stop!,
  QUIT: :stop,
  HUP:  :reload,
  USR2: :restart,
  TTIN: :incr,
  TTOU: :decr
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = nil, &block) ⇒ Pool

Returns a new instance of Pool.



16
17
18
19
20
# File 'lib/spool/pool.rb', line 16

def initialize(configuration=nil, &block)
  @configuration = configuration || DSL.configure(&block)
  @processes = []
  @started = false
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



14
15
16
# File 'lib/spool/pool.rb', line 14

def configuration
  @configuration
end

#processesObject (readonly)

Returns the value of attribute processes.



14
15
16
# File 'lib/spool/pool.rb', line 14

def processes
  @processes
end

#runnerObject (readonly)

Returns the value of attribute runner.



14
15
16
# File 'lib/spool/pool.rb', line 14

def runner
  @runner
end

Instance Method Details

#decr(count = 1) ⇒ Object



66
67
68
# File 'lib/spool/pool.rb', line 66

def decr(count=1)
  configuration.processes -= count
end

#incr(count = 1) ⇒ Object



62
63
64
# File 'lib/spool/pool.rb', line 62

def incr(count=1)
  configuration.processes += count
end

#reloadObject



70
71
72
# File 'lib/spool/pool.rb', line 70

def reload
  @configuration = DSL.configure configuration.source_file if configuration.source_file
end

#restartObject



74
75
76
# File 'lib/spool/pool.rb', line 74

def restart
  processes.each(&:stop)
end

#startObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/spool/pool.rb', line 30

def start
  @started = true

  handle_signals

  File.write configuration.pidfile, Process.pid if configuration.pidfile

  configuration.processes.times.map do
    processes << Spawner.spawn(configuration)
  end

  while @started
    check_status
    sleep 0.05
  end
end

#started?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/spool/pool.rb', line 22

def started?
  @started
end

#stop(timeout = 0) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/spool/pool.rb', line 47

def stop(timeout=0)
  processes.each(&:stop)
  Timeout.timeout(timeout) { wait_for_stopped processes }
rescue Timeout::Error
ensure
  stop!
end

#stop!Object



55
56
57
58
59
60
# File 'lib/spool/pool.rb', line 55

def stop!
  processes.each(&:kill)
  processes.clear
  File.delete configuration.pidfile if File.exists? configuration.pidfile
  @started = false
end

#stopped?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/spool/pool.rb', line 26

def stopped?
  !started?
end