3550: Merge branch 'master' into 3550-local-pipeline
[arvados.git] / services / api / app / models / job.rb
1 class Job < ArvadosModel
2   include HasUuid
3   include KindAndEtag
4   include CommonApiTemplate
5   attr_protected :docker_image_locator
6   serialize :script_parameters, Hash
7   serialize :runtime_constraints, Hash
8   serialize :tasks_summary, Hash
9   before_create :ensure_unique_submit_id
10   after_commit :trigger_crunch_dispatch_if_cancelled, :on => :update
11   before_validation :set_priority
12   validate :ensure_script_version_is_commit
13   validate :find_docker_image_locator
14
15   has_many :commit_ancestors, :foreign_key => :descendant, :primary_key => :script_version
16
17   class SubmitIdReused < StandardError
18   end
19
20   api_accessible :user, extend: :common do |t|
21     t.add :submit_id
22     t.add :priority
23     t.add :script
24     t.add :script_parameters
25     t.add :script_version
26     t.add :cancelled_at
27     t.add :cancelled_by_client_uuid
28     t.add :cancelled_by_user_uuid
29     t.add :started_at
30     t.add :finished_at
31     t.add :output
32     t.add :success
33     t.add :running
34     t.add :is_locked_by_uuid
35     t.add :log
36     t.add :runtime_constraints
37     t.add :tasks_summary
38     t.add :dependencies
39     t.add :nondeterministic
40     t.add :repository
41     t.add :supplied_script_version
42     t.add :docker_image_locator
43   end
44
45   def assert_finished
46     update_attributes(finished_at: finished_at || Time.now,
47                       success: success.nil? ? false : success,
48                       running: false)
49   end
50
51   def self.queue
52     self.where('started_at is ? and is_locked_by_uuid is ? and cancelled_at is ? and success is ?',
53                nil, nil, nil, nil).
54       order('priority desc, created_at')
55   end
56
57   def self.running
58     self.where('running = ?', true).
59       order('priority desc, created_at')
60   end
61
62   protected
63
64   def foreign_key_attributes
65     super + %w(output log)
66   end
67
68   def skip_uuid_read_permission_check
69     super + %w(cancelled_by_client_uuid)
70   end
71
72   def skip_uuid_existence_check
73     super + %w(output log)
74   end
75
76   def set_priority
77     if self.priority.nil?
78       self.priority = 0
79     end
80     true
81   end
82
83   def ensure_script_version_is_commit
84     if self.is_locked_by_uuid and self.started_at
85       # Apparently client has already decided to go for it. This is
86       # needed to run a local job using a local working directory
87       # instead of a commit-ish.
88       return true
89     end
90     if new_record? or script_version_changed?
91       sha1 = Commit.find_commit_range(current_user, self.repository, nil, self.script_version, nil)[0] rescue nil
92       if sha1
93         self.supplied_script_version = self.script_version if self.supplied_script_version.nil? or self.supplied_script_version.empty?
94         self.script_version = sha1
95       else
96         self.errors.add :script_version, "#{self.script_version} does not resolve to a commit"
97         return false
98       end
99     end
100   end
101
102   def ensure_unique_submit_id
103     if !submit_id.nil?
104       if Job.where('submit_id=?',self.submit_id).first
105         raise SubmitIdReused.new
106       end
107     end
108     true
109   end
110
111   def find_docker_image_locator
112     # Find the Collection that holds the Docker image specified in the
113     # runtime constraints, and store its locator in docker_image_locator.
114     unless runtime_constraints.is_a? Hash
115       # We're still in validation stage, so we can't assume
116       # runtime_constraints isn't something horrible like an array or
117       # a string. Treat those cases as "no docker image supplied";
118       # other validations will fail anyway.
119       self.docker_image_locator = nil
120       return true
121     end
122     image_search = runtime_constraints['docker_image']
123     image_tag = runtime_constraints['docker_image_tag']
124     if image_search.nil?
125       self.docker_image_locator = nil
126       true
127     elsif coll = Collection.for_latest_docker_image(image_search, image_tag)
128       self.docker_image_locator = coll.portable_data_hash
129       true
130     else
131       errors.add(:docker_image_locator, "not found for #{image_search}")
132       false
133     end
134   end
135
136   def dependencies
137     deps = {}
138     queue = self.script_parameters.values
139     while not queue.empty?
140       queue = queue.flatten.compact.collect do |v|
141         if v.is_a? Hash
142           v.values
143         elsif v.is_a? String
144           v.match(/^(([0-9a-f]{32})\b(\+[^,]+)?,?)*$/) do |locator|
145             deps[locator.to_s] = true
146           end
147           nil
148         end
149       end
150     end
151     deps.keys
152   end
153
154   def permission_to_update
155     if is_locked_by_uuid_was and !(current_user and
156                                    (current_user.uuid == is_locked_by_uuid_was or
157                                     current_user.uuid == system_user.uuid))
158       if script_changed? or
159           script_parameters_changed? or
160           script_version_changed? or
161           (!cancelled_at_was.nil? and
162            (cancelled_by_client_uuid_changed? or
163             cancelled_by_user_uuid_changed? or
164             cancelled_at_changed?)) or
165           started_at_changed? or
166           finished_at_changed? or
167           running_changed? or
168           success_changed? or
169           output_changed? or
170           log_changed? or
171           tasks_summary_changed?
172         logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
173         return false
174       end
175     end
176     if !is_locked_by_uuid_changed?
177       super
178     else
179       if !current_user
180         logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
181         false
182       elsif is_locked_by_uuid_was and is_locked_by_uuid_was != current_user.uuid
183         logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_uuid_was}"
184         false
185       elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
186         logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"
187         false
188       else
189         super
190       end
191     end
192   end
193
194   def update_modified_by_fields
195     if self.cancelled_at_changed?
196       # Ensure cancelled_at cannot be set to arbitrary non-now times,
197       # or changed once it is set.
198       if self.cancelled_at and not self.cancelled_at_was
199         self.cancelled_at = Time.now
200         self.cancelled_by_user_uuid = current_user.uuid
201         self.cancelled_by_client_uuid = current_api_client.andand.uuid
202         @need_crunch_dispatch_trigger = true
203       else
204         self.cancelled_at = self.cancelled_at_was
205         self.cancelled_by_user_uuid = self.cancelled_by_user_uuid_was
206         self.cancelled_by_client_uuid = self.cancelled_by_client_uuid_was
207       end
208     end
209     super
210   end
211
212   def trigger_crunch_dispatch_if_cancelled
213     if @need_crunch_dispatch_trigger
214       File.open(Rails.configuration.crunch_refresh_trigger, 'wb') do
215         # That's all, just create/touch a file for crunch-job to see.
216       end
217     end
218   end
219 end