:none dispatcher only runs one job at a time.
[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_validation :find_docker_image_locator
10   before_create :ensure_unique_submit_id
11   before_create :ensure_script_version_is_commit
12   before_update :ensure_script_version_is_commit
13   after_commit :trigger_crunch_dispatch_if_cancelled, :on => :update
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 :output_is_persistent
33     t.add :success
34     t.add :running
35     t.add :is_locked_by_uuid
36     t.add :log
37     t.add :runtime_constraints
38     t.add :tasks_summary
39     t.add :dependencies
40     t.add :nondeterministic
41     t.add :repository
42     t.add :supplied_script_version
43     t.add :docker_image_locator
44   end
45
46   def assert_finished
47     update_attributes(finished_at: finished_at || Time.now,
48                       success: success.nil? ? false : success,
49                       running: false)
50   end
51
52   def self.queue
53     self.where('started_at is ? and is_locked_by_uuid is ? and cancelled_at is ? and success is ?',
54                nil, nil, nil, nil).
55       order('priority desc, created_at')
56   end
57
58   def self.running
59     self.where('running = ?', true).
60       order('priority desc, created_at')
61   end
62
63   protected
64
65   def foreign_key_attributes
66     super + %w(output log)
67   end
68
69   def skip_uuid_read_permission_check
70     super + %w(cancelled_by_client_uuid)
71   end
72
73   def skip_uuid_existence_check
74     super + %w(output log)
75   end
76
77   def ensure_script_version_is_commit
78     if self.is_locked_by_uuid and self.started_at
79       # Apparently client has already decided to go for it. This is
80       # needed to run a local job using a local working directory
81       # instead of a commit-ish.
82       return true
83     end
84     if new_record? or script_version_changed?
85       sha1 = Commit.find_commit_range(current_user, self.repository, nil, self.script_version, nil)[0] rescue nil
86       if sha1
87         self.supplied_script_version = self.script_version if self.supplied_script_version.nil? or self.supplied_script_version.empty?
88         self.script_version = sha1
89       else
90         raise ArgumentError.new("Specified script_version does not resolve to a commit")
91       end
92     end
93   end
94
95   def ensure_unique_submit_id
96     if !submit_id.nil?
97       if Job.where('submit_id=?',self.submit_id).first
98         raise SubmitIdReused.new
99       end
100     end
101     true
102   end
103
104   def find_docker_image_locator
105     # Find the Collection that holds the Docker image specified in the
106     # runtime constraints, and store its locator in docker_image_locator.
107     image_search = runtime_constraints['docker_image']
108     image_tag = runtime_constraints['docker_image_tag']
109     if image_search.nil?
110       self.docker_image_locator = nil
111     elsif coll = Collection.for_latest_docker_image(image_search, image_tag)
112       self.docker_image_locator = coll.uuid
113     else
114       errors.add(:docker_image_locator, "not found for #{image_search}")
115       false
116     end
117   end
118
119   def dependencies
120     deps = {}
121     queue = self.script_parameters.values
122     while not queue.empty?
123       queue = queue.flatten.compact.collect do |v|
124         if v.is_a? Hash
125           v.values
126         elsif v.is_a? String
127           v.match(/^(([0-9a-f]{32})\b(\+[^,]+)?,?)*$/) do |locator|
128             deps[locator.to_s] = true
129           end
130           nil
131         end
132       end
133     end
134     deps.keys
135   end
136
137   def permission_to_update
138     if is_locked_by_uuid_was and !(current_user and
139                                    (current_user.uuid == is_locked_by_uuid_was or
140                                     current_user.uuid == system_user.uuid))
141       if script_changed? or
142           script_parameters_changed? or
143           script_version_changed? or
144           (!cancelled_at_was.nil? and
145            (cancelled_by_client_uuid_changed? or
146             cancelled_by_user_uuid_changed? or
147             cancelled_at_changed?)) or
148           started_at_changed? or
149           finished_at_changed? or
150           running_changed? or
151           success_changed? or
152           output_changed? or
153           log_changed? or
154           tasks_summary_changed?
155         logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
156         return false
157       end
158     end
159     if !is_locked_by_uuid_changed?
160       super
161     else
162       if !current_user
163         logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
164         false
165       elsif is_locked_by_uuid_was and is_locked_by_uuid_was != current_user.uuid
166         logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_uuid_was}"
167         false
168       elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
169         logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"
170         false
171       else
172         super
173       end
174     end
175   end
176
177   def update_modified_by_fields
178     if self.cancelled_at_changed?
179       # Ensure cancelled_at cannot be set to arbitrary non-now times,
180       # or changed once it is set.
181       if self.cancelled_at and not self.cancelled_at_was
182         self.cancelled_at = Time.now
183         self.cancelled_by_user_uuid = current_user.uuid
184         self.cancelled_by_client_uuid = current_api_client.uuid
185         @need_crunch_dispatch_trigger = true
186       else
187         self.cancelled_at = self.cancelled_at_was
188         self.cancelled_by_user_uuid = self.cancelled_by_user_uuid_was
189         self.cancelled_by_client_uuid = self.cancelled_by_client_uuid_was
190       end
191     end
192     super
193   end
194
195   def trigger_crunch_dispatch_if_cancelled
196     if @need_crunch_dispatch_trigger
197       File.open(Rails.configuration.crunch_refresh_trigger, 'wb') do
198         # That's all, just create/touch a file for crunch-job to see.
199       end
200     end
201   end
202 end