Allow users to cancel a running crunch job by updating cancelled_at
[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     sha1 = Commit.find_by_commit_ish(self.script_version) rescue nil
68     if sha1
69       self.script_version = sha1
70     else
71       raise ArgumentError.new("Specified script_version does not resolve to a commit")
72     end
73   end
74
75   def ensure_unique_submit_id
76     if !submit_id.nil?
77       if Job.where('submit_id=?',self.submit_id).first
78         raise SubmitIdReused.new
79       end
80     end
81     true
82   end
83
84   def dependencies
85     deps = {}
86     queue = self.script_parameters.values
87     while not queue.empty?
88       queue = queue.flatten.compact.collect do |v|
89         if v.is_a? Hash
90           v.values
91         elsif v.is_a? String
92           v.match(/^(([0-9a-f]{32})\b(\+[^,]+)?,?)*$/) do |locator|
93             deps[locator.to_s] = true
94           end
95           nil
96         end
97       end
98     end
99     deps.keys
100   end
101
102   def permission_to_update
103     if is_locked_by_uuid_was and !(current_user and
104                                    current_user.uuid == is_locked_by_uuid_was)
105       if script_changed? or
106           script_parameters_changed? or
107           script_version_changed? or
108           (!cancelled_at_was.nil? and
109            (cancelled_by_client_changed? or
110             cancelled_by_user_changed? or
111             cancelled_at_changed?)) or
112           started_at_changed? or
113           finished_at_changed? or
114           running_changed? or
115           success_changed? or
116           output_changed? or
117           log_changed? or
118           tasks_summary_changed?
119         logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
120         return false
121       end
122     end
123     if !is_locked_by_uuid_changed?
124       super
125     else
126       if !current_user
127         logger.warn "Anonymous user tried to change lock on #{self.class.to_s} #{uuid_was}"
128         false
129       elsif is_locked_by_uuid_was and is_locked_by_uuid_was != current_user.uuid
130         logger.warn "User #{current_user.uuid} tried to steal lock on #{self.class.to_s} #{uuid_was} from #{is_locked_by_uuid_was}"
131         false
132       elsif !is_locked_by_uuid.nil? and is_locked_by_uuid != current_user.uuid
133         logger.warn "User #{current_user.uuid} tried to lock #{self.class.to_s} #{uuid_was} with uuid #{is_locked_by_uuid}"
134         false
135       else
136         super
137       end
138     end
139   end
140
141   def update_modified_by_fields
142     if self.cancelled_at_changed?
143       # Ensure cancelled_at cannot be set to arbitrary non-now times,
144       # or changed once it is set.
145       if self.cancelled_at and not self.cancelled_at_was
146         self.cancelled_at = Time.now
147         self.cancelled_by_user_uuid = current_user.uuid
148         self.cancelled_by_client_uuid = current_api_client.uuid
149         @need_crunch_dispatch_trigger = true
150       else
151         self.cancelled_at = self.cancelled_at_was
152         self.cancelled_by_user_uuid = self.cancelled_by_user_uuid_was
153         self.cancelled_by_client_uuid = self.cancelled_by_client_uuid_was
154       end
155     end
156     super
157   end
158
159   def trigger_crunch_dispatch_if_cancelled
160     if @need_crunch_dispatch_trigger
161       File.open(Rails.configuration.crunch_dispatch_hup_trigger, 'wb') do
162         # That's all, just create a file for crunch-dispatch to see.
163       end
164     end
165   end
166
167 end