13773: Containers & CRs show "Failing" when a child job failed.
[arvados.git] / apps / workbench / app / models / container_work_unit.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class ContainerWorkUnit < ProxyWorkUnit
6   attr_accessor :container
7   attr_accessor :child_proxies
8
9   def initialize proxied, label, parent, child_objects=nil
10     super proxied, label, parent
11     if @proxied.is_a?(ContainerRequest)
12       container_uuid = get(:container_uuid)
13       if container_uuid
14         @container = Container.find(container_uuid)
15       end
16     end
17     @child_proxies = child_objects
18   end
19
20   def children
21     return @my_children if @my_children
22
23     items = []
24     container_uuid = if @proxied.is_a?(Container) then uuid else get(:container_uuid) end
25     if container_uuid
26       cols = ContainerRequest.columns.map(&:name) - %w(id updated_at mounts secret_mounts)
27       my_children = @child_proxies || ContainerRequest.select(cols).where(requesting_container_uuid: container_uuid).results if !my_children
28       my_child_containers = my_children.map(&:container_uuid).compact.uniq
29       grandchildren = {}
30       my_child_containers.each { |c| grandchildren[c] = []} if my_child_containers.any?
31       reqs = ContainerRequest.select(cols).where(requesting_container_uuid: my_child_containers).results if my_child_containers.any?
32       reqs.each {|cr| grandchildren[cr.requesting_container_uuid] << cr} if reqs
33
34       my_children.each do |cr|
35         items << cr.work_unit(cr.name || 'this container', child_objects=grandchildren[cr.container_uuid])
36       end
37     end
38
39     @child_proxies = nil #no need of this any longer
40     @my_children = items
41   end
42
43   def title
44     "container"
45   end
46
47   def uri
48     uuid = get(:uuid)
49
50     return nil unless uuid
51
52     if @proxied.class.respond_to? :table_name
53       "/#{@proxied.class.table_name}/#{uuid}"
54     else
55       resource_class = ArvadosBase.resource_class_for_uuid(uuid)
56       "#{resource_class.table_name}/#{uuid}" if resource_class
57     end
58   end
59
60   def can_cancel?
61     @proxied.is_a?(ContainerRequest) &&
62       @proxied.state == "Committed" &&
63       (@proxied.priority > 0 || get(:state, @container) != 'Running') &&
64       @proxied.editable?
65   end
66
67   def container_uuid
68     get(:container_uuid)
69   end
70
71   def requesting_container_uuid
72     get(:requesting_container_uuid)
73   end
74
75   def priority
76     @proxied.priority
77   end
78
79   # For the following properties, use value from the @container if exists
80   # This applies to a ContainerRequest with container_uuid
81
82   def started_at
83     t = get_combined(:started_at)
84     t = Time.parse(t) if (t.is_a? String)
85     t
86   end
87
88   def modified_at
89     t = get_combined(:modified_at)
90     t = Time.parse(t) if (t.is_a? String)
91     t
92   end
93
94   def finished_at
95     t = get_combined(:finished_at)
96     t = Time.parse(t) if (t.is_a? String)
97     t
98   end
99
100   def state_label
101     if get(:state) == 'Final' && get(:state, @container) != 'Complete'
102       # Request was finalized before its container started (or the
103       # container was cancelled)
104       return 'Cancelled'
105     end
106     state = get(:state, @container) || get(:state, @proxied)
107     case state
108     when 'Locked', 'Queued'
109       if priority == 0
110         'On hold'
111       else
112         'Queued'
113       end
114     when 'Complete'
115       if exit_code == 0
116         state
117       else
118         'Failed'
119       end
120     when 'Running'
121       if runtime_status[:error]
122         'Failing'
123       else
124         state
125       end
126     else
127       # Cancelled, or Uncommitted (no container assigned)
128       state
129     end
130   end
131
132   def runtime_status
133     return get(:runtime_status, @container) || get(:runtime_status, @proxied)
134   end
135
136   def state_bootstrap_class
137     case state_label
138     when 'Failing'
139       'danger'
140     else
141       super
142     end
143   end
144
145   def exit_code
146     get_combined(:exit_code)
147   end
148
149   def docker_image
150     get_combined(:container_image)
151   end
152
153   def runtime_constraints
154     get_combined(:runtime_constraints)
155   end
156
157   def log_collection
158     if @proxied.is_a?(ContainerRequest)
159       get(:log_uuid)
160     else
161       get(:log)
162     end
163   end
164
165   def outputs
166     items = []
167     if @proxied.is_a?(ContainerRequest)
168       out = get(:output_uuid)
169     else
170       out = get(:output)
171     end
172     items << out if out
173     items
174   end
175
176   def command
177     get_combined(:command)
178   end
179
180   def cwd
181     get_combined(:cwd)
182   end
183
184   def environment
185     env = get_combined(:environment)
186     env = nil if env.andand.empty?
187     env
188   end
189
190   def mounts
191     mnt = get_combined(:mounts)
192     mnt = nil if mnt.andand.empty?
193     mnt
194   end
195
196   def output_path
197     get_combined(:output_path)
198   end
199
200   def log_object_uuids
201     [get(:uuid, @container), get(:uuid, @proxied)].compact
202   end
203
204   def render_log
205     collection = Collection.find(log_collection) rescue nil
206     if collection
207       return {log: collection, partial: 'collections/show_files', locals: {object: collection, no_checkboxes: true}}
208     end
209   end
210
211   def template_uuid
212     properties = get(:properties)
213     if properties
214       properties[:template_uuid]
215     end
216   end
217
218   # End combined properties
219
220   protected
221   def get_combined key
222     from_container = get(key, @container)
223     from_proxied = get(key, @proxied)
224
225     if from_container.is_a? Hash or from_container.is_a? Array
226       if from_container.any? then from_container else from_proxied end
227     else
228       from_container || from_proxied
229     end
230   end
231 end