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 [:repository, :script, :script_version, :script_parameters].each do |r|
12 :error => "#{r} attribute must be specified"
13 }, status: :unprocessable_entity
17 r = Commit.find_commit_range(current_user,
18 resource_attrs[:repository],
19 resource_attrs[:minimum_script_version],
20 resource_attrs[:script_version],
21 resource_attrs[:exclude_script_versions])
22 if !resource_attrs[:nondeterministic] and !resource_attrs[:no_reuse]
23 # Search for jobs where the script_version is in the list of commits
24 # returned by find_commit_range
26 Job.readable_by(current_user).where(script: resource_attrs[:script],
29 if j.nondeterministic != true and
30 j.success != false and
31 j.script_parameters == resource_attrs[:script_parameters]
32 # Record the first job in the list
36 # Ensure that all candidate jobs actually did produce the same output
37 if @object.output != j.output
48 resource_attrs[:script_version] = r[0]
51 # Don't pass these on to activerecord
52 resource_attrs.delete(:minimum_script_version)
53 resource_attrs.delete(:exclude_script_versions)
54 resource_attrs.delete(:no_reuse)
59 reload_object_before_update
60 @object.update_attributes cancelled_at: Time.now
65 Q_UPDATE_INTERVAL = 12
66 def initialize(job, opts={})
72 yield "#{@job.uuid} finished at #{@job.finished_at}\n"
75 while not @job.started_at
76 # send a summary (job queue + available nodes) to the client
77 # every few seconds while waiting for the job to start
78 last_ack_at ||= Time.now - Q_UPDATE_INTERVAL - 1
79 if Time.now - last_ack_at >= Q_UPDATE_INTERVAL
80 nodes_in_state = {idle: 0, alloc: 0}
81 ActiveRecord::Base.uncached do
82 Node.where('hostname is not ?', nil).collect do |n|
83 if n.info[:slurm_state]
84 nodes_in_state[n.info[:slurm_state]] ||= 0
85 nodes_in_state[n.info[:slurm_state]] += 1
90 n_queued_before_me = 0
92 break if j.uuid == @job.uuid
93 n_queued_before_me += 1
97 " queue_position #{n_queued_before_me}" \
98 " queue_size #{job_queue.size}" \
99 " nodes_idle #{nodes_in_state[:idle]}" \
100 " nodes_alloc #{nodes_in_state[:alloc]}\n"
101 last_ack_at = Time.now
104 ActiveRecord::Base.uncached do
108 @redis = Redis.new(:timeout => 0)
109 if @redis.exists @job.uuid
110 # A log buffer exists. Start by showing the last few KB.
112 getrange(@job.uuid, 0 - [@opts[:buffer_size], 1].max, -1).
113 sub(/^[^\n]*\n?/, '').
119 # TODO: avoid missing log entries between getrange() above and
121 @redis.subscribe(@job.uuid) do |event|
122 event.message do |channel, msg|
124 @redis.unsubscribe @job.uuid
133 def self._log_tail_follow_requires_parameters
135 buffer_size: {type: 'integer', required: false, default: 2**13}
139 if !@object.andand.uuid
140 return render_not_found
142 if client_accepts_plain_text_stream
143 self.response.headers['Last-Modified'] = Time.now.ctime.to_s
144 self.response_body = LogStreamer.new @object, {
145 buffer_size: (params[:buffer_size].to_i rescue 2**13)
149 href: url_for(uuid: @object.uuid),
150 comment: ('To retrieve the log stream as plain text, ' +
151 'use a request header like "Accept: text/plain"')
160 is_locked_by_uuid: nil,
164 params[:order] ||= 'priority desc, created_at'
165 find_objects_for_index
169 def self._queue_requires_parameters
170 self._index_requires_parameters