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
32 t.add :is_locked_by_uuid
34 t.add :runtime_constraints
37 t.add :log_stream_href
39 t.add :nondeterministic
43 update_attributes(finished_at: finished_at || Time.now,
44 success: success.nil? ? false : success,
49 unless self.finished_at
50 "#{current_api_base}/#{self.class.to_s.pluralize.underscore}/#{self.uuid}/log_tail_follow"
55 self.where('started_at is ? and is_locked_by_uuid is ? and cancelled_at is ? and success is ?',
57 order('priority desc, created_at')
61 self.where('running = ?', true).
62 order('priority desc, created_at')
67 def foreign_key_attributes
68 super + %w(output log)
71 def ensure_script_version_is_commit
72 if self.is_locked_by_uuid and self.started_at
73 # Apparently client has already decided to go for it. This is
74 # needed to run a local job using a local working directory
75 # instead of a commit-ish.
78 if new_record? or script_version_changed?
79 sha1 = Commit.find_commit_range(current_user, nil, nil, self.script_version)[0] rescue nil
81 self.script_version = sha1
83 raise ArgumentError.new("Specified script_version does not resolve to a commit")
88 def ensure_unique_submit_id
90 if Job.where('submit_id=?',self.submit_id).first
91 raise SubmitIdReused.new
99 queue = self.script_parameters.values
100 while not queue.empty?
101 queue = queue.flatten.compact.collect do |v|
105 v.match(/^(([0-9a-f]{32})\b(\+[^,]+)?,?)*$/) do |locator|
106 deps[locator.to_s] = true
115 def permission_to_update
116 if is_locked_by_uuid_was and !(current_user and
117 (current_user.uuid == is_locked_by_uuid_was or
118 current_user.uuid == system_user.uuid))
119 if script_changed? or
120 script_parameters_changed? or
121 script_version_changed? or
122 (!cancelled_at_was.nil? and
123 (cancelled_by_client_uuid_changed? or
124 cancelled_by_user_uuid_changed? or
125 cancelled_at_changed?)) or
126 started_at_changed? or
127 finished_at_changed? or
132 tasks_summary_changed?
133 logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
137 if !is_locked_by_uuid_changed?
141 logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
143 elsif is_locked_by_uuid_was and is_locked_by_uuid_was != current_user.uuid
144 logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_uuid_was}"
146 elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
147 logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"
155 def update_modified_by_fields
156 if self.cancelled_at_changed?
157 # Ensure cancelled_at cannot be set to arbitrary non-now times,
158 # or changed once it is set.
159 if self.cancelled_at and not self.cancelled_at_was
160 self.cancelled_at = Time.now
161 self.cancelled_by_user_uuid = current_user.uuid
162 self.cancelled_by_client_uuid = current_api_client.uuid
163 @need_crunch_dispatch_trigger = true
165 self.cancelled_at = self.cancelled_at_was
166 self.cancelled_by_user_uuid = self.cancelled_by_user_uuid_was
167 self.cancelled_by_client_uuid = self.cancelled_by_client_uuid_was
173 def trigger_crunch_dispatch_if_cancelled
174 if @need_crunch_dispatch_trigger
175 File.open(Rails.configuration.crunch_refresh_trigger, 'wb') do
176 # That's all, just create/touch a file for crunch-job to see.
183 @@redis ||= Redis.new(:timeout => 0)
184 if @@redis.exists uuid
185 @@redis.getrange(uuid, 0 - 2**10, -1)
187 rescue Redis::CannotConnectError
188 return '(not available)'