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