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
18 has_many :commit_ancestors, :foreign_key => :descendant, :primary_key => :script_version
20 class SubmitIdReused < StandardError
23 api_accessible :user, extend: :common do |t|
27 t.add :script_parameters
30 t.add :cancelled_by_client_uuid
31 t.add :cancelled_by_user_uuid
38 t.add :is_locked_by_uuid
40 t.add :runtime_constraints
43 t.add :nondeterministic
45 t.add :supplied_script_version
46 t.add :docker_image_locator
51 # Supported states for a job
54 (Running = 'Running'),
55 (Cancelled = 'Cancelled'),
57 (Complete = 'Complete'),
61 update_attributes(finished_at: finished_at || Time.now,
62 success: success.nil? ? false : success,
67 self.where('started_at is ? and is_locked_by_uuid is ? and cancelled_at is ? and success is ?',
69 order('priority desc, created_at')
74 Job::queue.each do |j|
75 if j[:uuid] == self.uuid
83 self.where('running = ?', true).
84 order('priority desc, created_at')
89 def foreign_key_attributes
90 super + %w(output log)
93 def skip_uuid_read_permission_check
94 super + %w(cancelled_by_client_uuid)
97 def skip_uuid_existence_check
98 super + %w(output log)
102 if self.priority.nil?
108 def ensure_script_version_is_commit
109 if self.is_locked_by_uuid and self.started_at
110 # Apparently client has already decided to go for it. This is
111 # needed to run a local job using a local working directory
112 # instead of a commit-ish.
115 if new_record? or script_version_changed?
116 sha1 = Commit.find_commit_range(current_user, self.repository, nil, self.script_version, nil)[0] rescue nil
118 self.supplied_script_version = self.script_version if self.supplied_script_version.nil? or self.supplied_script_version.empty?
119 self.script_version = sha1
121 self.errors.add :script_version, "#{self.script_version} does not resolve to a commit"
127 def ensure_unique_submit_id
129 if Job.where('submit_id=?',self.submit_id).first
130 raise SubmitIdReused.new
136 def find_docker_image_locator
137 # Find the Collection that holds the Docker image specified in the
138 # runtime constraints, and store its locator in docker_image_locator.
139 unless runtime_constraints.is_a? Hash
140 # We're still in validation stage, so we can't assume
141 # runtime_constraints isn't something horrible like an array or
142 # a string. Treat those cases as "no docker image supplied";
143 # other validations will fail anyway.
144 self.docker_image_locator = nil
147 image_search = runtime_constraints['docker_image']
148 image_tag = runtime_constraints['docker_image_tag']
150 self.docker_image_locator = nil
152 elsif coll = Collection.for_latest_docker_image(image_search, image_tag)
153 self.docker_image_locator = coll.portable_data_hash
156 errors.add(:docker_image_locator, "not found for #{image_search}")
163 queue = self.script_parameters.values
164 while not queue.empty?
165 queue = queue.flatten.compact.collect do |v|
169 v.match(/^(([0-9a-f]{32})\b(\+[^,]+)?,?)*$/) do |locator|
170 deps[locator.to_s] = true
179 def permission_to_update
180 if is_locked_by_uuid_was and !(current_user and
181 (current_user.uuid == is_locked_by_uuid_was or
182 current_user.uuid == system_user.uuid))
183 if script_changed? or
184 script_parameters_changed? or
185 script_version_changed? or
186 (!cancelled_at_was.nil? and
187 (cancelled_by_client_uuid_changed? or
188 cancelled_by_user_uuid_changed? or
189 cancelled_at_changed?)) or
190 started_at_changed? or
191 finished_at_changed? or
196 tasks_summary_changed?
197 logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
201 if !is_locked_by_uuid_changed?
205 logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
207 elsif is_locked_by_uuid_was and is_locked_by_uuid_was != current_user.uuid
208 logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_uuid_was}"
210 elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
211 logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"
219 def update_modified_by_fields
220 if self.cancelled_at_changed?
221 # Ensure cancelled_at cannot be set to arbitrary non-now times,
222 # or changed once it is set.
223 if self.cancelled_at and not self.cancelled_at_was
224 self.cancelled_at = Time.now
225 self.cancelled_by_user_uuid = current_user.uuid
226 self.cancelled_by_client_uuid = current_api_client.andand.uuid
227 @need_crunch_dispatch_trigger = true
229 self.cancelled_at = self.cancelled_at_was
230 self.cancelled_by_user_uuid = self.cancelled_by_user_uuid_was
231 self.cancelled_by_client_uuid = self.cancelled_by_client_uuid_was
237 def trigger_crunch_dispatch_if_cancelled
238 if @need_crunch_dispatch_trigger
239 File.open(Rails.configuration.crunch_refresh_trigger, 'wb') do
240 # That's all, just create/touch a file for crunch-job to see.
245 def update_timestamps_when_state_changes
246 return if not (state_changed? or new_record?)
249 self.started_at ||= Time.now
250 when Failed, Complete
251 self.finished_at ||= Time.now
253 self.cancelled_at ||= Time.now
256 # TODO: Remove the following case block when old "success" and
257 # "running" attrs go away. Until then, this ensures we still
258 # expose correct success/running flags to older clients, even if
259 # some new clients are writing only the new state attribute.
267 when Cancelled, Failed
274 self.running ||= false # Default to false instead of nil.
279 def update_state_from_old_state_attrs
280 # If a client has touched the legacy state attrs, update the
281 # "state" attr to agree with the updated values of the legacy
284 # TODO: Remove this method when old "success" and "running" attrs
286 if cancelled_at_changed? or
291 self.state = Cancelled
292 elsif success == false
294 elsif success == true
295 self.state = Complete
296 elsif running == true
306 if self.state.in?(States)
309 errors.add :state, "#{state.inspect} must be one of: #{States.inspect}"