1 class Job < ArvadosModel
4 include CommonApiTemplate
5 attr_protected :arvados_sdk_version, :docker_image_locator
6 serialize :script_parameters, Hash
7 serialize :runtime_constraints, Hash
8 serialize :tasks_summary, Hash
9 before_create :ensure_unique_submit_id
10 after_commit :trigger_crunch_dispatch_if_cancelled, :on => :update
11 before_validation :set_priority
12 before_validation :update_state_from_old_state_attrs
13 validate :ensure_script_version_is_commit
14 validate :find_arvados_sdk_version
15 validate :find_docker_image_locator
16 validate :validate_status
17 validate :validate_state_change
18 before_save :update_timestamps_when_state_changes
20 has_many :commit_ancestors, :foreign_key => :descendant, :primary_key => :script_version
21 has_many(:nodes, foreign_key: :job_uuid, primary_key: :uuid)
23 class SubmitIdReused < StandardError
26 api_accessible :user, extend: :common do |t|
30 t.add :script_parameters
33 t.add :cancelled_by_client_uuid
34 t.add :cancelled_by_user_uuid
41 t.add :is_locked_by_uuid
43 t.add :runtime_constraints
46 t.add :nondeterministic
48 t.add :supplied_script_version
49 t.add :arvados_sdk_version
50 t.add :docker_image_locator
56 # Supported states for a job
59 (Running = 'Running'),
60 (Cancelled = 'Cancelled'),
62 (Complete = 'Complete'),
66 update_attributes(finished_at: finished_at || Time.now,
67 success: success.nil? ? false : success,
76 self.where('state = ?', Queued).order('priority desc, created_at')
80 Job::queue.each_with_index do |job, index|
81 if job[:uuid] == self.uuid
89 self.where('running = ?', true).
90 order('priority desc, created_at')
93 def lock locked_by_uuid
96 unless self.state == Queued and self.is_locked_by_uuid.nil?
97 raise AlreadyLockedError
100 self.is_locked_by_uuid = locked_by_uuid
107 def foreign_key_attributes
108 super + %w(output log)
111 def skip_uuid_read_permission_check
112 super + %w(cancelled_by_client_uuid)
115 def skip_uuid_existence_check
116 super + %w(output log)
120 if self.priority.nil?
126 def ensure_script_version_is_commit
127 if self.state == Running
128 # Apparently client has already decided to go for it. This is
129 # needed to run a local job using a local working directory
130 # instead of a commit-ish.
133 if new_record? or script_version_changed?
134 sha1 = Commit.find_commit_range(current_user, self.repository, nil, self.script_version, nil)[0] rescue nil
136 self.supplied_script_version = self.script_version if self.supplied_script_version.nil? or self.supplied_script_version.empty?
137 self.script_version = sha1
139 self.errors.add :script_version, "#{self.script_version} does not resolve to a commit"
145 def ensure_unique_submit_id
147 if Job.where('submit_id=?',self.submit_id).first
148 raise SubmitIdReused.new
154 def resolve_runtime_constraint(key, attr_sym)
155 if ((runtime_constraints.is_a? Hash) and
156 (search = runtime_constraints[key]))
157 ok, result = yield search
159 ok, result = true, nil
162 send("#{attr_sym}=".to_sym, result)
164 errors.add(attr_sym, result)
169 def find_arvados_sdk_version
170 resolve_runtime_constraint("arvados_sdk_version",
171 :arvados_sdk_version) do |git_search|
172 commits = Commit.find_commit_range(current_user, "arvados",
173 nil, git_search, nil)
174 if commits.andand.any?
175 [true, commits.first]
177 [false, "#{git_search} does not resolve to a commit"]
182 def find_docker_image_locator
183 resolve_runtime_constraint("docker_image",
184 :docker_image_locator) do |image_search|
185 image_tag = runtime_constraints['docker_image_tag']
186 if coll = Collection.for_latest_docker_image(image_search, image_tag)
187 [true, coll.portable_data_hash]
189 [false, "not found for #{image_search}"]
196 queue = self.script_parameters.values
197 while not queue.empty?
198 queue = queue.flatten.compact.collect do |v|
202 v.match(/^(([0-9a-f]{32})\b(\+[^,]+)?,?)*$/) do |locator|
203 deps[locator.to_s] = true
212 def permission_to_update
213 if is_locked_by_uuid_was and !(current_user and
214 (current_user.uuid == is_locked_by_uuid_was or
215 current_user.uuid == system_user.uuid))
216 if script_changed? or
217 script_parameters_changed? or
218 script_version_changed? or
219 (!cancelled_at_was.nil? and
220 (cancelled_by_client_uuid_changed? or
221 cancelled_by_user_uuid_changed? or
222 cancelled_at_changed?)) or
223 started_at_changed? or
224 finished_at_changed? or
229 tasks_summary_changed? or
231 logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
235 if !is_locked_by_uuid_changed?
239 logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
241 elsif is_locked_by_uuid_was and is_locked_by_uuid_was != current_user.uuid
242 logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_uuid_was}"
244 elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
245 logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"
253 def update_modified_by_fields
254 if self.cancelled_at_changed?
255 # Ensure cancelled_at cannot be set to arbitrary non-now times,
256 # or changed once it is set.
257 if self.cancelled_at and not self.cancelled_at_was
258 self.cancelled_at = Time.now
259 self.cancelled_by_user_uuid = current_user.uuid
260 self.cancelled_by_client_uuid = current_api_client.andand.uuid
261 @need_crunch_dispatch_trigger = true
263 self.cancelled_at = self.cancelled_at_was
264 self.cancelled_by_user_uuid = self.cancelled_by_user_uuid_was
265 self.cancelled_by_client_uuid = self.cancelled_by_client_uuid_was
271 def trigger_crunch_dispatch_if_cancelled
272 if @need_crunch_dispatch_trigger
273 File.open(Rails.configuration.crunch_refresh_trigger, 'wb') do
274 # That's all, just create/touch a file for crunch-job to see.
279 def update_timestamps_when_state_changes
280 return if not (state_changed? or new_record?)
284 self.started_at ||= Time.now
285 when Failed, Complete
286 self.finished_at ||= Time.now
288 self.cancelled_at ||= Time.now
291 # TODO: Remove the following case block when old "success" and
292 # "running" attrs go away. Until then, this ensures we still
293 # expose correct success/running flags to older clients, even if
294 # some new clients are writing only the new state attribute.
302 when Cancelled, Failed
309 self.running ||= false # Default to false instead of nil.
314 def update_state_from_old_state_attrs
315 # If a client has touched the legacy state attrs, update the
316 # "state" attr to agree with the updated values of the legacy
319 # TODO: Remove this method when old "success" and "running" attrs
321 if cancelled_at_changed? or
326 self.state = Cancelled
327 elsif success == false
329 elsif success == true
330 self.state = Complete
331 elsif running == true
341 if self.state.in?(States)
344 errors.add :state, "#{state.inspect} must be one of: #{States.inspect}"
349 def validate_state_change
351 if self.state_changed?
352 ok = case self.state_was
354 # state isn't set yet
357 # Permit going from queued to any state
360 # From running, may only transition to a finished state
361 [Complete, Failed, Cancelled].include? self.state
362 when Complete, Failed, Cancelled
363 # Once in a finished state, don't permit any more state changes
366 # Any other state transition is also invalid
370 errors.add :state, "invalid change from #{self.state_was} to #{self.state}"