Merge branch 'master' into 3889-functional-testing
[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.queue 
46     arvados_api_client.unpack_api_response arvados_api_client.api("jobs/", "queue", {"_method"=> "GET"})
47   end
48
49   def self.state job
50     if job.respond_to? :state and job.state
51       return job.state
52     end
53
54     if not job[:cancelled_at].nil?
55       "Cancelled"
56     elsif not job[:finished_at].nil? or not job[:success].nil?
57       if job[:success]
58         "Completed"
59       else
60         "Failed"
61       end
62     elsif job[:running]
63       "Running"
64     else
65       "Queued"
66     end
67   end
68
69   def textile_attributes
70     [ 'description' ]
71   end
72 end