lib/tork/master.rb in tork-17.1.0 vs lib/tork/master.rb in tork-18.0.0

- old
+ new

@@ -1,14 +1,36 @@ require 'tork/server' require 'tork/config' module Tork -module Master +class Master < Server - extend Server - extend self + def initialize + super + @worker_number_pool = (0 ... Config.max_forked_workers).to_a + @command_by_worker_pid = {} + + # process exited child processes and report finished workers to client + trap :SIGCHLD do + begin + while wait2_array = Process.wait2(-1, Process::WNOHANG) + child_pid, child_status = wait2_array + if command = @command_by_worker_pid.delete(child_pid) + @worker_number_pool.push command.last + command[0] = if child_status.success? then :pass else :fail end + @client.send command.push(child_status.to_i, child_status.inspect) + else + warn "tork-master: unknown child exited: #{wait2_array.inspect}" + end + end + rescue SystemCallError + # raised by wait2() when there are currently no child processes + end + end + end + def load paths, files $LOAD_PATH.unshift(*paths) @overhead_files = files.each do |file| branch, leaf = File.split(file) @@ -66,35 +88,12 @@ Process.kill :SIGTERM, *@command_by_worker_pid.keys.map {|pid| -pid } rescue ArgumentError, SystemCallError # some workers might have already exited before we sent them the signal end - def loop - super + def quit stop - end - -private - - @worker_number_pool = (0 ... Config.max_forked_workers).to_a - @command_by_worker_pid = {} - - # process exited child processes and report finished workers to client - trap :SIGCHLD do - begin - while wait2_array = Process.wait2(-1, Process::WNOHANG) - child_pid, child_status = wait2_array - if command = @command_by_worker_pid.delete(child_pid) - @worker_number_pool.push command.last - command[0] = if child_status.success? then :pass else :fail end - @client.send command.push(child_status) - else - warn "tork-master: unknown child exited: #{wait2_array.inspect}" - end - end - rescue SystemCallError - # raised by wait2() when there are currently no child processes - end + super end end end