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'
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 if Server::Application.config.crunch_job_user
74 cmd_args.unshift("sudo", "-u",
75 Server::Application.config.crunch_job_user)
78 job_auth = ApiClientAuthorization.
79 new(user: User.where('uuid=?', job.modified_by_user).first,
83 cmd_args << 'crunch-job'
84 cmd_args << '--job-api-token'
85 cmd_args << job_auth.api_token
89 commit = Commit.where(sha1: job.script_version).first
91 cmd_args << '--git-dir'
93 join(Rails.configuration.git_repositories_dir,
94 commit.repository_name,
98 $stderr.puts "dispatch: #{cmd_args.join ' '}"
101 i, o, e, t = Open3.popen3(*cmd_args)
103 $stderr.puts "dispatch: popen3: #{$!}"
108 $stderr.puts "dispatch: job #{job.uuid} start"
109 $stderr.puts "dispatch: child #{t.pid} start"
110 @running[job.uuid] = {
126 # no-op -- let crunch-job take care of locking.
131 # no-op -- let crunch-job take care of locking.
136 @running.each do |job_uuid, j|
139 # Throw away child stdout
141 j[:stdout].read_nonblock(2**20)
142 rescue Errno::EAGAIN, EOFError
145 # Read whatever is available from child stderr
148 stderr_buf = j[:stderr].read_nonblock(2**20)
149 rescue Errno::EAGAIN, EOFError
153 j[:stderr_buf] << stderr_buf
154 if j[:stderr_buf].index "\n"
155 lines = j[:stderr_buf].lines("\n").to_a
156 if j[:stderr_buf][-1] == "\n"
159 j[:stderr_buf] = lines.pop
162 $stderr.print "#{job_uuid} ! " unless line.index(job_uuid)
171 return if 0 == @running.size
177 pid_done = waitpid(-1, Process::WNOHANG | Process::WUNTRACED)
179 j_done = @running.values.
180 select { |j| j[:wait_thr].pid == pid_done }.
183 rescue SystemCallError
184 # I have @running processes but system reports I have no
185 # children. This is likely to happen repeatedly if it happens at
186 # all; I will log this no more than once per child process I
188 if 0 < @running.select { |uuid,j| j[:warned_waitpid_error].nil? }.size
189 children = @running.values.collect { |j| j[:wait_thr].pid }.join ' '
190 $stderr.puts "dispatch: IPC bug: waitpid() error (#{$!}), but I have children #{children}"
192 @running.each do |uuid,j| j[:warned_waitpid_error] = true end
195 @running.each do |uuid, j|
196 if j[:wait_thr].status == false
197 pid_done = j[:wait_thr].pid
205 job_done = j_done[:job]
206 $stderr.puts "dispatch: child #{pid_done} exit"
207 $stderr.puts "dispatch: job #{job_done.uuid} end"
209 # Ensure every last drop of stdout and stderr is consumed
211 if j_done[:stderr_buf] and j_done[:stderr_buf] != ''
212 $stderr.puts j_done[:stderr_buf] + "\n"
216 j_done[:wait_thr].value
218 # Invalidate the per-job auth token
219 j_done[:job_auth].update_attributes expires_at: Time.now
221 @running.delete job_done.uuid
227 $stderr.puts "dispatch: ready"
228 while !$signal[:term] or @running.size > 0
231 @running.each do |uuid, j|
232 if !j[:started] and j[:sent_int] < 2
234 Process.kill 'INT', j[:wait_thr].pid
236 # No such pid = race condition + desired result is
243 refresh_todo unless did_recently(:refresh_todo, 1.0)
244 start_jobs unless @todo.empty? or did_recently(:start_jobs, 1.0)
247 select(@running.values.collect { |j| [j[:stdout], j[:stderr]] }.flatten,
254 def did_recently(thing, min_interval)
256 if !@did_recently[thing] or @did_recently[thing] < Time.now - min_interval
257 @did_recently[thing] = Time.now