ba2776b39e520fd0fc0818bf1d273e9be2a0d574
[arvados.git] / apps / workbench / app / models / container_work_unit.rb
1 class ContainerWorkUnit < ProxyWorkUnit
2   attr_accessor :container
3
4   def initialize proxied, label
5     super
6     if @proxied.is_a?(ContainerRequest)
7       container_uuid = get(:container_uuid)
8       if container_uuid
9         @container = Container.where(uuid: container_uuid).first
10       end
11     end
12   end
13
14   def children
15     return self.my_children if self.my_children
16
17     items = []
18
19     if @proxied.is_a?(Container)
20       # If @proxied is a container, get all containter_requests where this is
21       # requesting_container_uuid and containers for their container_uuids
22       crs = {}
23       reqs = ContainerRequest.where(requesting_container_uuid: uuid).results
24       reqs.each { |cr| crs[cr.container_uuid] = cr.name }
25
26       containers = Container.where(uuid: crs.keys).results
27       containers.each do |c|
28         items << c.work_unit(crs[c.uuid] || 'this container')
29       end
30
31       self.my_children = items
32     else
33       # Else for a container_request, get all container_requests whose
34       # requesting_container_uuid is this container_request's container_uuid.
35       container_uuid = get(:container_uuid)
36       if container_uuid
37         reqs = ContainerRequest.where(requesting_container_uuid: container_uuid).results
38         reqs.each do |cr|
39           items << cr.work_unit(cr.name || 'this container')
40         end
41       end
42     end
43
44     self.my_children = items
45   end
46
47   def title
48     "container"
49   end
50
51   def uri
52     uuid = get(:uuid)
53     "/#{@proxied.class.table_name}/#{uuid}" rescue nil
54   end
55
56
57   def can_cancel?
58     @proxied.is_a?(ContainerRequest) && state_label.in?(["Queued", "Locked", "Running"]) && priority > 0
59   end
60
61   def container_uuid
62     get(:container_uuid)
63   end
64
65   # For the following properties, use value from the @container if exists
66   # This applies to a ContainerRequest with container_uuid
67
68   def started_at
69     t = get_combined(:started_at)
70     t = Time.parse(t) if (t.is_a? String)
71     t
72   end
73
74   def modified_at
75     t = get_combined(:modified_at)
76     t = Time.parse(t) if (t.is_a? String)
77     t
78   end
79
80   def finished_at
81     t = get_combined(:finished_at)
82     t = Time.parse(t) if (t.is_a? String)
83     t
84   end
85
86   def state_label
87     get_combined(:state)
88   end
89
90   def docker_image
91     get_combined(:container_image)
92   end
93
94   def runtime_constraints
95     get_combined(:runtime_constraints)
96   end
97
98   def priority
99     get_combined(:priority)
100   end
101
102   def log_collection
103     get_combined(:log)
104   end
105
106   def outputs
107     items = []
108     items << get_combined(:output) if get_combined(:output)
109     items
110   end
111
112   def command
113     get_combined(:command)
114   end
115
116   def cwd
117     get_combined(:cwd)
118   end
119
120   def environment
121     env = get_combined(:environment)
122     env = nil if env.andand.empty?
123     env
124   end
125
126   def mounts
127     mnt = get_combined(:mounts)
128     mnt = nil if mnt.andand.empty?
129     mnt
130   end
131
132   def output_path
133     get_combined(:output_path)
134   end
135
136   # End combined propeties
137
138   protected
139   def get_combined key
140     get(key, @container) || get(key, @proxied)
141   end
142 end