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'
21 include ApplicationHelper
24 return act_as_system_user
32 if Server::Application.config.crunch_job_wrapper.to_s.match /^slurm/
37 collect { |line| line.match /(\d+) +idle/ }.
39 @idle_slurm_nodes = re[1].to_i if re
49 if job.resource_limits['min_nodes']
50 min_nodes = begin job.resource_limits['min_nodes'].to_i rescue 1 end
53 next if @idle_slurm_nodes and @idle_slurm_nodes < min_nodes
55 next if @running[job.uuid]
59 case Server::Application.config.crunch_job_wrapper
67 "--job-name=#{job.uuid}",
68 "--nodes=#{min_nodes}"]
70 raise "Unknown crunch_job_wrapper: #{Server::Application.config.crunch_job_wrapper}"
73 job_auth = ApiClientAuthorization.
74 new(user: User.where('uuid=?', job.modified_by_user).first,
78 cmd_args << 'crunch-job'
79 cmd_args << '--job-api-token'
80 cmd_args << job_auth.api_token
84 commit = Commit.where(sha1: job.script_version).first
86 cmd_args << '--git-dir'
88 join(Rails.configuration.git_repositories_dir,
89 commit.repository_name,
93 $stderr.puts "dispatch: #{cmd_args.join ' '}"
96 i, o, e, t = Open3.popen3(*cmd_args)
98 $stderr.puts "dispatch: popen3: #{$!}"
103 $stderr.puts "dispatch: job #{job.uuid} start"
104 $stderr.puts "dispatch: child #{t.pid} start"
105 @running[job.uuid] = {
121 # no-op -- let crunch-job take care of locking.
126 # no-op -- let crunch-job take care of locking.
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").to_a
151 if j[:stderr_buf][-1] == "\n"
154 j[:stderr_buf] = lines.pop
157 $stderr.print "#{job_uuid} ! " unless line.index(job_uuid)
166 return if 0 == @running.size
172 pid_done = waitpid(-1, Process::WNOHANG | Process::WUNTRACED)
174 j_done = @running.values.
175 select { |j| j[:wait_thr].pid == pid_done }.
178 rescue SystemCallError
179 # I have @running processes but system reports I have no
180 # children. This is likely to happen repeatedly if it happens at
181 # all; I will log this no more than once per child process I
183 if 0 < @running.select { |uuid,j| j[:warned_waitpid_error].nil? }.size
184 children = @running.values.collect { |j| j[:wait_thr].pid }.join ' '
185 $stderr.puts "dispatch: IPC bug: waitpid() error (#{$!}), but I have children #{children}"
187 @running.each do |uuid,j| j[:warned_waitpid_error] = true end
190 @running.each do |uuid, j|
191 if j[:wait_thr].status == false
192 pid_done = j[:wait_thr].pid
200 job_done = j_done[:job]
201 $stderr.puts "dispatch: child #{pid_done} exit"
202 $stderr.puts "dispatch: job #{job_done.uuid} end"
204 # Ensure every last drop of stdout and stderr is consumed
206 if j_done[:stderr_buf] and j_done[:stderr_buf] != ''
207 $stderr.puts j_done[:stderr_buf] + "\n"
211 j_done[:wait_thr].value
213 # Invalidate the per-job auth token
214 j_done[:job_auth].update_attributes expires_at: Time.now
216 @running.delete job_done.uuid
222 $stderr.puts "dispatch: ready"
223 while !$signal[:term] or @running.size > 0
226 @running.each do |uuid, j|
227 if !j[:started] and j[:sent_int] < 2
229 Process.kill 'INT', j[:wait_thr].pid
231 # No such pid = race condition + desired result is
238 refresh_todo unless did_recently(:refresh_todo, 1.0)
239 start_jobs unless @todo.empty? or did_recently(:start_jobs, 1.0)
242 select(@running.values.collect { |j| [j[:stdout], j[:stderr]] }.flatten,
249 def did_recently(thing, min_interval)
251 if !@did_recently[thing] or @did_recently[thing] < Time.now - min_interval
252 @did_recently[thing] = Time.now