Merge branch '3187-pipeline-instance-page' into 3605-improved-dashboard
[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 attribute_editable? attr, *args
11     if attr.to_sym == :description
12       super && attr.to_sym == :description
13     else
14       false
15     end
16   end
17
18   def self.creatable?
19     false
20   end
21
22   def default_name
23     if script
24       x = "\"#{script}\" job"
25     else
26       x = super
27     end
28     if finished_at
29       x += " finished #{finished_at.strftime('%b %-d')}"
30     elsif started_at
31       x += " started #{started_at.strftime('%b %-d')}"
32     elsif created_at
33       x += " submitted #{created_at.strftime('%b %-d')}"
34     end
35   end
36
37   def cancel
38     arvados_api_client.api "jobs/#{self.uuid}/", "cancel", {}
39   end
40
41   def self.queue_size
42     arvados_api_client.api("jobs/", "queue_size", {"_method"=> "GET"})[:queue_size] rescue 0
43   end
44
45   def self.state job
46     if job.respond_to? :state and job.state
47       return job.state
48     end
49
50     if not job[:cancelled_at].nil?
51       "Cancelled"
52     elsif not job[:finished_at].nil? or not job[:success].nil?
53       if job[:success]
54         "Completed"
55       else
56         "Failed"
57       end
58     elsif job[:running]
59       "Running"
60     else
61       "Queued"
62     end
63   end
64
65   def textile_attributes
66     [ 'description' ]
67   end
68 end