Merge remote-tracking branch 'origin/master' into 2882-job-process-stats
[arvados.git] / services / api / app / models / job.rb
1 class Job < ArvadosModel
2   include HasUuid
3   include KindAndEtag
4   include CommonApiTemplate
5   serialize :script_parameters, Hash
6   serialize :runtime_constraints, 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   after_commit :trigger_crunch_dispatch_if_cancelled, :on => :update
12
13   has_many :commit_ancestors, :foreign_key => :descendant, :primary_key => :script_version
14
15   class SubmitIdReused < StandardError
16   end
17
18   api_accessible :user, extend: :common do |t|
19     t.add :submit_id
20     t.add :priority
21     t.add :script
22     t.add :script_parameters
23     t.add :script_version
24     t.add :cancelled_at
25     t.add :cancelled_by_client_uuid
26     t.add :cancelled_by_user_uuid
27     t.add :started_at
28     t.add :finished_at
29     t.add :output
30     t.add :output_is_persistent
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   end
42
43   def assert_finished
44     update_attributes(finished_at: finished_at || Time.now,
45                       success: success.nil? ? false : success,
46                       running: false)
47   end
48
49   def self.queue
50     self.where('started_at is ? and is_locked_by_uuid is ? and cancelled_at is ? and success is ?',
51                nil, nil, nil, nil).
52       order('priority desc, created_at')
53   end
54
55   def self.running
56     self.where('running = ?', true).
57       order('priority desc, created_at')
58   end
59
60   protected
61
62   def foreign_key_attributes
63     super + %w(output log)
64   end
65
66   def skip_uuid_read_permission_check
67     super + %w(cancelled_by_client_uuid)
68   end
69
70   def skip_uuid_existence_check
71     super + %w(output log)
72   end
73
74   def ensure_script_version_is_commit
75     if self.is_locked_by_uuid and self.started_at
76       # Apparently client has already decided to go for it. This is
77       # needed to run a local job using a local working directory
78       # instead of a commit-ish.
79       return true
80     end
81     if new_record? or script_version_changed?
82       sha1 = Commit.find_commit_range(current_user, self.repository, nil, self.script_version, nil)[0] rescue nil
83       if sha1
84         self.supplied_script_version = self.script_version if self.supplied_script_version.nil? or self.supplied_script_version.empty?
85         self.script_version = sha1
86       else
87         raise ArgumentError.new("Specified script_version does not resolve to a commit")
88       end
89     end
90   end
91
92   def ensure_unique_submit_id
93     if !submit_id.nil?
94       if Job.where('submit_id=?',self.submit_id).first
95         raise SubmitIdReused.new
96       end
97     end
98     true
99   end
100
101   def dependencies
102     deps = {}
103     queue = self.script_parameters.values
104     while not queue.empty?
105       queue = queue.flatten.compact.collect do |v|
106         if v.is_a? Hash
107           v.values
108         elsif v.is_a? String
109           v.match(/^(([0-9a-f]{32})\b(\+[^,]+)?,?)*$/) do |locator|
110             deps[locator.to_s] = true
111           end
112           nil
113         end
114       end
115     end
116     deps.keys
117   end
118
119   def permission_to_update
120     if is_locked_by_uuid_was and !(current_user and
121                                    (current_user.uuid == is_locked_by_uuid_was or
122                                     current_user.uuid == system_user.uuid))
123       if script_changed? or
124           script_parameters_changed? or
125           script_version_changed? or
126           (!cancelled_at_was.nil? and
127            (cancelled_by_client_uuid_changed? or
128             cancelled_by_user_uuid_changed? or
129             cancelled_at_changed?)) or
130           started_at_changed? or
131           finished_at_changed? or
132           running_changed? or
133           success_changed? or
134           output_changed? or
135           log_changed? or
136           tasks_summary_changed?
137         logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
138         return false
139       end
140     end
141     if !is_locked_by_uuid_changed?
142       super
143     else
144       if !current_user
145         logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
146         false
147       elsif is_locked_by_uuid_was and is_locked_by_uuid_was != current_user.uuid
148         logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_uuid_was}"
149         false
150       elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
151         logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"
152         false
153       else
154         super
155       end
156     end
157   end
158
159   def update_modified_by_fields
160     if self.cancelled_at_changed?
161       # Ensure cancelled_at cannot be set to arbitrary non-now times,
162       # or changed once it is set.
163       if self.cancelled_at and not self.cancelled_at_was
164         self.cancelled_at = Time.now
165         self.cancelled_by_user_uuid = current_user.uuid
166         self.cancelled_by_client_uuid = current_api_client.uuid
167         @need_crunch_dispatch_trigger = true
168       else
169         self.cancelled_at = self.cancelled_at_was
170         self.cancelled_by_user_uuid = self.cancelled_by_user_uuid_was
171         self.cancelled_by_client_uuid = self.cancelled_by_client_uuid_was
172       end
173     end
174     super
175   end
176
177   def trigger_crunch_dispatch_if_cancelled
178     if @need_crunch_dispatch_trigger
179       File.open(Rails.configuration.crunch_refresh_trigger, 'wb') do
180         # That's all, just create/touch a file for crunch-job to see.
181       end
182     end
183   end
184 end