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|
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? }
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])
47 load_job_specific_filters
48 rescue ArgumentError => error
49 return send_error(error.message)
53 # Check specified filters for some reasonableness.
54 filter_names = @filters.map { |f| f.first }.uniq
55 ["repository", "script"].each do |req_filter|
56 if not filter_names.include?(req_filter)
57 return send_error("#{req_filter} filter required")
61 # Search for a reusable Job, and return it if found.
62 @objects = Job.readable_by(current_user)
67 if j.nondeterministic != true and
68 ((j.success == true and j.output != nil) or j.running == true) and
69 j.script_parameters == resource_attrs[:script_parameters]
70 if j.running && j.owner_uuid == current_user.uuid
71 # We'll use this if we don't find a job that has completed
74 if Collection.readable_by(current_user).find_by_uuid(j.output)
75 # Record the first job in the list
79 # Ensure that all candidate jobs actually did produce the same output
80 if @object.output != j.output
87 @object ||= incomplete_job
98 reload_object_before_update
99 @object.update_attributes! cancelled_at: Time.now
104 Q_UPDATE_INTERVAL = 12
105 def initialize(job, opts={})
111 yield "#{@job.uuid} finished at #{@job.finished_at}\n"
114 while not @job.started_at
115 # send a summary (job queue + available nodes) to the client
116 # every few seconds while waiting for the job to start
117 last_ack_at ||= Time.now - Q_UPDATE_INTERVAL - 1
118 if Time.now - last_ack_at >= Q_UPDATE_INTERVAL
119 nodes_in_state = {idle: 0, alloc: 0}
120 ActiveRecord::Base.uncached do
121 Node.where('hostname is not ?', nil).collect do |n|
122 if n.info[:slurm_state]
123 nodes_in_state[n.info[:slurm_state]] ||= 0
124 nodes_in_state[n.info[:slurm_state]] += 1
128 job_queue = Job.queue
129 n_queued_before_me = 0
130 job_queue.each do |j|
131 break if j.uuid == @job.uuid
132 n_queued_before_me += 1
134 yield "#{Time.now}" \
135 " job #{@job.uuid}" \
136 " queue_position #{n_queued_before_me}" \
137 " queue_size #{job_queue.size}" \
138 " nodes_idle #{nodes_in_state[:idle]}" \
139 " nodes_alloc #{nodes_in_state[:alloc]}\n"
140 last_ack_at = Time.now
143 ActiveRecord::Base.uncached do
151 params[:order] ||= ['priority desc', 'created_at']
152 load_limit_offset_order_params
156 is_locked_by_uuid: nil,
160 return if false.equal?(load_filters_param)
161 find_objects_for_index
165 def self._create_requires_parameters
169 type: 'boolean', required: false, default: false
172 type: 'array', required: false
174 minimum_script_version: {
175 type: 'string', required: false
177 exclude_script_versions: {
178 type: 'array', required: false
183 def self._queue_requires_parameters
184 self._index_requires_parameters
189 def load_job_specific_filters
190 # Convert Job-specific @filters entries into general SQL filters.
191 script_info = {"repository" => nil, "script" => nil}
192 script_range = {"exclude_versions" => []}
193 @filters.select! do |filter|
194 if (script_info.has_key? filter[0]) and (filter[1] == "=")
195 if script_info[filter[0]].nil?
196 script_info[filter[0]] = filter[2]
197 elsif script_info[filter[0]] != filter[2]
198 raise ArgumentError.new("incompatible #{filter[0]} filters")
202 when ["script_version", "in git"]
203 script_range["min_version"] = filter.last
205 when ["script_version", "not in git"]
207 script_range["exclude_versions"] += filter.last
209 script_range["exclude_versions"] << filter.last
212 when ["docker_image_locator", "in docker"], ["docker_image_locator", "not in docker"]
213 filter[1].sub!(/ docker$/, '')
214 search_list = filter[2].is_a?(Enumerable) ? filter[2] : [filter[2]]
215 filter[2] = search_list.flat_map do |search_term|
216 image_search, image_tag = search_term.split(':', 2)
217 Collection.uuids_for_docker_image(image_search, image_tag, @read_users)
225 # Build a real script_version filter from any "not? in git" filters.
226 if (script_range.size > 1) or script_range["exclude_versions"].any?
227 script_info.each_pair do |key, value|
229 raise ArgumentError.new("script_version filter needs #{key} filter")
232 last_version = begin resource_attrs[:script_version] rescue "HEAD" end
233 version_range = Commit.find_commit_range(current_user,
234 script_info["repository"],
235 script_range["min_version"],
237 script_range["exclude_versions"])
238 if version_range.nil?
240 new(["error searching #{script_info['repository']} from",
241 "#{script_range['min_version']} to #{last_version},",
242 "excluding #{script_range['exclude_versions']}"].join(" "))
244 @filters.append(["script_version", "in", version_range])
248 def load_filters_param
251 load_job_specific_filters
252 rescue ArgumentError => error
253 send_error(error.message)