20032: Fix unnecessary race in test.
[arvados.git] / apps / workbench / app / models / pipeline_instance.rb
index 03d70b2897063435298707e3068dfa79ca16ff34..d481f41c7eab37b0f8ba0192ba9e17438fd3803e 100644 (file)
@@ -1,3 +1,7 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 require "arvados/keep"
 
 class PipelineInstance < ArvadosBase
@@ -13,7 +17,7 @@ class PipelineInstance < ArvadosBase
       template = if lookup and lookup[self.pipeline_template_uuid]
                    lookup[self.pipeline_template_uuid]
                  else
-                   PipelineTemplate.where(uuid: self.pipeline_template_uuid).first
+                   PipelineTemplate.find?(self.pipeline_template_uuid) if self.pipeline_template_uuid
                  end
       if template
         template.name
@@ -92,6 +96,10 @@ class PipelineInstance < ArvadosBase
     components_map { |cspec| cspec[:job][:log] rescue nil }
   end
 
+  def job_ids
+    components_map { |cspec| cspec[:job][:uuid] rescue nil }
+  end
+
   def stderr_log_object_uuids
     result = job_uuids.values.compact
     result << uuid
@@ -99,9 +107,10 @@ class PipelineInstance < ArvadosBase
 
   def stderr_log_query(limit=nil)
     query = Log.
-      where(event_type: "stderr",
-            object_uuid: stderr_log_object_uuids).
-      order("id DESC")
+            with_count('none').
+            where(event_type: "stderr",
+                  object_uuid: stderr_log_object_uuids).
+            order("created_at DESC")
     unless limit.nil?
       query = query.limit(limit)
     end
@@ -118,16 +127,24 @@ class PipelineInstance < ArvadosBase
       Keep::Locator.parse(loc_s)
     end
     if log_pdhs.any? and
-        Collection.where(portable_data_hash: log_pdhs).limit(1).results.any?
+        Collection.where(portable_data_hash: log_pdhs).limit(1).with_count("none").results.any?
       true
     elsif log_uuids.any? and
-        Collection.where(uuid: log_uuids).limit(1).results.any?
+        Collection.where(uuid: log_uuids).limit(1).with_count("none").results.any?
       true
     else
       stderr_log_query(1).results.any?
     end
   end
 
+  def work_unit(label=nil)
+    PipelineInstanceWorkUnit.new(self, label || self.name, self.uuid)
+  end
+
+  def cancel
+    arvados_api_client.api "pipeline_instances/#{self.uuid}/", "cancel", {"cascade" => true}
+  end
+
   private
 
   def components_map