Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / apps / workbench / app / models / container_work_unit.rb
index a5b26f0d6d3d5534fd19e1df76d45ac97e7aadd8..292bc3679bc3710dacf2b59077726fd77d89b5e1 100644 (file)
@@ -14,6 +14,7 @@ class ContainerWorkUnit < ProxyWorkUnit
         @container = Container.find(container_uuid)
       end
     end
+    @container = nil if !defined?(@container)
     @child_proxies = child_objects
   end
 
@@ -23,12 +24,12 @@ class ContainerWorkUnit < ProxyWorkUnit
     items = []
     container_uuid = if @proxied.is_a?(Container) then uuid else get(:container_uuid) end
     if container_uuid
-      cols = ContainerRequest.columns.map(&:name) - %w(id updated_at mounts)
-      my_children = @child_proxies || ContainerRequest.select(cols).where(requesting_container_uuid: container_uuid).results if !my_children
+      cols = ContainerRequest.columns.map(&:name) - %w(id updated_at mounts secret_mounts runtime_token)
+      my_children = @child_proxies || ContainerRequest.select(cols).where(requesting_container_uuid: container_uuid).with_count("none").results if !my_children
       my_child_containers = my_children.map(&:container_uuid).compact.uniq
       grandchildren = {}
       my_child_containers.each { |c| grandchildren[c] = []} if my_child_containers.any?
-      reqs = ContainerRequest.select(cols).where(requesting_container_uuid: my_child_containers).results if my_child_containers.any?
+      reqs = ContainerRequest.select(cols).where(requesting_container_uuid: my_child_containers).order(["requesting_container_uuid", "uuid"]).with_count("none").results if my_child_containers.any?
       reqs.each {|cr| grandchildren[cr.requesting_container_uuid] << cr} if reqs
 
       my_children.each do |cr|
@@ -58,7 +59,10 @@ class ContainerWorkUnit < ProxyWorkUnit
   end
 
   def can_cancel?
-    @proxied.is_a?(ContainerRequest) && @proxied.state == "Committed" && @proxied.priority > 0 && @proxied.editable?
+    @proxied.is_a?(ContainerRequest) &&
+      @proxied.state == "Committed" &&
+      (@proxied.priority > 0 || get(:state, @container) != 'Running') &&
+      @proxied.editable?
   end
 
   def container_uuid
@@ -95,14 +99,52 @@ class ContainerWorkUnit < ProxyWorkUnit
   end
 
   def state_label
-    ec = exit_code
-    return "Failed" if (ec && ec != 0)
+    if get(:state) == 'Final' && get(:state, @container) != 'Complete'
+      # Request was finalized before its container started (or the
+      # container was cancelled)
+      return 'Cancelled'
+    end
+    state = get(:state, @container) || get(:state, @proxied)
+    case state
+    when 'Locked', 'Queued'
+      if priority == 0
+        'On hold'
+      else
+        'Queued'
+      end
+    when 'Complete'
+      if exit_code == 0
+        state
+      else
+        'Failed'
+      end
+    when 'Running'
+      if runtime_status[:error]
+        'Failing'
+      elsif runtime_status[:warning]
+        'Warning'
+      else
+        state
+      end
+    else
+      # Cancelled, or Uncommitted (no container assigned)
+      state
+    end
+  end
 
-    state = get_combined(:state)
+  def runtime_status
+    return get(:runtime_status, @container) || get(:runtime_status, @proxied)
+  end
 
-    return "Queued" if state == "Locked"
-    return "Cancelled" if ((priority == 0) and (state == "Queued"))
-    state
+  def state_bootstrap_class
+    case state_label
+    when 'Failing'
+      'danger'
+    when 'Warning'
+      'warning'
+    else
+      super
+    end
   end
 
   def exit_code