Class: Angael::Manager

Inherits:
Object
  • Object
show all
Includes:
ProcessHelper
Defined in:
lib/angael/manager.rb

Overview

A Manager has a number of of worker objects. Starting the Manager simply calls #start! on each worker, then it goes into an infinite loop, waiting for SIGINT or SIGTERM. When either of those is received, the manager will call #stop_with_wait on each Worker.

Constant Summary collapse

LOOP_SLEEP_SECONDS =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ProcessHelper

#exit_status, #send_signal

Constructor Details

#initialize(worker_class, worker_count = 1, worker_args = [], opts = {}) ⇒ Manager

Creates a new manager.

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/angael/manager.rb', line 35

def initialize(worker_class, worker_count=1, worker_args=[], opts={})
  @workers = []
  worker_count.times { workers << worker_class.new(*worker_args) }
  @restart_after = opts[:restart_after]
  # TODO: Add a spec for this
  raise ArgumentError, ':restart_after must be either an Integer greater than zero or nil' if @restart_after && (@restart_after.to_i != @restart_after || @restart_after == 0)
  @logger = opts[:logger]
  if @logger
    @log_level = opts[:log_level] || begin
      require 'logger' # Only require it if it is absolutely neccessary.
      Logger::INFO
    end
  end
end

Instance Attribute Details

#workersObject (readonly)

Returns the value of attribute workers.



10
11
12
# File 'lib/angael/manager.rb', line 10

def workers
  @workers
end

Instance Method Details

#start!Object

Starts workers by calling Worker#start! Loops forever waiting for SIGINT or SIGTERM, at which time it calls Worker#stop_with_wait on each worker.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/angael/manager.rb', line 53

def start!
  workers.each { |w| w.start! }

  trap("CHLD") do
    log("SIGCHLD Received")
    @sigchld = true
  end
  trap("INT") do
    log("SIGINT Received")
    @interrupted = true
  end
  trap("TERM") do
    log("SIGTERM Received")
    @interrupted = true
  end

  if @restart_after
    loop do
      interrupted_handler
      sigchld_handler
      restart_worker_if_needed
      sleep LOOP_SLEEP_SECONDS
    end
  else
    loop do
      interrupted_handler
      sigchld_handler
      sleep LOOP_SLEEP_SECONDS
    end
  end
end