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 :superuser, :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
39 update_attributes(finished_at: finished_at || Time.now,
40 success: success.nil? ? false : success,
45 self.where('started_at is ? and is_locked_by_uuid is ? and cancelled_at is ?',
47 order('priority desc, created_at')
52 def ensure_script_version_is_commit
53 if self.is_locked_by_uuid and self.started_at
54 # Apparently client has already decided to go for it. This is
55 # needed to run a local job using a local working directory
56 # instead of a commit-ish.
59 sha1 = Commit.find_by_commit_ish(self.script_version) rescue nil
61 self.script_version = sha1
63 raise ArgumentError.new("Specified script_version does not resolve to a commit")
67 def ensure_unique_submit_id
69 if Job.where('submit_id=?',self.submit_id).first
70 raise SubmitIdReused.new
78 queue = self.script_parameters.values
79 while not queue.empty?
80 queue = queue.flatten.compact.collect do |v|
84 v.match(/^(([0-9a-f]{32})\b(\+[^,]+)?,?)*$/) do |locator|
85 deps[locator.to_s] = true
94 def permission_to_update
95 if is_locked_by_uuid_was and !(current_user and
96 current_user.uuid == is_locked_by_uuid_was)
98 script_parameters_changed? or
99 script_version_changed? or
100 cancelled_by_client_changed? or
101 cancelled_by_user_changed? or
102 cancelled_at_changed? or
103 started_at_changed? or
104 finished_at_changed? or
109 tasks_summary_changed?
110 logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
114 if !is_locked_by_uuid_changed?
118 logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
120 elsif is_locked_by_uuid_was and is_locked_by_uuid_was != current_user.uuid
121 logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_uuid_was}"
123 elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
124 logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"