Class: Mule::Grandmaster

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/mule/grandmaster.rb

Constant Summary collapse

QUEUE_SIGS =
[:QUIT, :INT, :TERM, :USR2]

Instance Method Summary collapse

Methods included from Log

#log

Constructor Details

#initialize(options) ⇒ Grandmaster

Returns a new instance of Grandmaster.



7
8
9
# File 'lib/mule/grandmaster.rb', line 7

def initialize(options)
  @config_file = options[:config]
end

Instance Method Details

#childrenObject



11
12
13
# File 'lib/mule/grandmaster.rb', line 11

def children
  @children ||= []
end

#cleanObject



75
76
77
78
# File 'lib/mule/grandmaster.rb', line 75

def clean
  children = []
  sig_queue = []
end

#exec_childObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mule/grandmaster.rb', line 28

def exec_child
  # create configurator instance
  configurator = Configurator.new(@config_file).parse!
  
  # start master
  pid = fork do
    master = Master.new(configurator)
    master.clean
    master.start
  end
  children << pid
  pid
end

#reap_children(graceful = false, grant_amnesty = []) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mule/grandmaster.rb', line 58

def reap_children(graceful=false, grant_amnesty=[])
  children.each do |pid|
    begin
      unless grant_amnesty.include?(pid)
        Process.kill((graceful)? :QUIT : :TERM , pid)
        sleep(0.1)
        Process.detach(pid) if grant_amnesty.any?
      end
    rescue Errno::ESRCH, Errno::ENOENT
      # do nothing, we don't care if were missing a pid that we're
      # trying to murder already
    end
  end
  children = grant_amnesty
  Process.waitall unless grant_amnesty.any?
end

#sig_queueObject



15
16
17
# File 'lib/mule/grandmaster.rb', line 15

def sig_queue
  @sig_queue ||= []
end

#startObject



19
20
21
22
23
24
25
26
# File 'lib/mule/grandmaster.rb', line 19

def start
  log "grandmaster starting"
  exec_child
  QUEUE_SIGS.each do |sig|
    trap(sig) {sig_queue << sig; wakeup}
  end
  sleep
end

#wakeupObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mule/grandmaster.rb', line 42

def wakeup
  case sig_queue.shift
  when :INT, :TERM
    log "grandmaster received TERM signal"
    reap_children
  when :QUIT
    log "grandmaster received QUIT signal"
    reap_children(true)
  when :USR2
    log "grandmaster received USR2 signal"
    new_child = exec_child
    reap_children(true, [new_child])
    sleep
  end
end