Class: Daemonizer::ProcessManager

Inherits:
Object
  • Object
show all
Defined in:
lib/daemonizer/process_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ProcessManager

Returns a new instance of ProcessManager.



3
4
5
6
# File 'lib/daemonizer/process_manager.rb', line 3

def initialize(config)
  @shutdown = false
  @config = config
end

Instance Method Details

#monitor_workersObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/daemonizer/process_manager.rb', line 16

def monitor_workers
  setup_signals

  Daemonizer.logger.debug 'Starting workers monitoring code...'
  loop do
    Daemonizer.logger.debug "Checking workers' health..."
    break if shutdown?
    @pool.check_workers
    
    @config.on_poll.each do |on_poll|
      on_poll.call(@pool)
    end

    break if shutdown?
    Daemonizer.logger.debug "Sleeping for #{@config.poll_period} seconds..." 
    sleep(@config.poll_period)
  end
rescue Exception => e
  Daemonizer.logger.fatal "Monitor error: #{e.to_s}\n  #{e.backtrace.join("\n  ")}"
  raise
ensure
  Daemonizer.logger.debug "Workers monitoring loop is finished, starting shutdown..."
  # Send out stop signals
  stop_workers(false)

  # Wait for all the workers to die
  unless wait_for_workers(10)
    Daemonizer.logger.warn "Some workers are still alive after 10 seconds of waiting. Killing them..."
    stop_workers(true)
    wait_for_workers(5)
  end
end

#setup_signalsObject



49
50
51
52
53
# File 'lib/daemonizer/process_manager.rb', line 49

def setup_signals
  # Zombie reapers
  trap('CHLD') {}
  trap('EXIT') {}
end

#shutdown?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/daemonizer/process_manager.rb', line 80

def shutdown?
  @shutdown
end

#start_shutdown!Object



84
85
86
87
# File 'lib/daemonizer/process_manager.rb', line 84

def start_shutdown!
  Daemonizer.logger.debug "Starting shutdown (shutdown flag set)..."
  @shutdown = true
end

#start_workers(&blk) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
# File 'lib/daemonizer/process_manager.rb', line 9

def start_workers(&blk)
  raise ArgumentError, "Need a worker block!" unless block_given?
  Daemonizer.logger.info "starting all workers"
  @pool = WorkerPool.new(@config.pool, self, &blk)
  @pool.start_workers(@config.workers)
end

#stop_workers(force = false) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/daemonizer/process_manager.rb', line 72

def stop_workers(force = false)      
  # Set shutdown flag
  Daemonizer.logger.debug "Stopping workers#{force ? ' (forced)' : ''}..."

  # Termination loop
  @pool.stop_workers(force)
end

#wait_for_workers(seconds) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/daemonizer/process_manager.rb', line 55

def wait_for_workers(seconds)
  seconds.times do
    Daemonizer.logger.debug "Shutting down... waiting for workers to die (we have #{seconds} seconds)..."
    running_total = @pool.wait_workers

    if running_total.zero?
      Daemonizer.logger.debug "All workers are dead. Exiting..."
      return true
    end

    Daemonizer.logger.debug "#{running_total} workers are still running! Sleeping for a second..."
    sleep(1)
  end

  return false
end