Class: Daemonizer::ProcessManager
- Inherits:
-
Object
- Object
- Daemonizer::ProcessManager
- Defined in:
- lib/daemonizer/process_manager.rb
Instance Method Summary collapse
-
#initialize(config) ⇒ ProcessManager
constructor
A new instance of ProcessManager.
- #monitor_workers ⇒ Object
- #setup_signals ⇒ Object
- #shutdown? ⇒ Boolean
- #start_shutdown! ⇒ Object
- #start_workers(&blk) ⇒ Object
- #stop_workers(force = false) ⇒ Object
- #wait_for_workers(seconds) ⇒ Object
Constructor Details
#initialize(config) ⇒ ProcessManager
Returns a new instance of ProcessManager.
3 4 5 6 7 |
# File 'lib/daemonizer/process_manager.rb', line 3 def initialize(config) @worker_pools = {} @shutdown = false @config = config end |
Instance Method Details
#monitor_workers ⇒ Object
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 |
# File 'lib/daemonizer/process_manager.rb', line 17 def monitor_workers setup_signals Daemonizer.logger.debug 'Starting workers monitoring code...' loop do Daemonizer.logger.debug "Checking workers' health..." @worker_pools.each do |name, pool| break if shutdown? pool.check_workers end break if shutdown? Daemonizer.logger.debug "Sleeping for #{@config.poll_period} seconds..." sleep(@config.poll_period) end 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_signals ⇒ Object
45 46 47 48 49 |
# File 'lib/daemonizer/process_manager.rb', line 45 def setup_signals # Zombie reapers trap('CHLD') {} trap('EXIT') {} end |
#shutdown? ⇒ Boolean
82 83 84 |
# File 'lib/daemonizer/process_manager.rb', line 82 def shutdown? @shutdown end |
#start_shutdown! ⇒ Object
86 87 88 89 |
# File 'lib/daemonizer/process_manager.rb', line 86 def start_shutdown! Daemonizer.logger.debug "Starting shutdown (shutdown flag set)..." @shutdown = true end |
#start_workers(&blk) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/daemonizer/process_manager.rb', line 10 def start_workers(&blk) raise ArgumentError, "Need a worker block!" unless block_given? Daemonizer.logger.info "starting all workers" @worker_pools[@config.pool] = WorkerPool.new(@config.pool, self, &blk) @worker_pools[@config.pool].start_workers(@config.workers) end |
#stop_workers(force = false) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# 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 @worker_pools.each do |name, pool| pool.stop_workers(force) end end |
#wait_for_workers(seconds) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/daemonizer/process_manager.rb', line 51 def wait_for_workers(seconds) seconds.times do Daemonizer.logger.debug "Shutting down... waiting for workers to die (we have #{seconds} seconds)..." running_total = 0 @worker_pools.each do |name, pool| running_total += pool.wait_workers end 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 |