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
42 update_attributes(finished_at: finished_at || Time.now,
43 success: success.nil? ? false : success,
48 unless self.finished_at
49 "#{current_api_base}/#{self.class.to_s.pluralize.underscore}/#{self.uuid}/log_tail_follow"
54 self.where('started_at is ? and is_locked_by_uuid is ? and cancelled_at is ?',
56 order('priority desc, created_at')
61 def ensure_script_version_is_commit
62 if self.is_locked_by_uuid and self.started_at
63 # Apparently client has already decided to go for it. This is
64 # needed to run a local job using a local working directory
65 # instead of a commit-ish.
68 if new_record? or script_version_changed?
69 sha1 = Commit.find_by_commit_ish(self.script_version) rescue nil
71 self.script_version = sha1
73 raise ArgumentError.new("Specified script_version does not resolve to a commit")
78 def ensure_unique_submit_id
80 if Job.where('submit_id=?',self.submit_id).first
81 raise SubmitIdReused.new
89 queue = self.script_parameters.values
90 while not queue.empty?
91 queue = queue.flatten.compact.collect do |v|
95 v.match(/^(([0-9a-f]{32})\b(\+[^,]+)?,?)*$/) do |locator|
96 deps[locator.to_s] = true
105 def permission_to_update
106 if is_locked_by_uuid_was and !(current_user and
107 current_user.uuid == is_locked_by_uuid_was)
108 if script_changed? or
109 script_parameters_changed? or
110 script_version_changed? or
111 (!cancelled_at_was.nil? and
112 (cancelled_by_client_uuid_changed? or
113 cancelled_by_user_uuid_changed? or
114 cancelled_at_changed?)) or
115 started_at_changed? or
116 finished_at_changed? or
121 tasks_summary_changed?
122 logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
126 if !is_locked_by_uuid_changed?
130 logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
132 elsif is_locked_by_uuid_was and is_locked_by_uuid_was != current_user.uuid
133 logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_uuid_was}"
135 elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
136 logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"
144 def update_modified_by_fields
145 if self.cancelled_at_changed?
146 # Ensure cancelled_at cannot be set to arbitrary non-now times,
147 # or changed once it is set.
148 if self.cancelled_at and not self.cancelled_at_was
149 self.cancelled_at = Time.now
150 self.cancelled_by_user_uuid = current_user.uuid
151 self.cancelled_by_client_uuid = current_api_client.uuid
152 @need_crunch_dispatch_trigger = true
154 self.cancelled_at = self.cancelled_at_was
155 self.cancelled_by_user_uuid = self.cancelled_by_user_uuid_was
156 self.cancelled_by_client_uuid = self.cancelled_by_client_uuid_was
162 def trigger_crunch_dispatch_if_cancelled
163 if @need_crunch_dispatch_trigger
164 File.open(Rails.configuration.crunch_refresh_trigger, 'wb') do
165 # That's all, just create/touch a file for crunch-job to see.
172 @@redis ||= Redis.new(:timeout => 0)
173 if @@redis.exists uuid
174 @@redis.getrange(uuid, 0 - 2**10, -1)
176 rescue Redis::CannotConnectError
177 return '(not available)'