10671: Changed default assigments to be before validation. Also make sure started_at...
[arvados.git] / services / api / app / models / pipeline_instance.rb
index 28345d51f507e8eebe9f9baf9a7640f4460a1041..f84c4a310fd19904f4f5f85cdbea23a4c0b83770 100644 (file)
@@ -10,15 +10,14 @@ class PipelineInstance < ArvadosModel
   before_validation :bootstrap_components
   before_validation :update_state
   before_validation :verify_status
+  before_validation :update_timestamps_when_state_changes
   before_create :set_state_before_save
   before_save :set_state_before_save
 
   api_accessible :user, extend: :common do |t|
     t.add :pipeline_template_uuid
-    t.add :pipeline_template, :if => :pipeline_template
     t.add :name
     t.add :components
-    t.add :dependencies
     t.add :properties
     t.add :state
     t.add :components_summary
@@ -39,10 +38,6 @@ class PipelineInstance < ArvadosModel
      (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?
@@ -118,30 +113,6 @@ class PipelineInstance < ArvadosModel
     end
   end
 
-  def dependency_search(haystack)
-    if haystack.is_a? String
-      if (re = haystack.match /^([0-9a-f]{32}(\+[^,]+)*)+/)
-        {re[1] => true}
-      else
-        {}
-      end
-    elsif haystack.is_a? Array
-      deps = {}
-      haystack.each do |value|
-        deps.merge! dependency_search(value)
-      end
-      deps
-    elsif haystack.respond_to? :keys
-      deps = {}
-      haystack.each do |key, value|
-        deps.merge! dependency_search(value)
-      end
-      deps
-    else
-      {}
-    end
-  end
-
   def verify_status
     changed_attributes = self.changed
 
@@ -166,4 +137,17 @@ class PipelineInstance < ArvadosModel
     end
   end
 
+  def update_timestamps_when_state_changes
+    return if not (state_changed? or new_record?)
+
+    case state
+    when RunningOnServer, RunningOnClient
+      self.started_at ||= db_current_time
+    when Failed, Complete
+      current_time = db_current_time
+      self.started_at ||= current_time
+      self.finished_at ||= current_time
+    end
+  end
+
 end