1 class Job < ArvadosModel
4 include CommonApiTemplate
5 serialize :components, Hash
6 attr_protected :arvados_sdk_version, :docker_image_locator
7 serialize :script_parameters, Hash
8 serialize :runtime_constraints, Hash
9 serialize :tasks_summary, Hash
10 before_create :ensure_unique_submit_id
11 after_commit :trigger_crunch_dispatch_if_cancelled, :on => :update
12 before_validation :set_priority
13 before_validation :update_state_from_old_state_attrs
14 before_validation :update_script_parameters_digest
15 validate :ensure_script_version_is_commit
16 validate :find_docker_image_locator
17 validate :find_arvados_sdk_version
18 validate :validate_status
19 validate :validate_state_change
20 validate :ensure_no_collection_uuids_in_script_params
21 before_save :tag_version_in_internal_repository
22 before_save :update_timestamps_when_state_changes
24 has_many :commit_ancestors, :foreign_key => :descendant, :primary_key => :script_version
25 has_many(:nodes, foreign_key: :job_uuid, primary_key: :uuid)
27 class SubmitIdReused < StandardError
30 api_accessible :user, extend: :common do |t|
34 t.add :script_parameters
37 t.add :cancelled_by_client_uuid
38 t.add :cancelled_by_user_uuid
45 t.add :is_locked_by_uuid
47 t.add :runtime_constraints
49 t.add :nondeterministic
51 t.add :supplied_script_version
52 t.add :arvados_sdk_version
53 t.add :docker_image_locator
60 # Supported states for a job
63 (Running = 'Running'),
64 (Cancelled = 'Cancelled'),
66 (Complete = 'Complete'),
70 update_attributes(finished_at: finished_at || db_current_time,
71 success: success.nil? ? false : success,
80 self.where('state = ?', Queued).order('priority desc, created_at')
84 # We used to report this accurately, but the implementation made queue
85 # API requests O(n**2) for the size of the queue. See #8800.
86 # We've soft-disabled it because it's not clear we even want this
87 # functionality: now that we have Node Manager with support for multiple
88 # node sizes, "queue position" tells you very little about when a job will
90 state == Queued ? 0 : nil
94 self.where('running = ?', true).
95 order('priority desc, created_at')
98 def lock locked_by_uuid
100 unless self.state == Queued and self.is_locked_by_uuid.nil?
101 raise AlreadyLockedError
104 self.is_locked_by_uuid = locked_by_uuid
109 def update_script_parameters_digest
110 self.script_parameters_digest = self.class.sorted_hash_digest(script_parameters)
113 def self.searchable_columns operator
114 super - ["script_parameters_digest"]
119 def self.sorted_hash_digest h
120 Digest::MD5.hexdigest(Oj.dump(deep_sort_hash(h)))
123 def self.deep_sort_hash h
124 return h unless h.is_a? Hash
125 h.sort.collect do |k, v|
126 [k, deep_sort_hash(v)]
130 def foreign_key_attributes
131 super + %w(output log)
134 def skip_uuid_read_permission_check
135 super + %w(cancelled_by_client_uuid)
138 def skip_uuid_existence_check
139 super + %w(output log)
143 if self.priority.nil?
149 def ensure_script_version_is_commit
151 # Apparently client has already decided to go for it. This is
152 # needed to run a local job using a local working directory
153 # instead of a commit-ish.
156 if new_record? or repository_changed? or script_version_changed?
157 sha1 = Commit.find_commit_range(repository,
158 nil, script_version, nil).first
160 errors.add :script_version, "#{script_version} does not resolve to a commit"
163 if supplied_script_version.nil? or supplied_script_version.empty?
164 self.supplied_script_version = script_version
166 self.script_version = sha1
171 def tag_version_in_internal_repository
173 # No point now. See ensure_script_version_is_commit.
176 # Won't be saved, and script_version might not even be valid.
178 elsif new_record? or repository_changed? or script_version_changed?
182 Commit.tag_in_internal_repository repository, script_version, uuid
190 def ensure_unique_submit_id
192 if Job.where('submit_id=?',self.submit_id).first
193 raise SubmitIdReused.new
199 def resolve_runtime_constraint(key, attr_sym)
200 if ((runtime_constraints.is_a? Hash) and
201 (search = runtime_constraints[key]))
202 ok, result = yield search
204 ok, result = true, nil
207 send("#{attr_sym}=".to_sym, result)
209 errors.add(attr_sym, result)
214 def find_arvados_sdk_version
215 resolve_runtime_constraint("arvados_sdk_version",
216 :arvados_sdk_version) do |git_search|
217 commits = Commit.find_commit_range("arvados",
218 nil, git_search, nil)
220 [false, "#{git_search} does not resolve to a commit"]
221 elsif not runtime_constraints["docker_image"]
222 [false, "cannot be specified without a Docker image constraint"]
224 [true, commits.first]
229 def find_docker_image_locator
230 runtime_constraints['docker_image'] =
231 Rails.configuration.default_docker_image_for_jobs if ((runtime_constraints.is_a? Hash) and
232 (runtime_constraints['docker_image']).nil? and
233 Rails.configuration.default_docker_image_for_jobs)
234 resolve_runtime_constraint("docker_image",
235 :docker_image_locator) do |image_search|
236 image_tag = runtime_constraints['docker_image_tag']
237 if coll = Collection.for_latest_docker_image(image_search, image_tag)
238 [true, coll.portable_data_hash]
240 [false, "not found for #{image_search}"]
245 def permission_to_update
246 if is_locked_by_uuid_was and !(current_user and
247 (current_user.uuid == is_locked_by_uuid_was or
248 current_user.uuid == system_user.uuid))
249 if script_changed? or
250 script_parameters_changed? or
251 script_version_changed? or
252 (!cancelled_at_was.nil? and
253 (cancelled_by_client_uuid_changed? or
254 cancelled_by_user_uuid_changed? or
255 cancelled_at_changed?)) or
256 started_at_changed? or
257 finished_at_changed? or
262 tasks_summary_changed? or
265 logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
269 if !is_locked_by_uuid_changed?
273 logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
275 elsif is_locked_by_uuid_was and is_locked_by_uuid_was != current_user.uuid
276 logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_uuid_was}"
278 elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
279 logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"
287 def update_modified_by_fields
288 if self.cancelled_at_changed?
289 # Ensure cancelled_at cannot be set to arbitrary non-now times,
290 # or changed once it is set.
291 if self.cancelled_at and not self.cancelled_at_was
292 self.cancelled_at = db_current_time
293 self.cancelled_by_user_uuid = current_user.uuid
294 self.cancelled_by_client_uuid = current_api_client.andand.uuid
295 @need_crunch_dispatch_trigger = true
297 self.cancelled_at = self.cancelled_at_was
298 self.cancelled_by_user_uuid = self.cancelled_by_user_uuid_was
299 self.cancelled_by_client_uuid = self.cancelled_by_client_uuid_was
305 def trigger_crunch_dispatch_if_cancelled
306 if @need_crunch_dispatch_trigger
307 File.open(Rails.configuration.crunch_refresh_trigger, 'wb') do
308 # That's all, just create/touch a file for crunch-job to see.
313 def update_timestamps_when_state_changes
314 return if not (state_changed? or new_record?)
318 self.started_at ||= db_current_time
319 when Failed, Complete
320 self.finished_at ||= db_current_time
322 self.cancelled_at ||= db_current_time
325 # TODO: Remove the following case block when old "success" and
326 # "running" attrs go away. Until then, this ensures we still
327 # expose correct success/running flags to older clients, even if
328 # some new clients are writing only the new state attribute.
336 when Cancelled, Failed
343 self.running ||= false # Default to false instead of nil.
345 @need_crunch_dispatch_trigger = true
350 def update_state_from_old_state_attrs
351 # If a client has touched the legacy state attrs, update the
352 # "state" attr to agree with the updated values of the legacy
355 # TODO: Remove this method when old "success" and "running" attrs
357 if cancelled_at_changed? or
362 self.state = Cancelled
363 elsif success == false
365 elsif success == true
366 self.state = Complete
367 elsif running == true
377 if self.state.in?(States)
380 errors.add :state, "#{state.inspect} must be one of: #{States.inspect}"
385 def validate_state_change
387 if self.state_changed?
388 ok = case self.state_was
390 # state isn't set yet
393 # Permit going from queued to any state
396 # From running, may only transition to a finished state
397 [Complete, Failed, Cancelled].include? self.state
398 when Complete, Failed, Cancelled
399 # Once in a finished state, don't permit any more state changes
402 # Any other state transition is also invalid
406 errors.add :state, "invalid change from #{self.state_was} to #{self.state}"
412 def ensure_no_collection_uuids_in_script_params
413 # recursive_hash_search searches recursively through hashes and
414 # arrays in 'thing' for string fields matching regular expression
415 # 'pattern'. Returns true if pattern is found, false otherwise.
416 def recursive_hash_search thing, pattern
419 return true if recursive_hash_search v, pattern
421 elsif thing.is_a? Array
423 return true if recursive_hash_search k, pattern
425 elsif thing.is_a? String
426 return true if thing.match pattern
431 # Fail validation if any script_parameters field includes a string containing a
432 # collection uuid pattern.
433 if self.script_parameters_changed?
434 if recursive_hash_search(self.script_parameters, Collection.uuid_regex)
435 self.errors.add :script_parameters, "must use portable_data_hash instead of collection uuid"