1 class Arvados::V1::JobsController < ApplicationController
2 accept_attribute_as_json :script_parameters, Hash
3 accept_attribute_as_json :runtime_constraints, Hash
4 accept_attribute_as_json :tasks_summary, Hash
5 skip_before_filter :find_object_by_uuid, :only => :queue
6 skip_before_filter :render_404_if_no_object, :only => :queue
9 want_ancestor = @where[:script_version_descends_from]
11 # Check for missing commit_ancestor rows, and create them if
15 includes(:commit_ancestors). # I wish Rails would let me
17 # commit_ancestors I am
20 if o.commit_ancestors.
21 select { |ca| ca.ancestor == want_ancestor }.
22 empty? and !o.script_version.nil?
24 o.commit_ancestors << CommitAncestor.find_or_create_by_descendant_and_ancestor(o.script_version, want_ancestor)
29 select { |ca| ca.ancestor == want_ancestor }.
33 # Now it is safe to do an .includes().where() because we are no
34 # longer interested in jobs that have other ancestors but not
37 includes(:commit_ancestors).
38 where('commit_ancestors.ancestor = ? and commit_ancestors.is = ?',
45 @object.update_attributes cancelled_at: Time.now
50 Q_UPDATE_INTERVAL = 12
51 def initialize(job, opts={})
57 yield "#{@job.uuid} finished at #{@job.finished_at}\n"
60 while not @job.started_at
61 # send a summary (job queue + available nodes) to the client
62 # every few seconds while waiting for the job to start
63 last_ack_at ||= Time.now - Q_UPDATE_INTERVAL - 1
64 if Time.now - last_ack_at >= Q_UPDATE_INTERVAL
65 nodes_in_state = {idle: 0, alloc: 0}
66 ActiveRecord::Base.uncached do
67 Node.where('hostname is not ?', nil).collect do |n|
68 if n.info[:slurm_state]
69 nodes_in_state[n.info[:slurm_state]] ||= 0
70 nodes_in_state[n.info[:slurm_state]] += 1
75 n_queued_before_me = 0
77 break if j.uuid == @job.uuid
78 n_queued_before_me += 1
82 " queue_position #{n_queued_before_me}" \
83 " queue_size #{job_queue.size}" \
84 " nodes_idle #{nodes_in_state[:idle]}" \
85 " nodes_alloc #{nodes_in_state[:alloc]}\n"
86 last_ack_at = Time.now
89 ActiveRecord::Base.uncached do
93 @redis = Redis.new(:timeout => 0)
94 if @redis.exists @job.uuid
95 # A log buffer exists. Start by showing the last few KB.
97 getrange(@job.uuid, 0 - [@opts[:buffer_size], 1].max, -1).
98 sub(/^[^\n]*\n?/, '').
104 # TODO: avoid missing log entries between getrange() above and
106 @redis.subscribe(@job.uuid) do |event|
107 event.message do |channel, msg|
109 @redis.unsubscribe @job.uuid
118 def self._log_tail_follow_requires_parameters
120 buffer_size: {type: 'integer', required: false, default: 2**13}
124 if !@object.andand.uuid
125 return render_not_found
127 if client_accepts_plain_text_stream
128 self.response.headers['Last-Modified'] = Time.now.ctime.to_s
129 self.response_body = LogStreamer.new @object, {
130 buffer_size: (params[:buffer_size].to_i rescue 2**13)
134 href: url_for(uuid: @object.uuid),
135 comment: ('To retrieve the log stream as plain text, ' +
136 'use a request header like "Accept: text/plain"')
145 is_locked_by_uuid: nil,
148 params[:order] ||= 'priority desc, created_at'
149 find_objects_for_index
153 def self._queue_requires_parameters
154 self._index_requires_parameters