6 %w{TERM INT}.each do |sig|
9 $stderr.puts "Received #{signame} signal"
14 ENV["RAILS_ENV"] = ARGV[0] || "development"
16 require File.dirname(__FILE__) + '/../config/boot'
17 require File.dirname(__FILE__) + '/../config/environment'
23 return @sysuser if @sysuser
24 Thread.current[:user] = User.new(is_admin: true)
25 sysuser_id = [Server::Application.config.uuid_prefix,
27 '000000000000000'].join('-')
28 @sysuser = User.where('uuid=?', sysuser_id).first
30 @sysuser = User.new(uuid: sysuser_id,
38 Thread.current[:user] = @sysuser
40 auth = ApiClientAuthorization.new(api_client_id: 0,
43 auth_token = auth.api_token
44 $stderr.puts "dispatch: sysuser.uuid = #{@sysuser.uuid}"
45 $stderr.puts "dispatch: api_client_authorization.api_token = #{auth_token}"
51 where('started_at is ? and is_locked_by is ?', nil, nil).
52 order('priority desc, created_at')
58 next if @running[job.uuid]
62 case Server::Application.config.whjobmanager_wrapper
70 "--job-name=#{job.uuid}",
73 raise "Unknown whjobmanager_wrapper: #{Server::Application.config.whjobmanager_wrapper}"
76 cmd_args << 'whjobmanager'
77 cmd_args << "id=#{job.uuid}"
78 cmd_args << "mrfunction=#{job.command}"
79 job.command_parameters.each do |k,v|
86 cmd_args << "#{k}=#{v}"
88 cmd_args << "revision=#{job.command_version}"
90 i, o, e, t = Open3.popen3(*cmd_args)
92 $stderr.puts "dispatch: popen3: #{$!}"
97 $stderr.puts "dispatch: job #{job.uuid} start"
98 $stderr.puts "dispatch: child #{t.pid} start"
99 @running[job.uuid] = {
115 ActiveRecord::Base.transaction do
117 if job.is_locked_by.nil? and
118 job.update_attributes(is_locked_by: sysuser.uuid)
127 job.update_attributes is_locked_by: nil
131 @running.each do |job_uuid, j|
134 # Throw away child stdout
136 j[:stdout].read_nonblock(2**20)
137 rescue Errno::EAGAIN, EOFError
140 # Read whatever is available from child stderr
143 stderr_buf = j[:stderr].read_nonblock(2**20)
144 rescue Errno::EAGAIN, EOFError
148 j[:stderr_buf] << stderr_buf
149 if j[:stderr_buf].index "\n"
150 lines = j[:stderr_buf].lines "\n"
151 if j[:stderr_buf][-1] == "\n"
154 j[:stderr_buf] = lines.pop
157 $stderr.print "#{job_uuid} ! " unless line.index(job_uuid)
160 if (re = line.match(/#{job_uuid} (\d+) (\S*) (.*)/))
161 ignorethis, whjmpid, taskid, message = re.to_a
162 if taskid == '' and message == 'start'
163 $stderr.puts "dispatch: noticed #{job_uuid} started"
165 ActiveRecord::Base.transaction do
167 j[:job].update_attributes running: true
169 elsif taskid == '' and message.match /^status: .* 0 todo/
170 $stderr.puts "dispatch: noticed #{job_uuid} succeeded"
172 elsif taskid == '' and (re = message.match /^outputkey (\S+)$/)
173 $stderr.puts "dispatch: noticed #{job_uuid} output #{re[1]}"
175 elsif taskid == '' and (re = message.match /^meta key is (\S+)$/)
176 $stderr.puts "dispatch: noticed #{job_uuid} log #{re[1]}"
178 ActiveRecord::Base.transaction do
180 j[:job].update_attributes log: j[:log]
182 elsif taskid.match(/^\d+/) and (re = message.match /^failure /)
183 $stderr.puts "dispatch: noticed #{job_uuid} task fail"
184 ActiveRecord::Base.transaction do
186 j[:job].tasks_summary ||= {}
187 j[:job].tasks_summary[:failed] ||= 0
188 j[:job].tasks_summary[:failed] += 1
191 elsif (re = message.match(/^status: (\d+) done, (\d+) running, (\d+) todo/))
192 $stderr.puts "dispatch: noticed #{job_uuid} #{message}"
193 ActiveRecord::Base.transaction do
195 j[:job].tasks_summary ||= {}
196 j[:job].tasks_summary[:done] = re[1].to_i
197 j[:job].tasks_summary[:running] = re[2].to_i
198 j[:job].tasks_summary[:todo] = re[3].to_i
210 return if 0 == @running.size
216 pid_done = waitpid(-1, Process::WNOHANG | Process::WUNTRACED)
218 j_done = @running.values.
219 select { |j| j[:wait_thr].pid == pid_done }.
222 rescue SystemCallError
223 # I have @running processes but system reports I have no
224 # children. This is likely to happen repeatedly if it happens at
225 # all; I will log this no more than once per child process I
227 if 0 < @running.select { |uuid,j| j[:warned_waitpid_error].nil? }.size
228 children = @running.values.collect { |j| j[:wait_thr].pid }.join ' '
229 $stderr.puts "dispatch: IPC bug: waitpid() error (#{$!}), but I have children #{children}"
231 @running.each do |uuid,j| j[:warned_waitpid_error] = true end
234 @running.each do |uuid, j|
235 if j[:wait_thr].status == false
236 pid_done = j[:wait_thr].pid
244 job_done = j_done[:job]
245 $stderr.puts "dispatch: child #{pid_done} exit"
246 $stderr.puts "dispatch: job #{job_done.uuid} end"
248 # Ensure every last drop of stdout and stderr is consumed
250 if j_done[:stderr_buf] and j_done[:stderr_buf] != ''
251 $stderr.puts j_done[:stderr_buf] + "\n"
254 j_done[:wait_thr].value # wait the thread
257 # If the job never really started (due to a scheduling
258 # failure), just put it back in the queue
260 $stderr.puts "dispatch: job #{job_done.uuid} requeued"
262 # Otherwise, mark the job as finished
263 ActiveRecord::Base.transaction do
265 job_done.log = j_done[:log]
266 job_done.output = j_done[:output]
267 job_done.success = j_done[:success]
268 job_done.assert_finished
271 @running.delete job_done.uuid
277 $stderr.puts "dispatch: ready"
278 while !$signal[:term] or @running.size > 0
281 @running.each do |uuid, j|
282 if !j[:started] and j[:sent_int] < 2
284 Process.kill 'INT', j[:wait_thr].pid
286 # No such pid = race condition + desired result is
297 select(@running.values.collect { |j| [j[:stdout], j[:stderr]] }.flatten,