Class: Mule::Jobmaster

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

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from Log

#log

Constructor Details

#initialize(configurator, job) ⇒ Jobmaster

Returns a new instance of Jobmaster.



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

def initialize(configurator, job)
  @configurator = configurator
  @job          = job
end

Instance Method Details

#childrenObject



12
13
14
# File 'lib/mule/jobmaster.rb', line 12

def children
  @children ||= []
end

#cleanObject



76
77
78
79
# File 'lib/mule/jobmaster.rb', line 76

def clean
  children = []
  sig_queue = []
end

#exec_childrenObject



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

def exec_children
  @configurator.events[:after_fork].call
  @job.events[:before_fork].call
  job_content = File.read(@job.file)
  @job.workers.times do
    pid = fork do
      worker = Worker.new
      worker.run(job_content)
    end
    children << pid
  end
end

#reap_children(graceful = false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mule/jobmaster.rb', line 54

def reap_children(graceful=false)
  children.each do |pid|
    begin
      if graceful
        log "jobmaster gracefully killing job worker"
      else
        log "jobmaster brutally murdering job worker"
      end
      Process.kill((graceful)? :QUIT : :TERM , pid)
      sleep(0.1)
    rescue Errno::ESRCH, Errno::ENOENT => e
      # do nothing, we don't care if were missing a pid that we're
      # trying to murder already
      log "jobmaster error: #{e}"
    end
  end
  children = []
  # wait for all the children to die
  Process.waitall
  log "jobmaster killed job workers, retiring to the grave"
end

#sig_queueObject



16
17
18
# File 'lib/mule/jobmaster.rb', line 16

def sig_queue
  @sig_queue ||= []
end

#startObject



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

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

#wakeupObject



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

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