Merge branch 'master' into 2681-new-inactive-user-notification
[arvados.git] / services / api / app / models / pipeline_instance.rb
index 32c284bb6d5f3a0b15c6504957eb43d619e789ff..7bb814c60d4a902d15a61485952ead0437a96cbe 100644 (file)
@@ -1,5 +1,5 @@
 class PipelineInstance < ArvadosModel
-  include AssignUuid
+  include HasUuid
   include KindAndEtag
   include CommonApiTemplate
   serialize :components, Hash
@@ -27,13 +27,16 @@ class PipelineInstance < ArvadosModel
   end
 
   # Supported states for a pipeline instance
-  New = 'New'
-  Ready = 'Ready'
-  RunningOnServer = 'RunningOnServer'
-  RunningOnClient = 'RunningOnClient'
-  Paused = 'Paused'
-  Failed = 'Failed'
-  Complete = 'Complete'
+  States =
+    [
+     (New = 'New'),
+     (Ready = 'Ready'),
+     (RunningOnServer = 'RunningOnServer'),
+     (RunningOnClient = 'RunningOnClient'),
+     (Paused = 'Paused'),
+     (Failed = 'Failed'),
+     (Complete = 'Complete'),
+    ]
 
   def dependencies
     dependency_search(self.components).keys
@@ -98,7 +101,7 @@ class PipelineInstance < ArvadosModel
   end
 
   def self.queue
-    self.where("active = true or state = 'RunningOnClient'")
+    self.where("state = 'RunningOnServer'")
   end
 
   protected
@@ -150,7 +153,7 @@ class PipelineInstance < ArvadosModel
         self.active = true
         self.success = nil
       when RunningOnClient
-        self.active = false
+        self.active = nil
         self.success = nil
       when Failed
         self.active = false
@@ -163,6 +166,7 @@ class PipelineInstance < ArvadosModel
         return false
       end
     elsif 'success'.in? changed_attributes
+      logger.info "pipeline_instance changed_attributes has success for #{self.uuid}"
       if self.success
         self.active = false
         self.state = Complete
@@ -171,24 +175,39 @@ class PipelineInstance < ArvadosModel
         self.state = Failed
       end
     elsif 'active'.in? changed_attributes
+      logger.info "pipeline_instance changed_attributes has active for #{self.uuid}"
       if self.active
-        if self.state == New || self.state == Ready || self.state == Paused
+        if self.state.in? [New, Ready, Paused]
           self.state = RunningOnServer
         end
       else
-        if self.components_look_ready?
+        if self.state == RunningOnServer # state was RunningOnServer
+          self.active = nil
+          self.state = Paused
+        elsif self.components_look_ready?
           self.state = Ready
         else
           self.state = New
         end
       end
+    elsif new_record? and self.state.nil?
+      # No state, active, or success given
+      self.state = New
     end
 
-    if 'components'.in? changed_attributes
-      if self.components_look_ready? && (!self.state || self.state == New)
+    if new_record? or 'components'.in? changed_attributes
+      self.state ||= New
+      if self.state == New and self.components_look_ready?
         self.state = Ready
       end
     end
+
+    if self.state.in?(States)
+      true
+    else
+      errors.add :state, "'#{state.inspect} must be one of: [#{States.join ', '}]"
+      false
+    end
   end
 
   def set_state_before_save
@@ -199,10 +218,6 @@ class PipelineInstance < ArvadosModel
         self.state = Ready
       end
     end
-
-    if self.state == Ready || self.state == New
-      self.active = nil
-    end 
   end
 
 end