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