6429: Rename request_finalize to handle_completed
[arvados.git] / services / api / app / models / container.rb
index 79c98681897ef449665b3b296ca440c6dfcd6687..d777c0c2fe1e81148a0345fec8e71895cb403d01 100644 (file)
@@ -13,10 +13,10 @@ class Container < ArvadosModel
 
   before_validation :fill_field_defaults, :if => :new_record?
   before_validation :set_timestamps
-  validates :command, :container_image, :output_path, :cwd, :presence => true
+  validates :command, :container_image, :output_path, :cwd, :priority, :presence => true
   validate :validate_state_change
   validate :validate_change
-  after_save :request_finalize
+  after_save :handle_completed
 
   has_many :container_requests, :foreign_key => :container_uuid, :class_name => 'ContainerRequest', :primary_key => :uuid
 
@@ -57,16 +57,18 @@ class Container < ArvadosModel
   end
 
   def update_priority!
-    # Update the priority of this container to the maximum priority of any of
-    # its committed container requests and save the record.
-    max = 0
-    ContainerRequest.where(container_uuid: uuid).each do |cr|
-      if cr.state == ContainerRequest::Committed and cr.priority > max
-        max = cr.priority
+    if [Queued, Running].include? self.state
+      # Update the priority of this container to the maximum priority of any of
+      # its committed container requests and save the record.
+      max = 0
+      ContainerRequest.where(container_uuid: uuid).each do |cr|
+        if cr.state == ContainerRequest::Committed and cr.priority > max
+          max = cr.priority
+        end
       end
+      self.priority = max
+      self.save!
     end
-    self.priority = max
-    self.save!
   end
 
   protected
@@ -145,7 +147,7 @@ class Container < ArvadosModel
     check_update_whitelist permitted
   end
 
-  def request_finalize
+  def handle_completed
     # This container is finished so finalize any associated container requests
     # that are associated with this container.
     if self.state_changed? and [Complete, Cancelled].include? self.state
@@ -157,10 +159,10 @@ class Container < ArvadosModel
           cr.save
         end
 
-        # Try to close any outstanding container requests made by this container.
+        # Try to cancel any outstanding container requests made by this container.
         ContainerRequest.where(requesting_container_uuid: uuid,
                                :state => ContainerRequest::Committed).each do |cr|
-          cr.state = ContainerRequest::Final
+          cr.priority = 0
           cr.save
         end
       end