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]
9 [:repository, :script, :script_version, :script_parameters].each do |r|
11 return send_error("#{r} attribute must be specified",
12 status: :unprocessable_entity)
16 # We used to ask for the minimum_, exclude_, and no_reuse params
17 # in the job resource. Now we advertise them as flags that alter
18 # the behavior of the create action.
19 [:minimum_script_version, :exclude_script_versions].each do |attr|
20 if resource_attrs.has_key? attr
21 params[attr] = resource_attrs.delete attr
24 if resource_attrs.has_key? :no_reuse
25 params[:find_or_create] = !resource_attrs.delete(:no_reuse)
28 if params[:find_or_create]
29 return if false.equal?(load_filters_param)
30 if @filters.empty? # Translate older creation parameters into filters.
32 [["repository", "=", resource_attrs[:repository]],
33 ["script", "=", resource_attrs[:script]],
34 ["script_version", "in git",
35 params[:minimum_script_version] || resource_attrs[:script_version]],
36 ["script_version", "not in git", params[:exclude_script_versions]],
37 ].reject { |filter| filter.last.nil? or filter.last.empty? }
38 if image_search = resource_attrs[:runtime_constraints].andand["docker_image"]
39 if image_tag = resource_attrs[:runtime_constraints]["docker_image_tag"]
40 image_search += ":#{image_tag}"
42 @filters.append(["docker_image_locator", "in docker", image_search])
44 @filters.append(["docker_image_locator", "=", nil])
46 if sdk_version = resource_attrs[:runtime_constraints].andand["arvados_sdk_version"]
47 @filters.append(["arvados_sdk_version", "in git", sdk_version])
50 load_job_specific_filters
51 rescue ArgumentError => error
52 return send_error(error.message)
56 # Check specified filters for some reasonableness.
57 filter_names = @filters.map { |f| f.first }.uniq
58 ["repository", "script"].each do |req_filter|
59 if not filter_names.include?(req_filter)
60 return send_error("#{req_filter} filter required")
64 # Search for a reusable Job, and return it if found.
65 @objects = Job.readable_by(current_user)
70 if j.nondeterministic != true and
71 ["Queued", "Running", "Complete"].include?(j.state) and
72 j.script_parameters == resource_attrs[:script_parameters]
73 if j.state != "Complete" && j.owner_uuid == current_user.uuid
74 # We'll use this if we don't find a job that has completed
77 if Collection.readable_by(current_user).find_by_portable_data_hash(j.output)
78 # Record the first job in the list
82 # Ensure that all candidate jobs actually did produce the same output
83 if @object.output != j.output
90 @object ||= incomplete_job
101 reload_object_before_update
102 @object.update_attributes! state: Job::Cancelled
107 @object.lock current_user.uuid
112 Q_UPDATE_INTERVAL = 12
113 def initialize(job, opts={})
119 yield "#{@job.uuid} finished at #{@job.finished_at}\n"
122 while not @job.started_at
123 # send a summary (job queue + available nodes) to the client
124 # every few seconds while waiting for the job to start
125 last_ack_at ||= Time.now - Q_UPDATE_INTERVAL - 1
126 if Time.now - last_ack_at >= Q_UPDATE_INTERVAL
127 nodes_in_state = {idle: 0, alloc: 0}
128 ActiveRecord::Base.uncached do
129 Node.where('hostname is not ?', nil).collect do |n|
130 if n.info[:slurm_state]
131 nodes_in_state[n.info[:slurm_state]] ||= 0
132 nodes_in_state[n.info[:slurm_state]] += 1
136 job_queue = Job.queue
137 n_queued_before_me = 0
138 job_queue.each do |j|
139 break if j.uuid == @job.uuid
140 n_queued_before_me += 1
142 yield "#{Time.now}" \
143 " job #{@job.uuid}" \
144 " queue_position #{n_queued_before_me}" \
145 " queue_size #{job_queue.size}" \
146 " nodes_idle #{nodes_in_state[:idle]}" \
147 " nodes_alloc #{nodes_in_state[:alloc]}\n"
148 last_ack_at = Time.now
151 ActiveRecord::Base.uncached do
159 params[:order] ||= ['priority desc', 'created_at']
160 load_limit_offset_order_params
162 @where.merge!({state: Job::Queued})
163 return if false.equal?(load_filters_param)
164 find_objects_for_index
169 # Users may not be allowed to see all the jobs in the queue, so provide a
170 # method to get just the queue size in order to get a gist of how busy the
172 render :json => {:queue_size => Job.queue.size}
175 def self._create_requires_parameters
179 type: 'boolean', required: false, default: false
182 type: 'array', required: false
184 minimum_script_version: {
185 type: 'string', required: false
187 exclude_script_versions: {
188 type: 'array', required: false
193 def self._queue_requires_parameters
194 self._index_requires_parameters
199 def load_job_specific_filters
200 # Convert Job-specific @filters entries into general SQL filters.
201 script_info = {"repository" => nil, "script" => nil}
202 git_filters = Hash.new do |hash, key|
203 hash[key] = {"max_version" => "HEAD", "exclude_versions" => []}
205 @filters.select! do |(attr, operator, operand)|
206 if (script_info.has_key? attr) and (operator == "=")
207 if script_info[attr].nil?
208 script_info[attr] = operand
209 elsif script_info[attr] != operand
210 raise ArgumentError.new("incompatible #{attr} filters")
215 git_filters[attr]["min_version"] = operand
218 git_filters[attr]["exclude_versions"] += Array.wrap(operand)
220 when "in docker", "not in docker"
221 image_hashes = Array.wrap(operand).flat_map do |search_term|
222 image_search, image_tag = search_term.split(':', 2)
224 find_all_for_docker_image(image_search, image_tag, @read_users).
225 map(&:portable_data_hash)
227 @filters << [attr, operator.sub(/ docker$/, ""), image_hashes]
234 # Build a real script_version filter from any "not? in git" filters.
235 git_filters.each_pair do |attr, filter|
237 when "script_version"
238 script_info.each_pair do |key, value|
240 raise ArgumentError.new("script_version filter needs #{key} filter")
243 filter["repository"] = script_info["repository"]
245 filter["max_version"] = resource_attrs[:script_version]
247 # Using HEAD, set earlier by the hash default, is fine.
249 when "arvados_sdk_version"
250 filter["repository"] = "arvados"
252 raise ArgumentError.new("unknown attribute for git filter: #{attr}")
254 version_range = Commit.find_commit_range(current_user,
255 filter["repository"],
256 filter["min_version"],
257 filter["max_version"],
258 filter["exclude_versions"])
259 if version_range.nil?
261 new("error searching #{filter['repository']} from " +
262 "'#{filter['min_version']}' to '#{filter['max_version']}', " +
263 "excluding #{filter['exclude_versions']}")
265 @filters.append([attr, "in", version_range])
269 def load_filters_param
272 load_job_specific_filters
273 rescue ArgumentError => error
274 send_error(error.message)