show provenance
[arvados.git] / services / api / app / models / job.rb
1 class Job < ArvadosModel
2   include AssignUuid
3   include KindAndEtag
4   include CommonApiTemplate
5   serialize :script_parameters, Hash
6   serialize :resource_limits, 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
12   has_many :commit_ancestors, :foreign_key => :descendant, :primary_key => :script_version
13
14   class SubmitIdReused < StandardError
15   end
16
17   api_accessible :superuser, :extend => :common do |t|
18     t.add :submit_id
19     t.add :priority
20     t.add :script
21     t.add :script_parameters
22     t.add :script_version
23     t.add :cancelled_at
24     t.add :cancelled_by_client_uuid
25     t.add :cancelled_by_user_uuid
26     t.add :started_at
27     t.add :finished_at
28     t.add :output
29     t.add :success
30     t.add :running
31     t.add :is_locked_by_uuid
32     t.add :log
33     t.add :resource_limits
34     t.add :tasks_summary
35     t.add :dependencies
36   end
37
38   def assert_finished
39     update_attributes(finished_at: finished_at || Time.now,
40                       success: success.nil? ? false : success,
41                       running: false)
42   end
43
44   def self.queue
45     self.where('started_at is ? and is_locked_by_uuid is ? and cancelled_at is ?',
46                nil, nil, nil).
47       order('priority desc, created_at')
48   end
49
50   protected
51
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.
57       return true
58     end
59     sha1 = Commit.find_by_commit_ish(self.script_version) rescue nil
60     if sha1
61       self.script_version = sha1
62     else
63       raise ArgumentError.new("Specified script_version does not resolve to a commit")
64     end
65   end
66
67   def ensure_unique_submit_id
68     if !submit_id.nil?
69       if Job.where('submit_id=?',self.submit_id).first
70         raise SubmitIdReused.new
71       end
72     end
73     true
74   end
75
76   def dependencies
77     deps = {}
78     self.script_parameters.values.each do |v|
79       next unless v.is_a? String
80       v.match(/^(([0-9a-f]{32})\b(\+[^,]+)?,?)*$/) do |locator|
81         deps[locator] = true
82       end
83     end
84     deps.keys
85   end
86
87   def permission_to_update
88     if is_locked_by_was and !(current_user and
89                               current_user.uuid == is_locked_by_was)
90       if script_changed? or
91           script_parameters_changed? or
92           script_version_changed? or
93           cancelled_by_client_changed? or
94           cancelled_by_user_changed? or
95           cancelled_at_changed? or
96           started_at_changed? or
97           finished_at_changed? or
98           running_changed? or
99           success_changed? or
100           output_changed? or
101           log_changed? or
102           tasks_summary_changed?
103         logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
104         return false
105       end
106     end
107     if !is_locked_by_changed?
108       super
109     else
110       if !current_user
111         logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
112         false
113       elsif is_locked_by_was and is_locked_by_was != current_user.uuid
114         logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_was}"
115         false
116       elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
117         logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"
118         false
119       else
120         super
121       end
122     end
123   end
124 end