Merge branch 'master' into 1978-compare-pipelines
[arvados.git] / services / api / app / models / job.rb
1 class Job < ArvadosModel
2   include AssignUuid
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 :success
31     t.add :running
32     t.add :is_locked_by_uuid
33     t.add :log
34     t.add :runtime_constraints
35     t.add :tasks_summary
36     t.add :dependencies
37     t.add :log_stream_href
38   end
39
40   def assert_finished
41     update_attributes(finished_at: finished_at || Time.now,
42                       success: success.nil? ? false : success,
43                       running: false)
44   end
45
46   def log_stream_href
47     unless self.finished_at
48       "#{current_api_base}/#{self.class.to_s.pluralize.underscore}/#{self.uuid}/log_tail_follow"
49     end
50   end
51
52   def self.queue
53     self.where('started_at is ? and is_locked_by_uuid is ? and cancelled_at is ?',
54                nil, nil, nil).
55       order('priority desc, created_at')
56   end
57
58   protected
59
60   def ensure_script_version_is_commit
61     if self.is_locked_by_uuid and self.started_at
62       # Apparently client has already decided to go for it. This is
63       # needed to run a local job using a local working directory
64       # instead of a commit-ish.
65       return true
66     end
67     if new_record? or script_version_changed?
68       sha1 = Commit.find_by_commit_ish(self.script_version) rescue nil
69       if sha1
70         self.script_version = sha1
71       else
72         raise ArgumentError.new("Specified script_version does not resolve to a commit")
73       end
74     end
75   end
76
77   def ensure_unique_submit_id
78     if !submit_id.nil?
79       if Job.where('submit_id=?',self.submit_id).first
80         raise SubmitIdReused.new
81       end
82     end
83     true
84   end
85
86   def dependencies
87     deps = {}
88     queue = self.script_parameters.values
89     while not queue.empty?
90       queue = queue.flatten.compact.collect do |v|
91         if v.is_a? Hash
92           v.values
93         elsif v.is_a? String
94           v.match(/^(([0-9a-f]{32})\b(\+[^,]+)?,?)*$/) do |locator|
95             deps[locator.to_s] = true
96           end
97           nil
98         end
99       end
100     end
101     deps.keys
102   end
103
104   def permission_to_update
105     if is_locked_by_uuid_was and !(current_user and
106                                    current_user.uuid == is_locked_by_uuid_was)
107       if script_changed? or
108           script_parameters_changed? or
109           script_version_changed? or
110           (!cancelled_at_was.nil? and
111            (cancelled_by_client_uuid_changed? or
112             cancelled_by_user_uuid_changed? or
113             cancelled_at_changed?)) or
114           started_at_changed? or
115           finished_at_changed? or
116           running_changed? or
117           success_changed? or
118           output_changed? or
119           log_changed? or
120           tasks_summary_changed?
121         logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
122         return false
123       end
124     end
125     if !is_locked_by_uuid_changed?
126       super
127     else
128       if !current_user
129         logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
130         false
131       elsif is_locked_by_uuid_was and is_locked_by_uuid_was != current_user.uuid
132         logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_uuid_was}"
133         false
134       elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
135         logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"
136         false
137       else
138         super
139       end
140     end
141   end
142
143   def update_modified_by_fields
144     if self.cancelled_at_changed?
145       # Ensure cancelled_at cannot be set to arbitrary non-now times,
146       # or changed once it is set.
147       if self.cancelled_at and not self.cancelled_at_was
148         self.cancelled_at = Time.now
149         self.cancelled_by_user_uuid = current_user.uuid
150         self.cancelled_by_client_uuid = current_api_client.uuid
151         @need_crunch_dispatch_trigger = true
152       else
153         self.cancelled_at = self.cancelled_at_was
154         self.cancelled_by_user_uuid = self.cancelled_by_user_uuid_was
155         self.cancelled_by_client_uuid = self.cancelled_by_client_uuid_was
156       end
157     end
158     super
159   end
160
161   def trigger_crunch_dispatch_if_cancelled
162     if @need_crunch_dispatch_trigger
163       File.open(Rails.configuration.crunch_refresh_trigger, 'wb') do
164         # That's all, just create/touch a file for crunch-job to see.
165       end
166     end
167   end
168
169 end