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
72 min_nodes = begin job.resource_limits['min_nodes'].to_i rescue 1 end
73 next if @idle_slurm_nodes and @idle_slurm_nodes < min_nodes
75 next if @running[job.uuid]
79 case Server::Application.config.whjobmanager_wrapper
87 "--job-name=#{job.uuid}",
90 raise "Unknown whjobmanager_wrapper: #{Server::Application.config.whjobmanager_wrapper}"
93 cmd_args << 'whjobmanager'
94 cmd_args << "id=#{job.uuid}"
95 cmd_args << "mrfunction=#{job.command}"
96 job.command_parameters.each do |k,v|
103 cmd_args << "#{k}=#{v}"
105 cmd_args << "revision=#{job.command_version}"
108 cmd_args << "stepspernode=#{job.resource_limits['max_tasks_per_node'].to_i}"
110 # OK if limit is not specified. OK to ignore if not integer.
113 $stderr.puts "dispatch: #{cmd_args.join ' '}"
116 i, o, e, t = Open3.popen3(*cmd_args)
118 $stderr.puts "dispatch: popen3: #{$!}"
123 $stderr.puts "dispatch: job #{job.uuid} start"
124 $stderr.puts "dispatch: child #{t.pid} start"
125 @running[job.uuid] = {
141 ActiveRecord::Base.transaction do
143 if job.is_locked_by.nil? and
144 job.update_attributes(is_locked_by: sysuser.uuid)
153 job.update_attributes is_locked_by: nil
157 @running.each do |job_uuid, j|
160 # Throw away child stdout
162 j[:stdout].read_nonblock(2**20)
163 rescue Errno::EAGAIN, EOFError
166 # Read whatever is available from child stderr
169 stderr_buf = j[:stderr].read_nonblock(2**20)
170 rescue Errno::EAGAIN, EOFError
174 j[:stderr_buf] << stderr_buf
175 if j[:stderr_buf].index "\n"
176 lines = j[:stderr_buf].lines "\n"
177 if j[:stderr_buf][-1] == "\n"
180 j[:stderr_buf] = lines.pop
183 $stderr.print "#{job_uuid} ! " unless line.index(job_uuid)
186 if (re = line.match(/#{job_uuid} (\d+) (\S*) (.*)/))
187 ignorethis, whjmpid, taskid, message = re.to_a
188 if taskid == '' and message == 'start'
189 $stderr.puts "dispatch: noticed #{job_uuid} started"
191 ActiveRecord::Base.transaction do
193 j[:job].update_attributes running: true
195 elsif taskid == '' and (re = message.match /^outputkey (\S+)$/)
196 $stderr.puts "dispatch: noticed #{job_uuid} output #{re[1]}"
198 elsif taskid == '' and (re = message.match /^meta key is (\S+)$/)
199 $stderr.puts "dispatch: noticed #{job_uuid} log #{re[1]}"
201 ActiveRecord::Base.transaction do
203 j[:job].update_attributes log: j[:log]
205 elsif taskid.match(/^\d+/) and (re = message.match /^failure /)
206 $stderr.puts "dispatch: noticed #{job_uuid} task fail"
207 ActiveRecord::Base.transaction do
209 j[:job].tasks_summary ||= {}
210 j[:job].tasks_summary[:failed] ||= 0
211 j[:job].tasks_summary[:failed] += 1
214 elsif (re = message.match(/^status: (\d+) done, (\d+) running, (\d+) todo/))
215 $stderr.puts "dispatch: noticed #{job_uuid} #{message}"
216 ActiveRecord::Base.transaction do
218 j[:job].tasks_summary ||= {}
219 j[:job].tasks_summary[:done] = re[1].to_i
220 j[:job].tasks_summary[:running] = re[2].to_i
221 j[:job].tasks_summary[:todo] = re[3].to_i
224 if re[2].to_i == 0 and re[3].to_i == 0
225 $stderr.puts "dispatch: noticed #{job_uuid} succeeded"
237 return if 0 == @running.size
243 pid_done = waitpid(-1, Process::WNOHANG | Process::WUNTRACED)
245 j_done = @running.values.
246 select { |j| j[:wait_thr].pid == pid_done }.
249 rescue SystemCallError
250 # I have @running processes but system reports I have no
251 # children. This is likely to happen repeatedly if it happens at
252 # all; I will log this no more than once per child process I
254 if 0 < @running.select { |uuid,j| j[:warned_waitpid_error].nil? }.size
255 children = @running.values.collect { |j| j[:wait_thr].pid }.join ' '
256 $stderr.puts "dispatch: IPC bug: waitpid() error (#{$!}), but I have children #{children}"
258 @running.each do |uuid,j| j[:warned_waitpid_error] = true end
261 @running.each do |uuid, j|
262 if j[:wait_thr].status == false
263 pid_done = j[:wait_thr].pid
271 job_done = j_done[:job]
272 $stderr.puts "dispatch: child #{pid_done} exit"
273 $stderr.puts "dispatch: job #{job_done.uuid} end"
275 # Ensure every last drop of stdout and stderr is consumed
277 if j_done[:stderr_buf] and j_done[:stderr_buf] != ''
278 $stderr.puts j_done[:stderr_buf] + "\n"
281 j_done[:wait_thr].value # wait the thread
284 # If the job never really started (due to a scheduling
285 # failure), just put it back in the queue
287 $stderr.puts "dispatch: job #{job_done.uuid} requeued"
289 # Otherwise, mark the job as finished
290 ActiveRecord::Base.transaction do
292 job_done.log = j_done[:log]
293 job_done.output = j_done[:output]
294 job_done.success = j_done[:success]
295 job_done.assert_finished
298 @running.delete job_done.uuid
304 $stderr.puts "dispatch: ready"
305 while !$signal[:term] or @running.size > 0
308 @running.each do |uuid, j|
309 if !j[:started] and j[:sent_int] < 2
311 Process.kill 'INT', j[:wait_thr].pid
313 # No such pid = race condition + desired result is
320 refresh_todo unless did_recently(:refresh_todo, 1.0)
321 start_jobs unless @todo.empty? or did_recently(:start_jobs, 1.0)
324 select(@running.values.collect { |j| [j[:stdout], j[:stderr]] }.flatten,
331 def did_recently(thing, min_interval)
333 if !@did_recently[thing] or @did_recently[thing] < Time.now - min_interval
334 @did_recently[thing] = Time.now