8876: move some methods such as log and output from job_work_unit into proxy_work_uni...
[arvados.git] / apps / workbench / app / models / job.rb
1 class Job < ArvadosBase
2   def self.goes_in_projects?
3     true
4   end
5
6   def content_summary
7     "#{script} job"
8   end
9
10   def editable_attributes
11     %w(description)
12   end
13
14   def default_name
15     if script
16       x = "\"#{script}\" job"
17     else
18       x = super
19     end
20     if finished_at
21       x += " finished #{finished_at.strftime('%b %-d')}"
22     elsif started_at
23       x += " started #{started_at.strftime('%b %-d')}"
24     elsif created_at
25       x += " submitted #{created_at.strftime('%b %-d')}"
26     end
27   end
28
29   def cancel
30     arvados_api_client.api "jobs/#{self.uuid}/", "cancel", {}
31   end
32
33   def self.queue_size
34     arvados_api_client.api("jobs/", "queue_size", {"_method"=> "GET"})[:queue_size] rescue 0
35   end
36
37   def self.queue
38     arvados_api_client.unpack_api_response arvados_api_client.api("jobs/", "queue", {"_method"=> "GET"})
39   end
40
41   def textile_attributes
42     [ 'description' ]
43   end
44
45   def stderr_log_query(limit=nil)
46     query = Log.where(event_type: "stderr", object_uuid: self.uuid)
47                .order("id DESC")
48     query = query.limit(limit) if limit
49     query
50   end
51
52   def stderr_log_lines(limit=2000)
53     stderr_log_query(limit).results.reverse.
54       flat_map { |log| log.properties[:text].split("\n") rescue [] }
55   end
56
57   def work_unit(label=nil)
58     JobWorkUnit.new(self, label)
59   end
60 end