Module: Yahns::ServerMP

Defined in:
lib/yahns/server_mp.rb

Overview

Copyright © 2013, Eric Wong <[email protected]> and all contributors License: GPLv3 or later (www.gnu.org/licenses/gpl-3.0.txt)

Constant Summary collapse

EXIT_SIGS =

:nodoc:

[ :QUIT, :TERM, :INT ]

Instance Method Summary collapse

Instance Method Details

#__call_hooks(ary, worker_nr) ⇒ Object



55
56
57
# File 'lib/yahns/server_mp.rb', line 55

def __call_hooks(ary, worker_nr)
  ary.each { |x| x.call(worker_nr) } if ary
end

#fdmap_init_mpObject



135
136
137
138
139
140
# File 'lib/yahns/server_mp.rb', line 135

def fdmap_init_mp
  fdmap = fdmap_init # builds apps (if not preloading)
  [:USR1, *EXIT_SIGS].each { |sig| trap(sig) { sqwakeup(sig) } }
  @config.postfork_cleanup # reduce live objects
  fdmap
end

#joinObject

monitors children and receives signals forever (or until a termination signal is sent). This handles signals one-at-a-time time and we’ll happily drop signals in case somebody is signalling us too often.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/yahns/server_mp.rb', line 84

def join
  spawn_missing_workers
  state = :respawn # :QUIT, :WINCH
  proc_name 'master'
  @logger.info "master process ready"
  daemon_ready
  begin
    @sev.kgio_wait_readable
    @sev.yahns_step
    reap_all
    case @sig_queue.shift
    when *EXIT_SIGS # graceful shutdown (twice for non graceful)
      @listeners.each(&:close).clear
      kill_each_worker(:QUIT)
      state = :QUIT
    when :USR1 # rotate logs
      usr1_reopen("master ")
      kill_each_worker(:USR1)
    when :USR2 # exec binary, stay alive in case something went wrong
      reexec
    when :WINCH
      if @daemon_pipe
        state = :WINCH
        @logger.info "gracefully stopping all workers"
        kill_each_worker(:QUIT)
        @worker_processes = 0
      else
        @logger.info "SIGWINCH ignored because we're not daemonized"
      end
    when :TTIN
      state = :respawn unless state == :QUIT
      @worker_processes += 1
    when :TTOU
      @worker_processes -= 1 if @worker_processes > 0
    when :HUP
      state = :respawn unless state == :QUIT
      if @config.config_file
        load_config!
      else # exec binary and exit if there's no config file
        @logger.info "config_file not present, reexecuting binary"
        reexec
      end
    end while @sig_queue[0]
    maintain_worker_count if state == :respawn
  rescue => e
    Yahns::Log.exception(@logger, "master loop error", e)
  end while state != :QUIT || @workers.size > 0
  @logger.info "master complete"
  unlink_pid_safe(@pid) if @pid
end

#kill_each_worker(signal) ⇒ Object

delivers a signal to each worker



16
17
18
# File 'lib/yahns/server_mp.rb', line 16

def kill_each_worker(signal)
  @workers.each_key { |wpid| Process.kill(signal, wpid) }
end

#maintain_worker_countObject



7
8
9
10
11
12
13
# File 'lib/yahns/server_mp.rb', line 7

def maintain_worker_count
  (off = @workers.size - @worker_processes) == 0 and return
  off < 0 and return spawn_missing_workers
  @workers.each do |wpid, worker|
    worker.nr >= @worker_processes and Process.kill(:QUIT, wpid)
  end
end

#mp_sig_handle(watch, alive) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/yahns/server_mp.rb', line 156

def mp_sig_handle(watch, alive)
  # not performance critical
  watch.delete_if { |io| io.to_io.closed? }
  if r = IO.select(watch, nil, nil, alive ? nil : 0.01)
    r[0].each { |io| io.yahns_step }
  end
  case @sig_queue.shift
  when *EXIT_SIGS
    return quit_enter(alive)
  when :USR1
    usr1_reopen("worker ")
  end
  alive
end

#reap_allObject

reaps all unreaped workers/reexec processes



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/yahns/server_mp.rb', line 172

def reap_all
  begin
    wpid, status = Process.waitpid2(-1, Process::WNOHANG)
    wpid or return
    if @reexec_pid == wpid
      @logger.error "reaped #{status.inspect} exec()-ed"
      @reexec_pid = 0
      self.pid = @pid.chomp('.oldbin') if @pid
      proc_name('master')
    else
      worker = @workers.delete(wpid)
      desc = worker ? "worker=#{worker.nr}" : "(unknown)"
      m = "reaped #{status.inspect} #{desc}"
      status.success? ? @logger.info(m) : @logger.error(m)
    end
  rescue Errno::ECHILD
    return
  end while true
end

#run_mp_worker(worker) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/yahns/server_mp.rb', line 142

def run_mp_worker(worker)
  fdmap = fdmap_init_mp
  alive = true
  watch = [ worker, @sev ]
  begin
    alive = mp_sig_handle(watch, alive)
  rescue => e
    Yahns::Log.exception(@logger, "main worker loop", e)
  end while alive || dropping(fdmap)
  exit
ensure
  quit_finish
end

#spawn_missing_workersObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/yahns/server_mp.rb', line 59

def spawn_missing_workers
  worker_nr = -1
  until (worker_nr += 1) == @worker_processes
    @workers.value?(worker_nr) and next
    worker = Yahns::Worker.new(worker_nr)
    @logger.info("worker=#{worker_nr} spawning...")
    __call_hooks(@atfork_prepare, worker_nr)
    if pid = fork
      @workers[pid] = worker.atfork_parent
      # XXX is this useful?
      __call_hooks(@atfork_parent, worker_nr)
    else
      worker_atfork_internal(worker)
      run_mp_worker(worker)
    end
  end
rescue => e
  Yahns::Log.exception(@logger, "spawning worker", e)
  exit!
end

#worker_atfork_internal(worker) ⇒ Object

this is the first thing that runs after forking in a child gets rid of stuff the worker has no business keeping track of to free some resources and drops all sig handlers. traps for USR1, USR2, and HUP may be set in the after_fork Proc by the user.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/yahns/server_mp.rb', line 25

def worker_atfork_internal(worker)
  worker.atfork_child

  # daemon_pipe may be true for non-initial workers
  @daemon_pipe = @daemon_pipe.close if @daemon_pipe.respond_to?(:close)

  srand # in case this pops up again: https://bugs.ruby-lang.org/issues/4338

  # The OpenSSL PRNG is seeded with only the pid, and apps with frequently
  # dying workers can recycle pids
  OpenSSL::Random.seed(rand.to_s) if defined?(OpenSSL::Random)
  # we'll re-trap EXIT_SIGS later for graceful shutdown iff we accept clients
  EXIT_SIGS.each { |sig| trap(sig) { exit!(0) } }
  exit!(0) if (@sig_queue & EXIT_SIGS)[0] # did we inherit sigs from parent?
  @sig_queue = []

  # ignore WINCH, TTIN, TTOU, HUP in the workers
  (Yahns::Server::QUEUE_SIGS - EXIT_SIGS).each { |sig| trap(sig, nil) }
  trap(:CHLD, 'DEFAULT')
  @logger.info("worker=#{worker.nr} spawned pid=#$$")
  proc_name "worker[#{worker.nr}]"
  Yahns::START.clear
  @sev.close
  @sev = Yahns::Sigevent.new
  switch_user(*@user) if @user
  @user = @workers = nil
  __call_hooks(@atfork_child, worker.nr)
  @atfork_child = @atfork_parent = @atfork_prepare = nil
end