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
41 def update_node_status
42 if Server::Application.config.crunch_job_wrapper.to_s.match /^slurm/
43 @nodes_in_state = {idle: 0, alloc: 0, down: 0}
47 `sinfo --noheader -o '%n:%t'`.
50 re = line.match /(\S+?):+(idle|alloc|down)/
53 # sinfo tells us about a node N times if it is shared by N partitions
54 next if node_seen[re[1]]
55 node_seen[re[1]] = true
57 # count nodes in each state
58 @nodes_in_state[re[2].to_sym] += 1
60 # update our database (and cache) when a node's state changes
61 if @node_state[re[1]] != re[2]
62 @node_state[re[1]] = re[2]
63 node = Node.where('hostname=?', re[1]).first
65 $stderr.puts "dispatch: update #{re[1]} state to #{re[2]}"
66 node.info[:slurm_state] = re[2]
69 $stderr.puts "dispatch: sinfo reports '#{re[1]}' is not down, but no node has that name"
83 if job.runtime_constraints['min_nodes']
84 min_nodes = begin job.runtime_constraints['min_nodes'].to_i rescue 1 end
89 next if @nodes_in_state[:idle] < min_nodes
93 next if @running[job.uuid]
97 case Server::Application.config.crunch_job_wrapper
100 when :slurm_immediate
101 cmd_args = ["salloc",
106 "--job-name=#{job.uuid}",
107 "--nodes=#{min_nodes}"]
109 raise "Unknown crunch_job_wrapper: #{Server::Application.config.crunch_job_wrapper}"
112 if Server::Application.config.crunch_job_user
113 cmd_args.unshift("sudo", "-E", "-u",
114 Server::Application.config.crunch_job_user,
115 "PERLLIB=#{ENV['PERLLIB']}")
118 job_auth = ApiClientAuthorization.
119 new(user: User.where('uuid=?', job.modified_by_user_uuid).first,
123 cmd_args << (ENV['CRUNCH_JOB_BIN'] || `which crunch-job`.strip)
124 cmd_args << '--job-api-token'
125 cmd_args << job_auth.api_token
129 commit = Commit.where(sha1: job.script_version).first
131 cmd_args << '--git-dir'
132 if File.exists?(File.
133 join(Rails.configuration.git_repositories_dir,
134 commit.repository_name + '.git'))
136 join(Rails.configuration.git_repositories_dir,
137 commit.repository_name + '.git')
140 join(Rails.configuration.git_repositories_dir,
141 commit.repository_name, '.git')
145 $stderr.puts "dispatch: #{cmd_args.join ' '}"
148 i, o, e, t = Open3.popen3(*cmd_args)
150 $stderr.puts "dispatch: popen3: #{$!}"
155 $stderr.puts "dispatch: job #{job.uuid} start"
156 $stderr.puts "dispatch: child #{t.pid} start"
157 @running[job.uuid] = {
173 # no-op -- let crunch-job take care of locking.
178 # no-op -- let crunch-job take care of locking.
183 @running.each do |job_uuid, j|
186 # Throw away child stdout
188 j[:stdout].read_nonblock(2**20)
189 rescue Errno::EAGAIN, EOFError
192 # Read whatever is available from child stderr
195 stderr_buf = j[:stderr].read_nonblock(2**20)
196 rescue Errno::EAGAIN, EOFError
200 j[:stderr_buf] << stderr_buf
201 if j[:stderr_buf].index "\n"
202 lines = j[:stderr_buf].lines("\n").to_a
203 if j[:stderr_buf][-1] == "\n"
206 j[:stderr_buf] = lines.pop
209 $stderr.print "#{job_uuid} ! " unless line.index(job_uuid)
211 $redis.publish job_uuid, "#{Time.now.ctime.to_s} #{line.strip}"
219 return if 0 == @running.size
225 pid_done = waitpid(-1, Process::WNOHANG | Process::WUNTRACED)
227 j_done = @running.values.
228 select { |j| j[:wait_thr].pid == pid_done }.
231 rescue SystemCallError
232 # I have @running processes but system reports I have no
233 # children. This is likely to happen repeatedly if it happens at
234 # all; I will log this no more than once per child process I
236 if 0 < @running.select { |uuid,j| j[:warned_waitpid_error].nil? }.size
237 children = @running.values.collect { |j| j[:wait_thr].pid }.join ' '
238 $stderr.puts "dispatch: IPC bug: waitpid() error (#{$!}), but I have children #{children}"
240 @running.each do |uuid,j| j[:warned_waitpid_error] = true end
243 @running.each do |uuid, j|
244 if j[:wait_thr].status == false
245 pid_done = j[:wait_thr].pid
253 job_done = j_done[:job]
254 $stderr.puts "dispatch: child #{pid_done} exit"
255 $stderr.puts "dispatch: job #{job_done.uuid} end"
256 $redis.publish job_done.uuid, "end"
258 # Ensure every last drop of stdout and stderr is consumed
260 if j_done[:stderr_buf] and j_done[:stderr_buf] != ''
261 $stderr.puts j_done[:stderr_buf] + "\n"
265 j_done[:wait_thr].value
267 # Invalidate the per-job auth token
268 j_done[:job_auth].update_attributes expires_at: Time.now
270 @running.delete job_done.uuid
276 $stderr.puts "dispatch: ready"
277 while !$signal[:term] or @running.size > 0
280 @running.each do |uuid, j|
281 if !j[:started] and j[:sent_int] < 2
283 Process.kill 'INT', j[:wait_thr].pid
285 # No such pid = race condition + desired result is
292 refresh_todo unless did_recently(:refresh_todo, 1.0)
294 start_jobs unless @todo.empty? or did_recently(:start_jobs, 1.0)
297 select(@running.values.collect { |j| [j[:stdout], j[:stderr]] }.flatten,
304 def did_recently(thing, min_interval)
306 if !@did_recently[thing] or @did_recently[thing] < Time.now - min_interval
307 @did_recently[thing] = Time.now