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 validate :ensure_script_version_is_commit
12 validate :find_docker_image_locator
14 has_many :commit_ancestors, :foreign_key => :descendant, :primary_key => :script_version
16 class SubmitIdReused < StandardError
19 api_accessible :user, extend: :common do |t|
23 t.add :script_parameters
26 t.add :cancelled_by_client_uuid
27 t.add :cancelled_by_user_uuid
31 t.add :output_is_persistent
34 t.add :is_locked_by_uuid
36 t.add :runtime_constraints
39 t.add :nondeterministic
41 t.add :supplied_script_version
42 t.add :docker_image_locator
48 update_attributes(finished_at: finished_at || Time.now,
49 success: success.nil? ? false : success,
54 self.where('started_at is ? and is_locked_by_uuid is ? and cancelled_at is ? and success is ?',
56 order('priority desc, created_at')
60 self.where('running = ?', true).
61 order('priority desc, created_at')
66 def foreign_key_attributes
67 super + %w(output log)
70 def skip_uuid_read_permission_check
71 super + %w(cancelled_by_client_uuid)
74 def skip_uuid_existence_check
75 super + %w(output log)
78 def ensure_script_version_is_commit
79 if self.is_locked_by_uuid and self.started_at
80 # Apparently client has already decided to go for it. This is
81 # needed to run a local job using a local working directory
82 # instead of a commit-ish.
85 if new_record? or script_version_changed?
86 sha1 = Commit.find_commit_range(current_user, self.repository, nil, self.script_version, nil)[0] rescue nil
88 self.supplied_script_version = self.script_version if self.supplied_script_version.nil? or self.supplied_script_version.empty?
89 self.script_version = sha1
91 self.errors.add :script_version, "#{self.script_version} does not resolve to a commit"
97 def ensure_unique_submit_id
99 if Job.where('submit_id=?',self.submit_id).first
100 raise SubmitIdReused.new
106 def find_docker_image_locator
107 # Find the Collection that holds the Docker image specified in the
108 # runtime constraints, and store its locator in docker_image_locator.
109 unless runtime_constraints.is_a? Hash
110 # We're still in validation stage, so we can't assume
111 # runtime_constraints isn't something horrible like an array or
112 # a string. Treat those cases as "no docker image supplied";
113 # other validations will fail anyway.
114 self.docker_image_locator = nil
117 image_search = runtime_constraints['docker_image']
118 image_tag = runtime_constraints['docker_image_tag']
120 self.docker_image_locator = nil
122 elsif coll = Collection.for_latest_docker_image(image_search, image_tag)
123 self.docker_image_locator = coll.portable_data_hash
126 errors.add(:docker_image_locator, "not found for #{image_search}")
133 queue = self.script_parameters.values
134 while not queue.empty?
135 queue = queue.flatten.compact.collect do |v|
139 v.match(/^(([0-9a-f]{32})\b(\+[^,]+)?,?)*$/) do |locator|
140 deps[locator.to_s] = true
149 def permission_to_update
150 if is_locked_by_uuid_was and !(current_user and
151 (current_user.uuid == is_locked_by_uuid_was or
152 current_user.uuid == system_user.uuid))
153 if script_changed? or
154 script_parameters_changed? or
155 script_version_changed? or
156 (!cancelled_at_was.nil? and
157 (cancelled_by_client_uuid_changed? or
158 cancelled_by_user_uuid_changed? or
159 cancelled_at_changed?)) or
160 started_at_changed? or
161 finished_at_changed? or
166 tasks_summary_changed?
167 logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
171 if !is_locked_by_uuid_changed?
175 logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
177 elsif is_locked_by_uuid_was and is_locked_by_uuid_was != current_user.uuid
178 logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_uuid_was}"
180 elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
181 logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"
189 def update_modified_by_fields
190 if self.cancelled_at_changed?
191 # Ensure cancelled_at cannot be set to arbitrary non-now times,
192 # or changed once it is set.
193 if self.cancelled_at and not self.cancelled_at_was
194 self.cancelled_at = Time.now
195 self.cancelled_by_user_uuid = current_user.uuid
196 self.cancelled_by_client_uuid = current_api_client.andand.uuid
197 @need_crunch_dispatch_trigger = true
199 self.cancelled_at = self.cancelled_at_was
200 self.cancelled_by_user_uuid = self.cancelled_by_user_uuid_was
201 self.cancelled_by_client_uuid = self.cancelled_by_client_uuid_was
207 def trigger_crunch_dispatch_if_cancelled
208 if @need_crunch_dispatch_trigger
209 File.open(Rails.configuration.crunch_refresh_trigger, 'wb') do
210 # That's all, just create/touch a file for crunch-job to see.