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