1 class Job < ArvadosModel
4 include CommonApiTemplate
5 attr_protected :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_timestamps_when_state_changes
13 before_validation :update_state_from_old_state_attrs
14 validate :ensure_script_version_is_commit
15 validate :find_docker_image_locator
16 validate :validate_status
17 validate :validate_state_change
19 has_many :commit_ancestors, :foreign_key => :descendant, :primary_key => :script_version
20 has_many(:nodes, foreign_key: :job_uuid, primary_key: :uuid)
22 class SubmitIdReused < StandardError
25 api_accessible :user, extend: :common do |t|
29 t.add :script_parameters
32 t.add :cancelled_by_client_uuid
33 t.add :cancelled_by_user_uuid
40 t.add :is_locked_by_uuid
42 t.add :runtime_constraints
45 t.add :nondeterministic
47 t.add :supplied_script_version
48 t.add :docker_image_locator
54 # Supported states for a job
57 (Running = 'Running'),
58 (Cancelled = 'Cancelled'),
60 (Complete = 'Complete'),
64 update_attributes(finished_at: finished_at || Time.now,
65 success: success.nil? ? false : success,
74 self.where('state = ?', Queued).order('priority desc, created_at')
78 Job::queue.each_with_index do |job, index|
79 if job[:uuid] == self.uuid
87 self.where('running = ?', true).
88 order('priority desc, created_at')
91 def lock locked_by_uuid
94 unless self.state == Queued and self.is_locked_by_uuid.nil?
95 raise AlreadyLockedError
98 self.is_locked_by_uuid = locked_by_uuid
105 def foreign_key_attributes
106 super + %w(output log)
109 def skip_uuid_read_permission_check
110 super + %w(cancelled_by_client_uuid)
113 def skip_uuid_existence_check
114 super + %w(output log)
118 if self.priority.nil?
124 def ensure_script_version_is_commit
125 if self.state == Running
126 # Apparently client has already decided to go for it. This is
127 # needed to run a local job using a local working directory
128 # instead of a commit-ish.
131 if new_record? or script_version_changed?
132 sha1 = Commit.find_commit_range(current_user, self.repository, nil, self.script_version, nil)[0] rescue nil
134 self.supplied_script_version = self.script_version if self.supplied_script_version.nil? or self.supplied_script_version.empty?
135 self.script_version = sha1
137 self.errors.add :script_version, "#{self.script_version} does not resolve to a commit"
143 def ensure_unique_submit_id
145 if Job.where('submit_id=?',self.submit_id).first
146 raise SubmitIdReused.new
152 def find_docker_image_locator
153 # Find the Collection that holds the Docker image specified in the
154 # runtime constraints, and store its locator in docker_image_locator.
155 unless runtime_constraints.is_a? Hash
156 # We're still in validation stage, so we can't assume
157 # runtime_constraints isn't something horrible like an array or
158 # a string. Treat those cases as "no docker image supplied";
159 # other validations will fail anyway.
160 self.docker_image_locator = nil
163 image_search = runtime_constraints['docker_image']
164 image_tag = runtime_constraints['docker_image_tag']
166 self.docker_image_locator = nil
168 elsif coll = Collection.for_latest_docker_image(image_search, image_tag)
169 self.docker_image_locator = coll.portable_data_hash
172 errors.add(:docker_image_locator, "not found for #{image_search}")
179 queue = self.script_parameters.values
180 while not queue.empty?
181 queue = queue.flatten.compact.collect do |v|
185 v.match(/^(([0-9a-f]{32})\b(\+[^,]+)?,?)*$/) do |locator|
186 deps[locator.to_s] = true
195 def permission_to_update
196 if is_locked_by_uuid_was and !(current_user and
197 (current_user.uuid == is_locked_by_uuid_was or
198 current_user.uuid == system_user.uuid))
199 if script_changed? or
200 script_parameters_changed? or
201 script_version_changed? or
202 (!cancelled_at_was.nil? and
203 (cancelled_by_client_uuid_changed? or
204 cancelled_by_user_uuid_changed? or
205 cancelled_at_changed?)) or
206 started_at_changed? or
207 finished_at_changed? or
212 tasks_summary_changed? or
214 logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
218 if !is_locked_by_uuid_changed?
222 logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
224 elsif is_locked_by_uuid_was and is_locked_by_uuid_was != current_user.uuid
225 logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_uuid_was}"
227 elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
228 logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"
236 def update_modified_by_fields
237 if self.cancelled_at_changed?
238 # Ensure cancelled_at cannot be set to arbitrary non-now times,
239 # or changed once it is set.
240 if self.cancelled_at and not self.cancelled_at_was
241 self.cancelled_at = Time.now
242 self.cancelled_by_user_uuid = current_user.uuid
243 self.cancelled_by_client_uuid = current_api_client.andand.uuid
244 @need_crunch_dispatch_trigger = true
246 self.cancelled_at = self.cancelled_at_was
247 self.cancelled_by_user_uuid = self.cancelled_by_user_uuid_was
248 self.cancelled_by_client_uuid = self.cancelled_by_client_uuid_was
254 def trigger_crunch_dispatch_if_cancelled
255 if @need_crunch_dispatch_trigger
256 File.open(Rails.configuration.crunch_refresh_trigger, 'wb') do
257 # That's all, just create/touch a file for crunch-job to see.
262 def update_timestamps_when_state_changes
263 return if not (state_changed? or new_record?)
266 self.started_at ||= Time.now
267 when Failed, Complete
268 self.finished_at ||= Time.now
270 self.cancelled_at ||= Time.now
273 # TODO: Remove the following case block when old "success" and
274 # "running" attrs go away. Until then, this ensures we still
275 # expose correct success/running flags to older clients, even if
276 # some new clients are writing only the new state attribute.
284 when Cancelled, Failed
291 self.running ||= false # Default to false instead of nil.
296 def update_state_from_old_state_attrs
297 # If a client has touched the legacy state attrs, update the
298 # "state" attr to agree with the updated values of the legacy
301 # TODO: Remove this method when old "success" and "running" attrs
303 if cancelled_at_changed? or
308 self.state = Cancelled
309 elsif success == false
311 elsif success == true
312 self.state = Complete
313 elsif running == true
323 if self.state.in?(States)
326 errors.add :state, "#{state.inspect} must be one of: #{States.inspect}"
331 def validate_state_change
333 if self.state_changed?
334 ok = case self.state_was
336 # state isn't set yet
339 # Permit going from queued to any state
342 # From running, may only transition to a finished state
343 [Complete, Failed, Cancelled].include? self.state
344 when Complete, Failed, Cancelled
345 # Once in a finished state, don't permit any more state changes
348 # Any other state transition is also invalid
352 errors.add :state, "invalid change from #{self.state_was} to #{self.state}"