6 %w{TERM INT}.each do |sig|
9 $stderr.puts "Received #{signame} signal"
14 ENV["RAILS_ENV"] = ARGV[0] || ENV["RAILS_ENV"] || "development"
16 require File.dirname(__FILE__) + '/../config/boot'
17 require File.dirname(__FILE__) + '/../config/environment'
23 include ApplicationHelper
26 return act_as_system_user
34 if Server::Application.config.crunch_job_wrapper.to_s.match /^slurm/
39 collect { |line| line.match /(\d+) +idle/ }.
41 @idle_slurm_nodes = re[1].to_i if re
51 if job.resource_limits['min_nodes']
52 min_nodes = begin job.resource_limits['min_nodes'].to_i rescue 1 end
55 next if @idle_slurm_nodes and @idle_slurm_nodes < min_nodes
57 next if @running[job.uuid]
61 case Server::Application.config.crunch_job_wrapper
70 "--job-name=#{job.uuid}",
71 "--nodes=#{min_nodes}"]
73 raise "Unknown crunch_job_wrapper: #{Server::Application.config.crunch_job_wrapper}"
76 if Server::Application.config.crunch_job_user
77 cmd_args.unshift("sudo", "-E", "-u",
78 Server::Application.config.crunch_job_user,
79 "PERLLIB=#{ENV['PERLLIB']}")
82 job_auth = ApiClientAuthorization.
83 new(user: User.where('uuid=?', job.modified_by_user_uuid).first,
87 cmd_args << (ENV['CRUNCH_JOB_BIN'] || `which crunch-job`.strip)
88 cmd_args << '--job-api-token'
89 cmd_args << job_auth.api_token
93 commit = Commit.where(sha1: job.script_version).first
95 cmd_args << '--git-dir'
97 join(Rails.configuration.git_repositories_dir,
98 commit.repository_name + '.git'))
100 join(Rails.configuration.git_repositories_dir,
101 commit.repository_name + '.git')
104 join(Rails.configuration.git_repositories_dir,
105 commit.repository_name, '.git')
109 $stderr.puts "dispatch: #{cmd_args.join ' '}"
112 i, o, e, t = Open3.popen3(*cmd_args)
114 $stderr.puts "dispatch: popen3: #{$!}"
119 $stderr.puts "dispatch: job #{job.uuid} start"
120 $stderr.puts "dispatch: child #{t.pid} start"
121 @running[job.uuid] = {
137 # no-op -- let crunch-job take care of locking.
142 # no-op -- let crunch-job take care of locking.
147 @running.each do |job_uuid, j|
150 # Throw away child stdout
152 j[:stdout].read_nonblock(2**20)
153 rescue Errno::EAGAIN, EOFError
156 # Read whatever is available from child stderr
159 stderr_buf = j[:stderr].read_nonblock(2**20)
160 rescue Errno::EAGAIN, EOFError
164 j[:stderr_buf] << stderr_buf
165 if j[:stderr_buf].index "\n"
166 lines = j[:stderr_buf].lines("\n").to_a
167 if j[:stderr_buf][-1] == "\n"
170 j[:stderr_buf] = lines.pop
173 $stderr.print "#{job_uuid} ! " unless line.index(job_uuid)
175 $redis.publish job_uuid, "#{Time.now.ctime.to_s} #{line}"
183 return if 0 == @running.size
189 pid_done = waitpid(-1, Process::WNOHANG | Process::WUNTRACED)
191 j_done = @running.values.
192 select { |j| j[:wait_thr].pid == pid_done }.
195 rescue SystemCallError
196 # I have @running processes but system reports I have no
197 # children. This is likely to happen repeatedly if it happens at
198 # all; I will log this no more than once per child process I
200 if 0 < @running.select { |uuid,j| j[:warned_waitpid_error].nil? }.size
201 children = @running.values.collect { |j| j[:wait_thr].pid }.join ' '
202 $stderr.puts "dispatch: IPC bug: waitpid() error (#{$!}), but I have children #{children}"
204 @running.each do |uuid,j| j[:warned_waitpid_error] = true end
207 @running.each do |uuid, j|
208 if j[:wait_thr].status == false
209 pid_done = j[:wait_thr].pid
217 job_done = j_done[:job]
218 $stderr.puts "dispatch: child #{pid_done} exit"
219 $stderr.puts "dispatch: job #{job_done.uuid} end"
220 $redis.publish job_done.uuid, "end"
222 # Ensure every last drop of stdout and stderr is consumed
224 if j_done[:stderr_buf] and j_done[:stderr_buf] != ''
225 $stderr.puts j_done[:stderr_buf] + "\n"
229 j_done[:wait_thr].value
231 # Invalidate the per-job auth token
232 j_done[:job_auth].update_attributes expires_at: Time.now
234 @running.delete job_done.uuid
240 $stderr.puts "dispatch: ready"
241 while !$signal[:term] or @running.size > 0
244 @running.each do |uuid, j|
245 if !j[:started] and j[:sent_int] < 2
247 Process.kill 'INT', j[:wait_thr].pid
249 # No such pid = race condition + desired result is
256 refresh_todo unless did_recently(:refresh_todo, 1.0)
257 start_jobs unless @todo.empty? or did_recently(:start_jobs, 1.0)
260 select(@running.values.collect { |j| [j[:stdout], j[:stderr]] }.flatten,
267 def did_recently(thing, min_interval)
269 if !@did_recently[thing] or @did_recently[thing] < Time.now - min_interval
270 @did_recently[thing] = Time.now