Merge branch '4621-crunch-memory-usage'
[arvados.git] / services / api / script / crunch-dispatch.rb
1 #!/usr/bin/env ruby
2
3 require 'shellwords'
4 include Process
5
6 $options = {}
7 (ARGV.any? ? ARGV : ['--jobs', '--pipelines']).each do |arg|
8   case arg
9   when '--jobs'
10     $options[:jobs] = true
11   when '--pipelines'
12     $options[:pipelines] = true
13   else
14     abort "Unrecognized command line option '#{arg}'"
15   end
16 end
17 if not ($options[:jobs] or $options[:pipelines])
18   abort "Nothing to do. Please specify at least one of: --jobs, --pipelines."
19 end
20
21 ARGV.reject! { |a| a =~ /--jobs|--pipelines/ }
22
23 $warned = {}
24 $signal = {}
25 %w{TERM INT}.each do |sig|
26   signame = sig
27   Signal.trap(sig) do
28     $stderr.puts "Received #{signame} signal"
29     $signal[:term] = true
30   end
31 end
32
33 if ENV["CRUNCH_DISPATCH_LOCKFILE"]
34   lockfilename = ENV.delete "CRUNCH_DISPATCH_LOCKFILE"
35   lockfile = File.open(lockfilename, File::RDWR|File::CREAT, 0644)
36   unless lockfile.flock File::LOCK_EX|File::LOCK_NB
37     abort "Lock unavailable on #{lockfilename} - exit"
38   end
39 end
40
41 ENV["RAILS_ENV"] = ARGV[0] || ENV["RAILS_ENV"] || "development"
42
43 require File.dirname(__FILE__) + '/../config/boot'
44 require File.dirname(__FILE__) + '/../config/environment'
45 require 'open3'
46
47 class LogTime < Time
48   def to_s
49     self.utc.strftime "%Y-%m-%d_%H:%M:%S"
50   end
51 end
52
53 class Dispatcher
54   include ApplicationHelper
55
56   def sysuser
57     return act_as_system_user
58   end
59
60   def refresh_todo
61     @todo = []
62     if $options[:jobs]
63       @todo = Job.queue.select(&:repository)
64     end
65     @todo_pipelines = []
66     if $options[:pipelines]
67       @todo_pipelines = PipelineInstance.queue
68     end
69   end
70
71   def each_slurm_line(cmd, outfmt, max_fields=nil)
72     max_fields ||= outfmt.split(":").size
73     max_fields += 1  # To accommodate the node field we add
74     @@slurm_version ||= Gem::Version.new(`sinfo --version`.match(/\b[\d\.]+\b/)[0])
75     if Gem::Version.new('2.3') <= @@slurm_version
76       `#{cmd} --noheader -o '%n:#{outfmt}'`.each_line do |line|
77         yield line.chomp.split(":", max_fields)
78       end
79     else
80       # Expand rows with hostname ranges (like "foo[1-3,5,9-12]:idle")
81       # into multiple rows with one hostname each.
82       `#{cmd} --noheader -o '%N:#{outfmt}'`.each_line do |line|
83         tokens = line.chomp.split(":", max_fields)
84         if (re = tokens[0].match /^(.*?)\[([-,\d]+)\]$/)
85           tokens.shift
86           re[2].split(",").each do |range|
87             range = range.split("-").collect(&:to_i)
88             (range[0]..range[-1]).each do |n|
89               yield [re[1] + n.to_s] + tokens
90             end
91           end
92         else
93           yield tokens
94         end
95       end
96     end
97   end
98
99   def slurm_status
100     slurm_nodes = {}
101     each_slurm_line("sinfo", "%t") do |hostname, state|
102       # Treat nodes in idle* state as down, because the * means that slurm
103       # hasn't been able to communicate with it recently.
104       state.sub!(/^idle\*/, "down")
105       state.sub!(/\W+$/, "")
106       state = "down" unless %w(idle alloc down).include?(state)
107       slurm_nodes[hostname] = {state: state, job: nil}
108     end
109     each_slurm_line("squeue", "%j") do |hostname, job_uuid|
110       slurm_nodes[hostname][:job] = job_uuid if slurm_nodes[hostname]
111     end
112     slurm_nodes
113   end
114
115   def update_node_status
116     return unless Server::Application.config.crunch_job_wrapper.to_s.match /^slurm/
117     @node_state ||= {}
118     slurm_status.each_pair do |hostname, slurmdata|
119       next if @node_state[hostname] == slurmdata
120       begin
121         node = Node.where('hostname=?', hostname).order(:last_ping_at).last
122         if node
123           $stderr.puts "dispatch: update #{hostname} state to #{slurmdata}"
124           node.info["slurm_state"] = slurmdata[:state]
125           node.job_uuid = slurmdata[:job]
126           if node.save
127             @node_state[hostname] = slurmdata
128           else
129             $stderr.puts "dispatch: failed to update #{node.uuid}: #{node.errors.messages}"
130           end
131         elsif slurmdata[:state] != 'down'
132           $stderr.puts "dispatch: SLURM reports '#{hostname}' is not down, but no node has that name"
133         end
134       rescue => error
135         $stderr.puts "dispatch: error updating #{hostname} node status: #{error}"
136       end
137     end
138   end
139
140   def positive_int(raw_value, default=nil)
141     value = begin raw_value.to_i rescue 0 end
142     if value > 0
143       value
144     else
145       default
146     end
147   end
148
149   NODE_CONSTRAINT_MAP = {
150     # Map Job runtime_constraints keys to the corresponding Node info key.
151     'min_ram_mb_per_node' => 'total_ram_mb',
152     'min_scratch_mb_per_node' => 'total_scratch_mb',
153     'min_cores_per_node' => 'total_cpu_cores',
154   }
155
156   def nodes_available_for_job_now(job)
157     # Find Nodes that satisfy a Job's runtime constraints (by building
158     # a list of Procs and using them to test each Node).  If there
159     # enough to run the Job, return an array of their names.
160     # Otherwise, return nil.
161     need_procs = NODE_CONSTRAINT_MAP.each_pair.map do |job_key, node_key|
162       Proc.new do |node|
163         positive_int(node.info[node_key], 0) >=
164           positive_int(job.runtime_constraints[job_key], 0)
165       end
166     end
167     min_node_count = positive_int(job.runtime_constraints['min_nodes'], 1)
168     usable_nodes = []
169     Node.find_each do |node|
170       good_node = (node.info['slurm_state'] == 'idle')
171       need_procs.each { |node_test| good_node &&= node_test.call(node) }
172       if good_node
173         usable_nodes << node
174         if usable_nodes.count >= min_node_count
175           return usable_nodes.map { |node| node.hostname }
176         end
177       end
178     end
179     nil
180   end
181
182   def nodes_available_for_job(job)
183     # Check if there are enough idle nodes with the Job's minimum
184     # hardware requirements to run it.  If so, return an array of
185     # their names.  If not, up to once per hour, signal start_jobs to
186     # hold off launching Jobs.  This delay is meant to give the Node
187     # Manager an opportunity to make new resources available for new
188     # Jobs.
189     #
190     # The exact timing parameters here might need to be adjusted for
191     # the best balance between helping the longest-waiting Jobs run,
192     # and making efficient use of immediately available resources.
193     # These are all just first efforts until we have more data to work
194     # with.
195     nodelist = nodes_available_for_job_now(job)
196     if nodelist.nil? and not did_recently(:wait_for_available_nodes, 3600)
197       $stderr.puts "dispatch: waiting for nodes for #{job.uuid}"
198       @node_wait_deadline = Time.now + 5.minutes
199     end
200     nodelist
201   end
202
203   def fail_job job, message
204     $stderr.puts "dispatch: #{job.uuid}: #{message}"
205     begin
206       Log.new(object_uuid: job.uuid,
207               event_type: 'dispatch',
208               owner_uuid: job.owner_uuid,
209               summary: message,
210               properties: {"text" => message}).save!
211     rescue
212       $stderr.puts "dispatch: log.create failed"
213     end
214
215     begin
216       job.lock @authorizations[job.uuid].user.uuid
217       job.state = "Failed"
218       if not job.save
219         $stderr.puts "dispatch: save failed setting job #{job.uuid} to failed"
220       end
221     rescue ArvadosModel::AlreadyLockedError
222       $stderr.puts "dispatch: tried to mark job #{job.uuid} as failed but it was already locked by someone else"
223     end
224   end
225
226   def start_jobs
227     @todo.each do |job|
228       next if @running[job.uuid]
229
230       cmd_args = nil
231       case Server::Application.config.crunch_job_wrapper
232       when :none
233         if @running.size > 0
234             # Don't run more than one at a time.
235             return
236         end
237         cmd_args = []
238       when :slurm_immediate
239         nodelist = nodes_available_for_job(job)
240         if nodelist.nil?
241           if Time.now < @node_wait_deadline
242             break
243           else
244             next
245           end
246         end
247         cmd_args = ["salloc",
248                     "--chdir=/",
249                     "--immediate",
250                     "--exclusive",
251                     "--no-kill",
252                     "--job-name=#{job.uuid}",
253                     "--nodelist=#{nodelist.join(',')}"]
254       else
255         raise "Unknown crunch_job_wrapper: #{Server::Application.config.crunch_job_wrapper}"
256       end
257
258       if Server::Application.config.crunch_job_user
259         cmd_args.unshift("sudo", "-E", "-u",
260                          Server::Application.config.crunch_job_user,
261                          "PATH=#{ENV['PATH']}",
262                          "PERLLIB=#{ENV['PERLLIB']}",
263                          "PYTHONPATH=#{ENV['PYTHONPATH']}",
264                          "RUBYLIB=#{ENV['RUBYLIB']}",
265                          "GEM_PATH=#{ENV['GEM_PATH']}")
266       end
267
268       @authorizations ||= {}
269       if @authorizations[job.uuid] and
270           @authorizations[job.uuid].user.uuid != job.modified_by_user_uuid
271         # We already made a token for this job, but we need a new one
272         # because modified_by_user_uuid has changed (the job will run
273         # as a different user).
274         @authorizations[job.uuid].update_attributes expires_at: Time.now
275         @authorizations[job.uuid] = nil
276       end
277       if not @authorizations[job.uuid]
278         auth = ApiClientAuthorization.
279           new(user: User.where('uuid=?', job.modified_by_user_uuid).first,
280               api_client_id: 0)
281         if not auth.save
282           $stderr.puts "dispatch: auth.save failed"
283           next
284         end
285         @authorizations[job.uuid] = auth
286       end
287
288       crunch_job_bin = (ENV['CRUNCH_JOB_BIN'] || `which arv-crunch-job`.strip)
289       if crunch_job_bin == ''
290         raise "No CRUNCH_JOB_BIN env var, and crunch-job not in path."
291       end
292
293       arvados_internal = Rails.configuration.git_internal_dir
294       if not File.exists? arvados_internal
295         $stderr.puts `mkdir -p #{arvados_internal.shellescape} && cd #{arvados_internal.shellescape} && git init --bare`
296       end
297
298       git = "git --git-dir=#{arvados_internal.shellescape}"
299
300       # @fetched_commits[V]==true if we know commit V exists in the
301       # arvados_internal git repository.
302       @fetched_commits ||= {}
303       if !@fetched_commits[job.script_version]
304
305         repo_root = Rails.configuration.git_repositories_dir
306         src_repo = File.join(repo_root, job.repository + '.git')
307         if not File.exists? src_repo
308           src_repo = File.join(repo_root, job.repository, '.git')
309           if not File.exists? src_repo
310             fail_job job, "No #{job.repository}.git or #{job.repository}/.git at #{repo_root}"
311             next
312           end
313         end
314
315         # check if the commit needs to be fetched or not
316         commit_rev = `#{git} rev-list -n1 #{job.script_version.shellescape} 2>/dev/null`.chomp
317         unless $? == 0 and commit_rev == job.script_version
318           # commit does not exist in internal repository, so import the source repository using git fetch-pack
319           cmd = "#{git} fetch-pack --no-progress --all #{src_repo.shellescape}"
320           $stderr.puts "dispatch: #{cmd}"
321           $stderr.puts `#{cmd}`
322           unless $? == 0
323             fail_job job, "git fetch-pack failed"
324             next
325           end
326         end
327         @fetched_commits[job.script_version] = true
328       end
329
330       # @job_tags[J]==V if we know commit V has been tagged J in the
331       # arvados_internal repository. (J is a job UUID, V is a commit
332       # sha1.)
333       @job_tags ||= {}
334       if not @job_tags[job.uuid]
335         cmd = "#{git} tag #{job.uuid.shellescape} #{job.script_version.shellescape} 2>/dev/null"
336         $stderr.puts "dispatch: #{cmd}"
337         $stderr.puts `#{cmd}`
338         unless $? == 0
339           # git tag failed.  This may be because the tag already exists, so check for that.
340           tag_rev = `#{git} rev-list -n1 #{job.uuid.shellescape}`.chomp
341           if $? == 0
342             # We got a revision back
343             if tag_rev != job.script_version
344               # Uh oh, the tag doesn't point to the revision we were expecting.
345               # Someone has been monkeying with the job record and/or git.
346               fail_job job, "Existing tag #{job.uuid} points to commit #{tag_rev} but expected commit #{job.script_version}"
347               next
348             end
349             # we're okay (fall through to setting @job_tags below)
350           else
351             # git rev-list failed for some reason.
352             fail_job job, "'git tag' for #{job.uuid} failed but did not find any existing tag using 'git rev-list'"
353             next
354           end
355         end
356         # 'git tag' was successful, or there is an existing tag that points to the same revision.
357         @job_tags[job.uuid] = job.script_version
358       elsif @job_tags[job.uuid] != job.script_version
359         fail_job job, "Existing tag #{job.uuid} points to commit #{@job_tags[job.uuid]} but this job uses commit #{job.script_version}"
360         next
361       end
362
363       cmd_args << crunch_job_bin
364       cmd_args << '--job-api-token'
365       cmd_args << @authorizations[job.uuid].api_token
366       cmd_args << '--job'
367       cmd_args << job.uuid
368       cmd_args << '--git-dir'
369       cmd_args << arvados_internal
370
371       $stderr.puts "dispatch: #{cmd_args.join ' '}"
372
373       begin
374         i, o, e, t = Open3.popen3(*cmd_args)
375       rescue
376         $stderr.puts "dispatch: popen3: #{$!}"
377         sleep 1
378         next
379       end
380
381       $stderr.puts "dispatch: job #{job.uuid}"
382       start_banner = "dispatch: child #{t.pid} start #{LogTime.now}"
383       $stderr.puts start_banner
384
385       @running[job.uuid] = {
386         stdin: i,
387         stdout: o,
388         stderr: e,
389         wait_thr: t,
390         job: job,
391         buf: {stderr: '', stdout: ''},
392         started: false,
393         sent_int: 0,
394         job_auth: @authorizations[job.uuid],
395         stderr_buf_to_flush: '',
396         stderr_flushed_at: Time.new(0),
397         bytes_logged: 0,
398         events_logged: 0,
399         log_throttle_is_open: true,
400         log_throttle_reset_time: Time.now + Rails.configuration.crunch_log_throttle_period,
401         log_throttle_bytes_so_far: 0,
402         log_throttle_lines_so_far: 0,
403         log_throttle_bytes_skipped: 0,
404       }
405       i.close
406       update_node_status
407     end
408   end
409
410   # Test for hard cap on total output and for log throttling.  Returns whether
411   # the log line should go to output or not.  Modifies "line" in place to
412   # replace it with an error if a logging limit is tripped.
413   def rate_limit running_job, line
414     message = false
415     linesize = line.size
416     if running_job[:log_throttle_is_open]
417       running_job[:log_throttle_lines_so_far] += 1
418       running_job[:log_throttle_bytes_so_far] += linesize
419       running_job[:bytes_logged] += linesize
420
421       if (running_job[:bytes_logged] >
422           Rails.configuration.crunch_limit_log_bytes_per_job)
423         message = "Exceeded log limit #{Rails.configuration.crunch_limit_log_bytes_per_job} bytes (crunch_limit_log_bytes_per_job). Log will be truncated."
424         running_job[:log_throttle_reset_time] = Time.now + 100.years
425         running_job[:log_throttle_is_open] = false
426
427       elsif (running_job[:log_throttle_bytes_so_far] >
428              Rails.configuration.crunch_log_throttle_bytes)
429         remaining_time = running_job[:log_throttle_reset_time] - Time.now
430         message = "Exceeded rate #{Rails.configuration.crunch_log_throttle_bytes} bytes per #{Rails.configuration.crunch_log_throttle_period} seconds (crunch_log_throttle_bytes). Logging will be silenced for the next #{remaining_time.round} seconds.\n"
431         running_job[:log_throttle_is_open] = false
432
433       elsif (running_job[:log_throttle_lines_so_far] >
434              Rails.configuration.crunch_log_throttle_lines)
435         remaining_time = running_job[:log_throttle_reset_time] - Time.now
436         message = "Exceeded rate #{Rails.configuration.crunch_log_throttle_lines} lines per #{Rails.configuration.crunch_log_throttle_period} seconds (crunch_log_throttle_lines), logging will be silenced for the next #{remaining_time.round} seconds.\n"
437         running_job[:log_throttle_is_open] = false
438       end
439     end
440
441     if not running_job[:log_throttle_is_open]
442       # Don't log anything if any limit has been exceeded. Just count lossage.
443       running_job[:log_throttle_bytes_skipped] += linesize
444     end
445
446     if message
447       # Yes, write to logs, but use our "rate exceeded" message
448       # instead of the log message that exceeded the limit.
449       line.replace message
450       true
451     else
452       running_job[:log_throttle_is_open]
453     end
454   end
455
456   def read_pipes
457     @running.each do |job_uuid, j|
458       job = j[:job]
459
460       now = Time.now
461       if now > j[:log_throttle_reset_time]
462         # It has been more than throttle_period seconds since the last
463         # checkpoint so reset the throttle
464         if j[:log_throttle_bytes_skipped] > 0
465           message = "#{job_uuid} ! Skipped #{j[:log_throttle_bytes_skipped]} bytes of log"
466           $stderr.puts message
467           j[:stderr_buf_to_flush] << "#{LogTime.now} #{message}\n"
468         end
469
470         j[:log_throttle_reset_time] = now + Rails.configuration.crunch_log_throttle_period
471         j[:log_throttle_bytes_so_far] = 0
472         j[:log_throttle_lines_so_far] = 0
473         j[:log_throttle_bytes_skipped] = 0
474         j[:log_throttle_is_open] = true
475       end
476
477       j[:buf].each do |stream, streambuf|
478         # Read some data from the child stream
479         buf = ''
480         begin
481           # It's important to use a big enough buffer here. When we're
482           # being flooded with logs, we must read and discard many
483           # bytes at once. Otherwise, we can easily peg a CPU with
484           # time-checking and other loop overhead. (Quick tests show a
485           # 1MiB buffer working 2.5x as fast as a 64 KiB buffer.)
486           #
487           # So don't reduce this buffer size!
488           buf = j[stream].read_nonblock(2**20)
489         rescue Errno::EAGAIN, EOFError
490         end
491
492         # Short circuit the counting code if we're just going to throw
493         # away the data anyway.
494         if not j[:log_throttle_is_open]
495           j[:log_throttle_bytes_skipped] += streambuf.size + buf.size
496           streambuf.replace ''
497           next
498         elsif buf == ''
499           next
500         end
501
502         # Append to incomplete line from previous read, if any
503         streambuf << buf
504
505         bufend = ''
506         streambuf.each_line do |line|
507           if not line.end_with? $/
508             if line.size > Rails.configuration.crunch_log_throttle_bytes
509               # Without a limit here, we'll use 2x an arbitrary amount
510               # of memory, and waste a lot of time copying strings
511               # around, all without providing any feedback to anyone
512               # about what's going on _or_ hitting any of our throttle
513               # limits.
514               #
515               # Here we leave "line" alone, knowing it will never be
516               # sent anywhere: rate_limit() will reach
517               # crunch_log_throttle_bytes immediately. However, we'll
518               # leave [...] in bufend: if the trailing end of the long
519               # line does end up getting sent anywhere, it will have
520               # some indication that it is incomplete.
521               bufend = "[...]"
522             else
523               # If line length is sane, we'll wait for the rest of the
524               # line to appear in the next read_pipes() call.
525               bufend = line
526               break
527             end
528           end
529           # rate_limit returns true or false as to whether to actually log
530           # the line or not.  It also modifies "line" in place to replace
531           # it with an error if a logging limit is tripped.
532           if rate_limit j, line
533             $stderr.print "#{job_uuid} ! " unless line.index(job_uuid)
534             $stderr.puts line
535             pub_msg = "#{LogTime.now} #{line.strip}\n"
536             j[:stderr_buf_to_flush] << pub_msg
537           end
538         end
539
540         # Leave the trailing incomplete line (if any) in streambuf for
541         # next time.
542         streambuf.replace bufend
543       end
544       # Flush buffered logs to the logs table, if appropriate. We have
545       # to do this even if we didn't collect any new logs this time:
546       # otherwise, buffered data older than seconds_between_events
547       # won't get flushed until new data arrives.
548       write_log j
549     end
550   end
551
552   def reap_children
553     return if 0 == @running.size
554     pid_done = nil
555     j_done = nil
556
557     if false
558       begin
559         pid_done = waitpid(-1, Process::WNOHANG | Process::WUNTRACED)
560         if pid_done
561           j_done = @running.values.
562             select { |j| j[:wait_thr].pid == pid_done }.
563             first
564         end
565       rescue SystemCallError
566         # I have @running processes but system reports I have no
567         # children. This is likely to happen repeatedly if it happens at
568         # all; I will log this no more than once per child process I
569         # start.
570         if 0 < @running.select { |uuid,j| j[:warned_waitpid_error].nil? }.size
571           children = @running.values.collect { |j| j[:wait_thr].pid }.join ' '
572           $stderr.puts "dispatch: IPC bug: waitpid() error (#{$!}), but I have children #{children}"
573         end
574         @running.each do |uuid,j| j[:warned_waitpid_error] = true end
575       end
576     else
577       @running.each do |uuid, j|
578         if j[:wait_thr].status == false
579           pid_done = j[:wait_thr].pid
580           j_done = j
581         end
582       end
583     end
584
585     return if !pid_done
586
587     job_done = j_done[:job]
588     $stderr.puts "dispatch: child #{pid_done} exit"
589     $stderr.puts "dispatch: job #{job_done.uuid} end"
590
591     # Ensure every last drop of stdout and stderr is consumed.
592     read_pipes
593     # Reset flush timestamp to make sure log gets written.
594     j_done[:stderr_flushed_at] = Time.new(0)
595     # Write any remaining logs.
596     write_log j_done
597
598     j_done[:buf].each do |stream, streambuf|
599       if streambuf != ''
600         $stderr.puts streambuf + "\n"
601       end
602     end
603
604     # Wait the thread (returns a Process::Status)
605     exit_status = j_done[:wait_thr].value.exitstatus
606
607     jobrecord = Job.find_by_uuid(job_done.uuid)
608     if exit_status != 75 and jobrecord.state == "Running"
609       # crunch-job did not return exit code 75 (see below) and left the job in
610       # the "Running" state, which means there was an unhandled error.  Fail
611       # the job.
612       jobrecord.state = "Failed"
613       if not jobrecord.save
614         $stderr.puts "dispatch: jobrecord.save failed"
615       end
616     else
617       # Don't fail the job if crunch-job didn't even get as far as
618       # starting it. If the job failed to run due to an infrastructure
619       # issue with crunch-job or slurm, we want the job to stay in the
620       # queue. If crunch-job exited after losing a race to another
621       # crunch-job process, it exits 75 and we should leave the job
622       # record alone so the winner of the race do its thing.
623       #
624       # There is still an unhandled race condition: If our crunch-job
625       # process is about to lose a race with another crunch-job
626       # process, but crashes before getting to its "exit 75" (for
627       # example, "cannot fork" or "cannot reach API server") then we
628       # will assume incorrectly that it's our process's fault
629       # jobrecord.started_at is non-nil, and mark the job as failed
630       # even though the winner of the race is probably still doing
631       # fine.
632     end
633
634     # Invalidate the per-job auth token, unless the job is still queued and we
635     # might want to try it again.
636     if jobrecord.state != "Queued"
637       j_done[:job_auth].update_attributes expires_at: Time.now
638     end
639
640     @running.delete job_done.uuid
641   end
642
643   def update_pipelines
644     expire_tokens = @pipe_auth_tokens.dup
645     @todo_pipelines.each do |p|
646       pipe_auth = (@pipe_auth_tokens[p.uuid] ||= ApiClientAuthorization.
647                    create(user: User.where('uuid=?', p.modified_by_user_uuid).first,
648                           api_client_id: 0))
649       puts `export ARVADOS_API_TOKEN=#{pipe_auth.api_token} && arv-run-pipeline-instance --run-pipeline-here --no-wait --instance #{p.uuid}`
650       expire_tokens.delete p.uuid
651     end
652
653     expire_tokens.each do |k, v|
654       v.update_attributes expires_at: Time.now
655       @pipe_auth_tokens.delete k
656     end
657   end
658
659   def run
660     act_as_system_user
661     @running ||= {}
662     @pipe_auth_tokens ||= { }
663     $stderr.puts "dispatch: ready"
664     while !$signal[:term] or @running.size > 0
665       read_pipes
666       if $signal[:term]
667         @running.each do |uuid, j|
668           if !j[:started] and j[:sent_int] < 2
669             begin
670               Process.kill 'INT', j[:wait_thr].pid
671             rescue Errno::ESRCH
672               # No such pid = race condition + desired result is
673               # already achieved
674             end
675             j[:sent_int] += 1
676           end
677         end
678       else
679         refresh_todo unless did_recently(:refresh_todo, 1.0)
680         update_node_status unless did_recently(:update_node_status, 1.0)
681         unless @todo.empty? or did_recently(:start_jobs, 1.0) or $signal[:term]
682           start_jobs
683         end
684         unless (@todo_pipelines.empty? and @pipe_auth_tokens.empty?) or did_recently(:update_pipelines, 5.0)
685           update_pipelines
686         end
687       end
688       reap_children
689       select(@running.values.collect { |j| [j[:stdout], j[:stderr]] }.flatten,
690              [], [], 1)
691     end
692   end
693
694   protected
695
696   def did_recently(thing, min_interval)
697     @did_recently ||= {}
698     if !@did_recently[thing] or @did_recently[thing] < Time.now - min_interval
699       @did_recently[thing] = Time.now
700       false
701     else
702       true
703     end
704   end
705
706   # send message to log table. we want these records to be transient
707   def write_log running_job
708     return if running_job[:stderr_buf_to_flush] == ''
709
710     # Send out to log event if buffer size exceeds the bytes per event or if
711     # it has been at least crunch_log_seconds_between_events seconds since
712     # the last flush.
713     if running_job[:stderr_buf_to_flush].size > Rails.configuration.crunch_log_bytes_per_event or
714         (Time.now - running_job[:stderr_flushed_at]) >= Rails.configuration.crunch_log_seconds_between_events
715       begin
716         log = Log.new(object_uuid: running_job[:job].uuid,
717                       event_type: 'stderr',
718                       owner_uuid: running_job[:job].owner_uuid,
719                       properties: {"text" => running_job[:stderr_buf_to_flush]})
720         log.save!
721         running_job[:events_logged] += 1
722       rescue => exception
723         $stderr.puts "Failed to write logs"
724         $stderr.puts exception.backtrace
725       end
726       running_job[:stderr_buf_to_flush] = ''
727       running_job[:stderr_flushed_at] = Time.now
728     end
729   end
730 end
731
732 # This is how crunch-job child procs know where the "refresh" trigger file is
733 ENV["CRUNCH_REFRESH_TRIGGER"] = Rails.configuration.crunch_refresh_trigger
734
735 Dispatcher.new.run