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
12 has_many :commit_ancestors, :foreign_key => :descendant, :primary_key => :script_version
14 class SubmitIdReused < StandardError
17 api_accessible :user, extend: :common do |t|
21 t.add :script_parameters
24 t.add :cancelled_by_client_uuid
25 t.add :cancelled_by_user_uuid
31 t.add :is_locked_by_uuid
33 t.add :runtime_constraints
36 t.add :log_stream_href
40 update_attributes(finished_at: finished_at || Time.now,
41 success: success.nil? ? false : success,
46 unless self.finished_at
47 "#{current_api_base}/#{self.class.to_s.pluralize.underscore}/#{self.uuid}/log_tail_follow"
52 self.where('started_at is ? and is_locked_by_uuid is ? and cancelled_at is ?',
54 order('priority desc, created_at')
59 def ensure_script_version_is_commit
60 if self.is_locked_by_uuid and self.started_at
61 # Apparently client has already decided to go for it. This is
62 # needed to run a local job using a local working directory
63 # instead of a commit-ish.
66 sha1 = Commit.find_by_commit_ish(self.script_version) rescue nil
68 self.script_version = sha1
70 raise ArgumentError.new("Specified script_version does not resolve to a commit")
74 def ensure_unique_submit_id
76 if Job.where('submit_id=?',self.submit_id).first
77 raise SubmitIdReused.new
85 queue = self.script_parameters.values
86 while not queue.empty?
87 queue = queue.flatten.compact.collect do |v|
91 v.match(/^(([0-9a-f]{32})\b(\+[^,]+)?,?)*$/) do |locator|
92 deps[locator.to_s] = true
101 def permission_to_update
102 if is_locked_by_uuid_was and !(current_user and
103 current_user.uuid == is_locked_by_uuid_was)
104 if script_changed? or
105 script_parameters_changed? or
106 script_version_changed? or
107 cancelled_by_client_changed? or
108 cancelled_by_user_changed? or
109 cancelled_at_changed? or
110 started_at_changed? or
111 finished_at_changed? or
116 tasks_summary_changed?
117 logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
121 if !is_locked_by_uuid_changed?
125 logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
127 elsif is_locked_by_uuid_was and is_locked_by_uuid_was != current_user.uuid
128 logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_uuid_was}"
130 elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
131 logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"