2352: when using arv-run-pipeline-instance, set state to RunningOnClient.
[arvados.git] / services / api / app / models / pipeline_instance.rb
index b16769cb3cbe868548834c919d45499d2117f051..258ee2729a54b07bc1eabb9cdd74d0aaffa43baa 100644 (file)
@@ -4,10 +4,14 @@ class PipelineInstance < ArvadosModel
   include CommonApiTemplate
   serialize :components, Hash
   serialize :properties, Hash
+  serialize :components_summary, Hash
   belongs_to :pipeline_template, :foreign_key => :pipeline_template_uuid, :primary_key => :uuid
 
   before_validation :bootstrap_components
   before_validation :update_success
+  before_validation :verify_status
+  before_create :set_state_before_save
+  before_save :set_state_before_save
 
   api_accessible :user, extend: :common do |t|
     t.add :pipeline_template_uuid
@@ -18,12 +22,45 @@ class PipelineInstance < ArvadosModel
     t.add :active
     t.add :dependencies
     t.add :properties
+    t.add :state
+    t.add :components_summary
   end
 
+  # Supported states for a pipeline instance
+  New = 'New'
+  Ready = 'Ready'
+  RunningOnServer = 'RunningOnServer'
+  RunningOnClient = 'RunningOnClient'
+  Paused = 'Paused'
+  Failed = 'Failed'
+  Complete = 'Complete'
+
   def dependencies
     dependency_search(self.components).keys
   end
 
+  # if all components have input, the pipeline is Ready
+  def components_look_ready?
+    if !self.components || self.components.empty?
+      return false
+    end
+
+    all_components_have_input = true
+    self.components.each do |name, component|
+      component['script_parameters'].andand.each do |parametername, parameter|
+        parameter = { 'value' => parameter } unless parameter.is_a? Hash
+        if parameter['value'].nil? and parameter['required']
+          if parameter['output_of']
+            next
+          end
+          all_components_have_input = false
+          break
+        end
+      end
+    end
+    return all_components_have_input
+  end
+
   def progress_table
     begin
       # v0 pipeline format
@@ -61,7 +98,7 @@ class PipelineInstance < ArvadosModel
   end
 
   def self.queue
-    self.where('active = true')
+    self.where("active = true or state = 'RunningOnClient'")
   end
 
   protected
@@ -100,4 +137,68 @@ class PipelineInstance < ArvadosModel
       {}
     end
   end
+
+  def verify_status
+    changed_attributes = self.changed
+
+    if 'state'.in? changed_attributes
+      case self.state
+      when New, Ready, Paused
+        self.active = nil
+        self.success = nil
+      when RunningOnServer
+        self.active = true
+        self.success = nil
+      when RunningOnClient
+        self.active = false
+        self.success = nil
+      when Failed
+        self.active = false
+        self.success = false
+        self.state = Failed   # before_validation will fail if false is returned in the previous line
+      when Complete
+        self.active = false
+        self.success = true
+      else
+        return false
+      end
+    elsif 'success'.in? changed_attributes
+      if self.success
+        self.active = false
+        self.state = Complete
+      else
+        self.active = false
+        self.state = Failed
+      end
+    elsif 'active'.in? changed_attributes
+      if self.active
+        if self.state == New || self.state == Ready || self.state == Paused
+          self.state = RunningOnServer
+        end
+      else
+        if self.components_look_ready?
+          self.state = Ready
+        else
+          self.state = New
+        end
+      end
+    end
+
+    if 'components'.in? changed_attributes
+      if self.components_look_ready? && (!self.state || self.state == New)
+        self.state = Ready
+      end
+    end
+  end
+
+  def set_state_before_save
+    if !self.state || self.state == New || self.state == Ready || self.state == Paused
+      if self.active
+        self.state = RunningOnServer
+      elsif self.components_look_ready? && (!self.state || self.state == New)
+        self.state = Ready
+      end
+    end
+  end
+
 end