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, :queue_size]
6 skip_before_filter :render_404_if_no_object, :only => [:queue, :queue_size]
11 [:repository, :script, :script_version, :script_parameters].each do |r|
13 return send_error("#{r} attribute must be specified",
14 status: :unprocessable_entity)
18 # We used to ask for the minimum_, exclude_, and no_reuse params
19 # in the job resource. Now we advertise them as flags that alter
20 # the behavior of the create action.
21 [:minimum_script_version, :exclude_script_versions].each do |attr|
22 if resource_attrs.has_key? attr
23 params[attr] = resource_attrs.delete attr
26 if resource_attrs.has_key? :no_reuse
27 params[:find_or_create] = !resource_attrs.delete(:no_reuse)
30 if params[:find_or_create]
31 return if false.equal?(load_filters_param)
32 if @filters.empty? # Translate older creation parameters into filters.
34 [["repository", "=", resource_attrs[:repository]],
35 ["script", "=", resource_attrs[:script]],
36 ["script_version", "in git",
37 params[:minimum_script_version] || resource_attrs[:script_version]],
38 ["script_version", "not in git", params[:exclude_script_versions]],
39 ].reject { |filter| filter.last.nil? or filter.last.empty? }
40 if image_search = resource_attrs[:runtime_constraints].andand["docker_image"]
41 if image_tag = resource_attrs[:runtime_constraints]["docker_image_tag"]
42 image_search += ":#{image_tag}"
44 @filters.append(["docker_image_locator", "in docker", image_search])
46 @filters.append(["docker_image_locator", "=", nil])
48 if sdk_version = resource_attrs[:runtime_constraints].andand["arvados_sdk_version"]
49 @filters.append(["arvados_sdk_version", "in git", sdk_version])
52 load_job_specific_filters
53 rescue ArgumentError => error
54 return send_error(error.message)
58 # Check specified filters for some reasonableness.
59 filter_names = @filters.map { |f| f.first }.uniq
60 ["repository", "script"].each do |req_filter|
61 if not filter_names.include?(req_filter)
62 return send_error("#{req_filter} filter required")
66 # Search for a reusable Job, and return it if found.
67 @objects = Job.readable_by(current_user)
72 if j.nondeterministic != true and
73 ["Queued", "Running", "Complete"].include?(j.state) and
74 j.script_parameters == resource_attrs[:script_parameters]
75 if j.state != "Complete" && j.owner_uuid == current_user.uuid
76 # We'll use this if we don't find a job that has completed
79 if Collection.readable_by(current_user).find_by_portable_data_hash(j.output)
80 # Record the first job in the list
84 # Ensure that all candidate jobs actually did produce the same output
85 if @object.output != j.output
92 @object ||= incomplete_job
103 reload_object_before_update
104 @object.update_attributes! state: Job::Cancelled
109 @object.lock current_user.uuid
114 Q_UPDATE_INTERVAL = 12
115 def initialize(job, opts={})
121 yield "#{@job.uuid} finished at #{@job.finished_at}\n"
124 while not @job.started_at
125 # send a summary (job queue + available nodes) to the client
126 # every few seconds while waiting for the job to start
127 current_time = db_current_time
128 last_ack_at ||= current_time - Q_UPDATE_INTERVAL - 1
129 if current_time - last_ack_at >= Q_UPDATE_INTERVAL
130 nodes_in_state = {idle: 0, alloc: 0}
131 ActiveRecord::Base.uncached do
132 Node.where('hostname is not ?', nil).collect do |n|
133 if n.info[:slurm_state]
134 nodes_in_state[n.info[:slurm_state]] ||= 0
135 nodes_in_state[n.info[:slurm_state]] += 1
139 job_queue = Job.queue
140 n_queued_before_me = 0
141 job_queue.each do |j|
142 break if j.uuid == @job.uuid
143 n_queued_before_me += 1
145 yield "#{db_current_time}" \
146 " job #{@job.uuid}" \
147 " queue_position #{n_queued_before_me}" \
148 " queue_size #{job_queue.size}" \
149 " nodes_idle #{nodes_in_state[:idle]}" \
150 " nodes_alloc #{nodes_in_state[:alloc]}\n"
151 last_ack_at = db_current_time
154 ActiveRecord::Base.uncached do
162 params[:order] ||= ['priority desc', 'created_at']
163 load_limit_offset_order_params
165 @where.merge!({state: Job::Queued})
166 return if false.equal?(load_filters_param)
167 find_objects_for_index
172 # Users may not be allowed to see all the jobs in the queue, so provide a
173 # method to get just the queue size in order to get a gist of how busy the
175 render :json => {:queue_size => Job.queue.size}
178 def self._create_requires_parameters
182 type: 'boolean', required: false, default: false
185 type: 'array', required: false
187 minimum_script_version: {
188 type: 'string', required: false
190 exclude_script_versions: {
191 type: 'array', required: false
196 def self._queue_requires_parameters
197 self._index_requires_parameters
202 def load_job_specific_filters
203 # Convert Job-specific @filters entries into general SQL filters.
204 script_info = {"repository" => nil, "script" => nil}
205 git_filters = Hash.new do |hash, key|
206 hash[key] = {"max_version" => "HEAD", "exclude_versions" => []}
208 @filters.select! do |(attr, operator, operand)|
209 if (script_info.has_key? attr) and (operator == "=")
210 if script_info[attr].nil?
211 script_info[attr] = operand
212 elsif script_info[attr] != operand
213 raise ArgumentError.new("incompatible #{attr} filters")
218 git_filters[attr]["min_version"] = operand
221 git_filters[attr]["exclude_versions"] += Array.wrap(operand)
223 when "in docker", "not in docker"
224 image_hashes = Array.wrap(operand).flat_map do |search_term|
225 image_search, image_tag = search_term.split(':', 2)
227 find_all_for_docker_image(image_search, image_tag, @read_users).
228 map(&:portable_data_hash)
230 @filters << [attr, operator.sub(/ docker$/, ""), image_hashes]
237 # Build a real script_version filter from any "not? in git" filters.
238 git_filters.each_pair do |attr, filter|
240 when "script_version"
241 script_info.each_pair do |key, value|
243 raise ArgumentError.new("script_version filter needs #{key} filter")
246 filter["repository"] = script_info["repository"]
248 filter["max_version"] = resource_attrs[:script_version]
250 # Using HEAD, set earlier by the hash default, is fine.
252 when "arvados_sdk_version"
253 filter["repository"] = "arvados"
255 raise ArgumentError.new("unknown attribute for git filter: #{attr}")
257 version_range = Commit.find_commit_range(current_user,
258 filter["repository"],
259 filter["min_version"],
260 filter["max_version"],
261 filter["exclude_versions"])
262 if version_range.nil?
264 new("error searching #{filter['repository']} from " +
265 "'#{filter['min_version']}' to '#{filter['max_version']}', " +
266 "excluding #{filter['exclude_versions']}")
268 @filters.append([attr, "in", version_range])
272 def load_filters_param
275 load_job_specific_filters
276 rescue ArgumentError => error
277 send_error(error.message)