1 class Job < ArvadosModel
4 include CommonApiTemplate
5 serialize :script_parameters, Hash
6 serialize :runtime_constraints, Hash
7 serialize :tasks_summary, Hash
8 before_create :ensure_unique_submit_id
9 before_create :ensure_script_version_is_commit
10 before_update :ensure_script_version_is_commit
11 after_commit :trigger_crunch_dispatch_if_cancelled, :on => :update
13 has_many :commit_ancestors, :foreign_key => :descendant, :primary_key => :script_version
15 class SubmitIdReused < StandardError
18 api_accessible :user, extend: :common do |t|
22 t.add :script_parameters
25 t.add :cancelled_by_client_uuid
26 t.add :cancelled_by_user_uuid
30 t.add :output_is_persistent
33 t.add :is_locked_by_uuid
35 t.add :runtime_constraints
38 t.add :log_stream_href
40 t.add :nondeterministic
45 update_attributes(finished_at: finished_at || Time.now,
46 success: success.nil? ? false : success,
51 unless self.finished_at
52 "#{current_api_base}/#{self.class.to_s.pluralize.underscore}/#{self.uuid}/log_tail_follow"
57 self.where('started_at is ? and is_locked_by_uuid is ? and cancelled_at is ? and success is ?',
59 order('priority desc, created_at')
63 self.where('running = ?', true).
64 order('priority desc, created_at')
69 def foreign_key_attributes
70 super + %w(output log)
73 def skip_uuid_read_permission_check
74 super + %w(cancelled_by_client_uuid)
77 def ensure_script_version_is_commit
78 if self.is_locked_by_uuid and self.started_at
79 # Apparently client has already decided to go for it. This is
80 # needed to run a local job using a local working directory
81 # instead of a commit-ish.
84 if new_record? or script_version_changed?
85 sha1 = Commit.find_commit_range(current_user, nil, nil, self.script_version, nil)[0] rescue nil
87 self.script_version = sha1
89 raise ArgumentError.new("Specified script_version does not resolve to a commit")
94 def ensure_unique_submit_id
96 if Job.where('submit_id=?',self.submit_id).first
97 raise SubmitIdReused.new
105 queue = self.script_parameters.values
106 while not queue.empty?
107 queue = queue.flatten.compact.collect do |v|
111 v.match(/^(([0-9a-f]{32})\b(\+[^,]+)?,?)*$/) do |locator|
112 deps[locator.to_s] = true
121 def permission_to_update
122 if is_locked_by_uuid_was and !(current_user and
123 (current_user.uuid == is_locked_by_uuid_was or
124 current_user.uuid == system_user.uuid))
125 if script_changed? or
126 script_parameters_changed? or
127 script_version_changed? or
128 (!cancelled_at_was.nil? and
129 (cancelled_by_client_uuid_changed? or
130 cancelled_by_user_uuid_changed? or
131 cancelled_at_changed?)) or
132 started_at_changed? or
133 finished_at_changed? or
138 tasks_summary_changed?
139 logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
143 if !is_locked_by_uuid_changed?
147 logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
149 elsif is_locked_by_uuid_was and is_locked_by_uuid_was != current_user.uuid
150 logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_uuid_was}"
152 elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
153 logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"
161 def update_modified_by_fields
162 if self.cancelled_at_changed?
163 # Ensure cancelled_at cannot be set to arbitrary non-now times,
164 # or changed once it is set.
165 if self.cancelled_at and not self.cancelled_at_was
166 self.cancelled_at = Time.now
167 self.cancelled_by_user_uuid = current_user.uuid
168 self.cancelled_by_client_uuid = current_api_client.uuid
169 @need_crunch_dispatch_trigger = true
171 self.cancelled_at = self.cancelled_at_was
172 self.cancelled_by_user_uuid = self.cancelled_by_user_uuid_was
173 self.cancelled_by_client_uuid = self.cancelled_by_client_uuid_was
179 def trigger_crunch_dispatch_if_cancelled
180 if @need_crunch_dispatch_trigger
181 File.open(Rails.configuration.crunch_refresh_trigger, 'wb') do
182 # That's all, just create/touch a file for crunch-job to see.
189 @@redis ||= Redis.new(:timeout => 0)
190 if @@redis.exists uuid
191 @@redis.getrange(uuid, 0 - 2**10, -1)
193 rescue Redis::CannotConnectError
194 return '(not available)'