Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / apps / workbench / app / models / job.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class Job < ArvadosBase
6   def self.goes_in_projects?
7     true
8   end
9
10   def content_summary
11     "#{script} job"
12   end
13
14   def editable_attributes
15     %w(description)
16   end
17
18   def default_name
19     if script
20       x = "\"#{script}\" job"
21     else
22       x = super
23     end
24     if finished_at
25       x += " finished #{finished_at.strftime('%b %-d')}"
26     elsif started_at
27       x += " started #{started_at.strftime('%b %-d')}"
28     elsif created_at
29       x += " submitted #{created_at.strftime('%b %-d')}"
30     end
31   end
32
33   def cancel
34     arvados_api_client.api "jobs/#{self.uuid}/", "cancel", {"cascade" => true}
35   end
36
37   def self.queue_size
38     arvados_api_client.api("jobs/", "queue_size", {"_method"=> "GET"})[:queue_size] rescue 0
39   end
40
41   def self.queue
42     arvados_api_client.unpack_api_response arvados_api_client.api("jobs/", "queue", {"_method"=> "GET"})
43   end
44
45   def textile_attributes
46     [ 'description' ]
47   end
48
49   def stderr_log_query(limit=nil)
50     query = Log.where(object_uuid: self.uuid).order("created_at DESC").with_count('none')
51     query = query.limit(limit) if limit
52     query
53   end
54
55   def stderr_log_lines(limit=2000)
56     stderr_log_query(limit).results.reverse.
57       flat_map { |log| log.properties[:text].split("\n") rescue [] }
58   end
59
60   def work_unit(label=nil)
61     JobWorkUnit.new(self, label, self.uuid)
62   end
63 end