Class: Mule::Master

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

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from Log

#log

Constructor Details

#initialize(configurator) ⇒ Master

Returns a new instance of Master.



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

def initialize(configurator)
  @configurator = configurator
end

Instance Method Details

#childrenObject



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

def children
  @children ||= []
end

#cleanObject



68
69
70
71
# File 'lib/mule/master.rb', line 68

def clean
  children = []
  sig_queue = []
end

#exec_childrenObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mule/master.rb', line 40

def exec_children
  @configurator.events[:before_fork].call
  @configurator.jobs.each do |job|
    pid = fork do
      jobmaster = Jobmaster.new(@configurator, job)
      jobmaster.clean
      jobmaster.start
    end
    children << pid
  end
end

#reap_children(graceful = false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mule/master.rb', line 52

def reap_children(graceful=false)
  children.each do |pid|
    begin
      Process.kill((graceful)? :QUIT : :TERM , pid)
      sleep(0.1)
    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 = []
  # wait for all the children to die
  Process.waitall
  log "master killed jobmasters, retiring to the grave"
end

#sig_queueObject



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

def sig_queue
  @sig_queue ||= []
end

#startObject



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

def start
  log "master starting"
  exec_children
  # trap sigs
  QUEUE_SIGS.each do |sig|
    trap(sig) {sig_queue << sig; wakeup}
  end
  sleep
end

#wakeupObject



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

def wakeup
  case sig_queue.shift
  when :QUIT
    log "master received QUIT signal"
    reap_children(true)
  when :INT, :TERM
    log "master received TERM signal"
    reap_children
  end
end