Merge branch '9996-stop-on-error'
[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(object_uuid: self.uuid).order("created_at DESC")
47     query = query.limit(limit) if limit
48     query
49   end
50
51   def stderr_log_lines(limit=2000)
52     stderr_log_query(limit).results.reverse.
53       flat_map { |log| log.properties[:text].split("\n") rescue [] }
54   end
55
56   def work_unit(label=nil)
57     JobWorkUnit.new(self, label)
58   end
59 end