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 return super unless @where.is_a? Hash
10 want_ancestor = @where[:script_version_descends_from]
12 # Check for missing commit_ancestor rows, and create them if
16 includes(:commit_ancestors). # I wish Rails would let me
18 # commit_ancestors I am
21 if o.commit_ancestors.
22 select { |ca| ca.ancestor == want_ancestor }.
23 empty? and !o.script_version.nil?
25 o.commit_ancestors << CommitAncestor.find_or_create_by_descendant_and_ancestor(o.script_version, want_ancestor)
30 select { |ca| ca.ancestor == want_ancestor }.
34 # Now it is safe to do an .includes().where() because we are no
35 # longer interested in jobs that have other ancestors but not
38 includes(:commit_ancestors).
39 where('commit_ancestors.ancestor = ? and commit_ancestors.is = ?',
46 reload_object_before_update
47 @object.update_attributes cancelled_at: Time.now
52 Q_UPDATE_INTERVAL = 12
53 def initialize(job, opts={})
59 yield "#{@job.uuid} finished at #{@job.finished_at}\n"
62 while not @job.started_at
63 # send a summary (job queue + available nodes) to the client
64 # every few seconds while waiting for the job to start
65 last_ack_at ||= Time.now - Q_UPDATE_INTERVAL - 1
66 if Time.now - last_ack_at >= Q_UPDATE_INTERVAL
67 nodes_in_state = {idle: 0, alloc: 0}
68 ActiveRecord::Base.uncached do
69 Node.where('hostname is not ?', nil).collect do |n|
70 if n.info[:slurm_state]
71 nodes_in_state[n.info[:slurm_state]] ||= 0
72 nodes_in_state[n.info[:slurm_state]] += 1
77 n_queued_before_me = 0
79 break if j.uuid == @job.uuid
80 n_queued_before_me += 1
84 " queue_position #{n_queued_before_me}" \
85 " queue_size #{job_queue.size}" \
86 " nodes_idle #{nodes_in_state[:idle]}" \
87 " nodes_alloc #{nodes_in_state[:alloc]}\n"
88 last_ack_at = Time.now
91 ActiveRecord::Base.uncached do
95 @redis = Redis.new(:timeout => 0)
96 if @redis.exists @job.uuid
97 # A log buffer exists. Start by showing the last few KB.
99 getrange(@job.uuid, 0 - [@opts[:buffer_size], 1].max, -1).
100 sub(/^[^\n]*\n?/, '').
106 # TODO: avoid missing log entries between getrange() above and
108 @redis.subscribe(@job.uuid) do |event|
109 event.message do |channel, msg|
111 @redis.unsubscribe @job.uuid
120 def self._log_tail_follow_requires_parameters
122 buffer_size: {type: 'integer', required: false, default: 2**13}
126 if !@object.andand.uuid
127 return render_not_found
129 if client_accepts_plain_text_stream
130 self.response.headers['Last-Modified'] = Time.now.ctime.to_s
131 self.response_body = LogStreamer.new @object, {
132 buffer_size: (params[:buffer_size].to_i rescue 2**13)
136 href: url_for(uuid: @object.uuid),
137 comment: ('To retrieve the log stream as plain text, ' +
138 'use a request header like "Accept: text/plain"')
147 is_locked_by_uuid: nil,
150 params[:order] ||= 'priority desc, created_at'
151 find_objects_for_index
155 def self._queue_requires_parameters
156 self._index_requires_parameters