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 ? and cancelled_at is ?',
53 order('priority desc, created_at')
57 if Server::Application.config.whjobmanager_wrapper.to_s.match /^slurm/
62 collect { |line| line.match /(\d+) +idle/ }.
64 @idle_slurm_nodes = re[1].to_i if re
74 if job.resource_limits['min_nodes']
75 min_nodes = begin job.resource_limits['min_nodes'].to_i rescue 1 end
78 next if @idle_slurm_nodes and @idle_slurm_nodes < min_nodes
80 next if @running[job.uuid]
84 case Server::Application.config.whjobmanager_wrapper
92 "--job-name=#{job.uuid}",
93 "--nodes=#{min_nodes}"]
95 raise "Unknown whjobmanager_wrapper: #{Server::Application.config.whjobmanager_wrapper}"
98 cmd_args << 'whjobmanager'
99 cmd_args << "id=#{job.uuid}"
100 cmd_args << "mrfunction=#{job.script}"
101 job.script_parameters.each do |k,v|
108 cmd_args << "#{k}=#{v}"
110 cmd_args << "revision=#{job.script_version}"
113 cmd_args << "stepspernode=#{job.resource_limits['max_tasks_per_node'].to_i}"
115 # OK if limit is not specified. OK to ignore if not integer.
118 $stderr.puts "dispatch: #{cmd_args.join ' '}"
121 i, o, e, t = Open3.popen3(*cmd_args)
123 $stderr.puts "dispatch: popen3: #{$!}"
128 $stderr.puts "dispatch: job #{job.uuid} start"
129 $stderr.puts "dispatch: child #{t.pid} start"
130 @running[job.uuid] = {
146 ActiveRecord::Base.transaction do
148 if job.is_locked_by.nil? and
149 job.update_attributes(is_locked_by: sysuser.uuid)
158 job.update_attributes is_locked_by: nil
162 @running.each do |job_uuid, j|
165 # Throw away child stdout
167 j[:stdout].read_nonblock(2**20)
168 rescue Errno::EAGAIN, EOFError
171 # Read whatever is available from child stderr
174 stderr_buf = j[:stderr].read_nonblock(2**20)
175 rescue Errno::EAGAIN, EOFError
179 j[:stderr_buf] << stderr_buf
180 if j[:stderr_buf].index "\n"
181 lines = j[:stderr_buf].lines "\n"
182 if j[:stderr_buf][-1] == "\n"
185 j[:stderr_buf] = lines.pop
188 $stderr.print "#{job_uuid} ! " unless line.index(job_uuid)
191 if (re = line.match(/#{job_uuid} (\d+) (\S*) (.*)/))
192 ignorethis, whjmpid, taskid, message = re.to_a
193 if taskid == '' and message == 'start'
194 $stderr.puts "dispatch: noticed #{job_uuid} started"
196 ActiveRecord::Base.transaction do
198 j[:job].update_attributes running: true, started_at: Time.now
200 elsif taskid == '' and (re = message.match /^revision (\S+)$/)
201 $stderr.puts "dispatch: noticed #{job_uuid} version #{re[1]}"
202 ActiveRecord::Base.transaction do
204 j[:job].script_version = re[1]
207 elsif taskid == '' and (re = message.match /^outputkey (\S+)$/)
208 $stderr.puts "dispatch: noticed #{job_uuid} output #{re[1]}"
210 elsif taskid == '' and (re = message.match /^meta key is (\S+)$/)
211 $stderr.puts "dispatch: noticed #{job_uuid} log #{re[1]}"
213 ActiveRecord::Base.transaction do
215 j[:job].update_attributes log: j[:log]
217 elsif taskid.match(/^\d+/) and (re = message.match /^failure /)
218 $stderr.puts "dispatch: noticed #{job_uuid} task fail"
219 ActiveRecord::Base.transaction do
221 j[:job].tasks_summary ||= {}
222 j[:job].tasks_summary[:failed] ||= 0
223 j[:job].tasks_summary[:failed] += 1
226 elsif (re = message.match(/^status: (\d+) done, (\d+) running, (\d+) todo/))
227 $stderr.puts "dispatch: noticed #{job_uuid} #{message}"
228 ActiveRecord::Base.transaction do
230 j[:job].tasks_summary ||= {}
231 j[:job].tasks_summary[:done] = re[1].to_i
232 j[:job].tasks_summary[:running] = re[2].to_i
233 j[:job].tasks_summary[:todo] = re[3].to_i
236 if re[2].to_i == 0 and re[3].to_i == 0
237 $stderr.puts "dispatch: noticed #{job_uuid} succeeded"
249 return if 0 == @running.size
255 pid_done = waitpid(-1, Process::WNOHANG | Process::WUNTRACED)
257 j_done = @running.values.
258 select { |j| j[:wait_thr].pid == pid_done }.
261 rescue SystemCallError
262 # I have @running processes but system reports I have no
263 # children. This is likely to happen repeatedly if it happens at
264 # all; I will log this no more than once per child process I
266 if 0 < @running.select { |uuid,j| j[:warned_waitpid_error].nil? }.size
267 children = @running.values.collect { |j| j[:wait_thr].pid }.join ' '
268 $stderr.puts "dispatch: IPC bug: waitpid() error (#{$!}), but I have children #{children}"
270 @running.each do |uuid,j| j[:warned_waitpid_error] = true end
273 @running.each do |uuid, j|
274 if j[:wait_thr].status == false
275 pid_done = j[:wait_thr].pid
283 job_done = j_done[:job]
284 $stderr.puts "dispatch: child #{pid_done} exit"
285 $stderr.puts "dispatch: job #{job_done.uuid} end"
287 # Ensure every last drop of stdout and stderr is consumed
289 if j_done[:stderr_buf] and j_done[:stderr_buf] != ''
290 $stderr.puts j_done[:stderr_buf] + "\n"
293 j_done[:wait_thr].value # wait the thread
296 # If the job never really started (due to a scheduling
297 # failure), just put it back in the queue
299 $stderr.puts "dispatch: job #{job_done.uuid} requeued"
301 # Otherwise, mark the job as finished
302 ActiveRecord::Base.transaction do
304 job_done.log = j_done[:log]
305 job_done.output = j_done[:output]
306 job_done.success = j_done[:success]
307 job_done.assert_finished
310 @running.delete job_done.uuid
316 $stderr.puts "dispatch: ready"
317 while !$signal[:term] or @running.size > 0
320 @running.each do |uuid, j|
321 if !j[:started] and j[:sent_int] < 2
323 Process.kill 'INT', j[:wait_thr].pid
325 # No such pid = race condition + desired result is
332 refresh_todo unless did_recently(:refresh_todo, 1.0)
333 start_jobs unless @todo.empty? or did_recently(:start_jobs, 1.0)
336 select(@running.values.collect { |j| [j[:stdout], j[:stderr]] }.flatten,
343 def did_recently(thing, min_interval)
345 if !@did_recently[thing] or @did_recently[thing] < Time.now - min_interval
346 @did_recently[thing] = Time.now