6 %w{TERM INT}.each do |sig|
9 $stderr.puts "Received #{signame} signal"
14 if ENV["CRUNCH_DISPATCH_LOCKFILE"]
15 lockfilename = ENV.delete "CRUNCH_DISPATCH_LOCKFILE"
16 lockfile = File.open(lockfilename, File::RDWR|File::CREAT, 0644)
17 unless lockfile.flock File::LOCK_EX|File::LOCK_NB
18 abort "Lock unavailable on #{lockfilename} - exit"
22 ENV["RAILS_ENV"] = ARGV[0] || ENV["RAILS_ENV"] || "development"
24 require File.dirname(__FILE__) + '/../config/boot'
25 require File.dirname(__FILE__) + '/../config/environment'
31 include ApplicationHelper
34 return act_as_system_user
42 if Server::Application.config.crunch_job_wrapper.to_s.match /^slurm/
47 collect { |line| line.match /(\d+) +idle/ }.
49 @idle_slurm_nodes = re[1].to_i if re
59 if job.resource_limits['min_nodes']
60 min_nodes = begin job.resource_limits['min_nodes'].to_i rescue 1 end
63 next if @idle_slurm_nodes and @idle_slurm_nodes < min_nodes
65 next if @running[job.uuid]
69 case Server::Application.config.crunch_job_wrapper
78 "--job-name=#{job.uuid}",
79 "--nodes=#{min_nodes}"]
81 raise "Unknown crunch_job_wrapper: #{Server::Application.config.crunch_job_wrapper}"
84 if Server::Application.config.crunch_job_user
85 cmd_args.unshift("sudo", "-E", "-u",
86 Server::Application.config.crunch_job_user,
87 "PERLLIB=#{ENV['PERLLIB']}")
90 job_auth = ApiClientAuthorization.
91 new(user: User.where('uuid=?', job.modified_by_user_uuid).first,
95 cmd_args << (ENV['CRUNCH_JOB_BIN'] || `which crunch-job`.strip)
96 cmd_args << '--job-api-token'
97 cmd_args << job_auth.api_token
101 commit = Commit.where(sha1: job.script_version).first
103 cmd_args << '--git-dir'
104 if File.exists?(File.
105 join(Rails.configuration.git_repositories_dir,
106 commit.repository_name + '.git'))
108 join(Rails.configuration.git_repositories_dir,
109 commit.repository_name + '.git')
112 join(Rails.configuration.git_repositories_dir,
113 commit.repository_name, '.git')
117 $stderr.puts "dispatch: #{cmd_args.join ' '}"
120 i, o, e, t = Open3.popen3(*cmd_args)
122 $stderr.puts "dispatch: popen3: #{$!}"
127 $stderr.puts "dispatch: job #{job.uuid} start"
128 $stderr.puts "dispatch: child #{t.pid} start"
129 @running[job.uuid] = {
145 # no-op -- let crunch-job take care of locking.
150 # no-op -- let crunch-job take care of locking.
155 @running.each do |job_uuid, j|
158 # Throw away child stdout
160 j[:stdout].read_nonblock(2**20)
161 rescue Errno::EAGAIN, EOFError
164 # Read whatever is available from child stderr
167 stderr_buf = j[:stderr].read_nonblock(2**20)
168 rescue Errno::EAGAIN, EOFError
172 j[:stderr_buf] << stderr_buf
173 if j[:stderr_buf].index "\n"
174 lines = j[:stderr_buf].lines("\n").to_a
175 if j[:stderr_buf][-1] == "\n"
178 j[:stderr_buf] = lines.pop
181 $stderr.print "#{job_uuid} ! " unless line.index(job_uuid)
183 $redis.publish job_uuid, "#{Time.now.ctime.to_s} #{line.strip}"
191 return if 0 == @running.size
197 pid_done = waitpid(-1, Process::WNOHANG | Process::WUNTRACED)
199 j_done = @running.values.
200 select { |j| j[:wait_thr].pid == pid_done }.
203 rescue SystemCallError
204 # I have @running processes but system reports I have no
205 # children. This is likely to happen repeatedly if it happens at
206 # all; I will log this no more than once per child process I
208 if 0 < @running.select { |uuid,j| j[:warned_waitpid_error].nil? }.size
209 children = @running.values.collect { |j| j[:wait_thr].pid }.join ' '
210 $stderr.puts "dispatch: IPC bug: waitpid() error (#{$!}), but I have children #{children}"
212 @running.each do |uuid,j| j[:warned_waitpid_error] = true end
215 @running.each do |uuid, j|
216 if j[:wait_thr].status == false
217 pid_done = j[:wait_thr].pid
225 job_done = j_done[:job]
226 $stderr.puts "dispatch: child #{pid_done} exit"
227 $stderr.puts "dispatch: job #{job_done.uuid} end"
228 $redis.publish job_done.uuid, "end"
230 # Ensure every last drop of stdout and stderr is consumed
232 if j_done[:stderr_buf] and j_done[:stderr_buf] != ''
233 $stderr.puts j_done[:stderr_buf] + "\n"
237 j_done[:wait_thr].value
239 # Invalidate the per-job auth token
240 j_done[:job_auth].update_attributes expires_at: Time.now
242 @running.delete job_done.uuid
248 $stderr.puts "dispatch: ready"
249 while !$signal[:term] or @running.size > 0
252 @running.each do |uuid, j|
253 if !j[:started] and j[:sent_int] < 2
255 Process.kill 'INT', j[:wait_thr].pid
257 # No such pid = race condition + desired result is
264 refresh_todo unless did_recently(:refresh_todo, 1.0)
265 start_jobs unless @todo.empty? or did_recently(:start_jobs, 1.0)
268 select(@running.values.collect { |j| [j[:stdout], j[:stderr]] }.flatten,
275 def did_recently(thing, min_interval)
277 if !@did_recently[thing] or @did_recently[thing] < Time.now - min_interval
278 @did_recently[thing] = Time.now