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", "not in git", params[:exclude_script_versions]],
37 ].reject { |filter| filter.last.nil? or filter.last.empty? }
38 if !params[:minimum_script_version].blank?
39 @filters << ["script_version", "in git",
40 params[:minimum_script_version]]
42 add_default_git_filter("script_version", resource_attrs[:repository],
43 resource_attrs[:script_version])
45 if image_search = resource_attrs[:runtime_constraints].andand["docker_image"]
46 if image_tag = resource_attrs[:runtime_constraints]["docker_image_tag"]
47 image_search += ":#{image_tag}"
49 image_locator = Collection.
50 for_latest_docker_image(image_search).andand.portable_data_hash
54 @filters << ["docker_image_locator", "=", image_locator]
55 if sdk_version = resource_attrs[:runtime_constraints].andand["arvados_sdk_version"]
56 add_default_git_filter("arvados_sdk_version", "arvados", sdk_version)
59 load_job_specific_filters
60 rescue ArgumentError => error
61 return send_error(error.message)
65 # Check specified filters for some reasonableness.
66 filter_names = @filters.map { |f| f.first }.uniq
67 ["repository", "script"].each do |req_filter|
68 if not filter_names.include?(req_filter)
69 return send_error("#{req_filter} filter required")
73 # Search for a reusable Job, and return it if found.
74 @objects = Job.readable_by(current_user)
79 if j.nondeterministic != true and
80 ["Queued", "Running", "Complete"].include?(j.state) and
81 j.script_parameters == resource_attrs[:script_parameters]
82 if j.state != "Complete" && j.owner_uuid == current_user.uuid
83 # We'll use this if we don't find a job that has completed
86 if Collection.readable_by(current_user).find_by_portable_data_hash(j.output)
87 # Record the first job in the list
91 # Ensure that all candidate jobs actually did produce the same output
92 if @object.output != j.output
99 @object ||= incomplete_job
110 reload_object_before_update
111 @object.update_attributes! state: Job::Cancelled
116 @object.lock current_user.uuid
121 Q_UPDATE_INTERVAL = 12
122 def initialize(job, opts={})
128 yield "#{@job.uuid} finished at #{@job.finished_at}\n"
131 while not @job.started_at
132 # send a summary (job queue + available nodes) to the client
133 # every few seconds while waiting for the job to start
134 current_time = db_current_time
135 last_ack_at ||= current_time - Q_UPDATE_INTERVAL - 1
136 if current_time - last_ack_at >= Q_UPDATE_INTERVAL
137 nodes_in_state = {idle: 0, alloc: 0}
138 ActiveRecord::Base.uncached do
139 Node.where('hostname is not ?', nil).collect do |n|
140 if n.info[:slurm_state]
141 nodes_in_state[n.info[:slurm_state]] ||= 0
142 nodes_in_state[n.info[:slurm_state]] += 1
146 job_queue = Job.queue
147 n_queued_before_me = 0
148 job_queue.each do |j|
149 break if j.uuid == @job.uuid
150 n_queued_before_me += 1
152 yield "#{db_current_time}" \
153 " job #{@job.uuid}" \
154 " queue_position #{n_queued_before_me}" \
155 " queue_size #{job_queue.size}" \
156 " nodes_idle #{nodes_in_state[:idle]}" \
157 " nodes_alloc #{nodes_in_state[:alloc]}\n"
158 last_ack_at = db_current_time
161 ActiveRecord::Base.uncached do
169 params[:order] ||= ['priority desc', 'created_at']
170 load_limit_offset_order_params
172 @where.merge!({state: Job::Queued})
173 return if false.equal?(load_filters_param)
174 find_objects_for_index
179 # Users may not be allowed to see all the jobs in the queue, so provide a
180 # method to get just the queue size in order to get a gist of how busy the
182 render :json => {:queue_size => Job.queue.size}
185 def self._create_requires_parameters
189 type: 'boolean', required: false, default: false
192 type: 'array', required: false
194 minimum_script_version: {
195 type: 'string', required: false
197 exclude_script_versions: {
198 type: 'array', required: false
203 def self._queue_requires_parameters
204 self._index_requires_parameters
209 def add_default_git_filter(attr_name, repo_name, refspec)
210 # Add a filter to @filters for `attr_name` = the latest commit available
211 # in `repo_name` at `refspec`. No filter is added if refspec can't be
213 commits = Commit.find_commit_range(repo_name, nil, refspec, nil)
214 if commit_hash = commits.first
215 @filters << [attr_name, "=", commit_hash]
219 def load_job_specific_filters
220 # Convert Job-specific @filters entries into general SQL filters.
221 script_info = {"repository" => nil, "script" => nil}
222 git_filters = Hash.new do |hash, key|
223 hash[key] = {"max_version" => "HEAD", "exclude_versions" => []}
225 @filters.select! do |(attr, operator, operand)|
226 if (script_info.has_key? attr) and (operator == "=")
227 if script_info[attr].nil?
228 script_info[attr] = operand
229 elsif script_info[attr] != operand
230 raise ArgumentError.new("incompatible #{attr} filters")
235 git_filters[attr]["min_version"] = operand
238 git_filters[attr]["exclude_versions"] += Array.wrap(operand)
240 when "in docker", "not in docker"
241 image_hashes = Array.wrap(operand).flat_map do |search_term|
242 image_search, image_tag = search_term.split(':', 2)
244 find_all_for_docker_image(image_search, image_tag, @read_users).
245 map(&:portable_data_hash)
247 @filters << [attr, operator.sub(/ docker$/, ""), image_hashes]
254 # Build a real script_version filter from any "not? in git" filters.
255 git_filters.each_pair do |attr, filter|
257 when "script_version"
258 script_info.each_pair do |key, value|
260 raise ArgumentError.new("script_version filter needs #{key} filter")
263 filter["repository"] = script_info["repository"]
265 filter["max_version"] = resource_attrs[:script_version]
267 # Using HEAD, set earlier by the hash default, is fine.
269 when "arvados_sdk_version"
270 filter["repository"] = "arvados"
272 raise ArgumentError.new("unknown attribute for git filter: #{attr}")
274 revisions = Commit.find_commit_range(filter["repository"],
275 filter["min_version"],
276 filter["max_version"],
277 filter["exclude_versions"])
280 new("error searching #{filter['repository']} from " +
281 "'#{filter['min_version']}' to '#{filter['max_version']}', " +
282 "excluding #{filter['exclude_versions']}")
284 @filters.append([attr, "in", revisions])
288 def load_filters_param
291 load_job_specific_filters
292 rescue ArgumentError => error
293 send_error(error.message)