Merge branch '8484-sanity-check-collection-count' closes #8484
[arvados.git] / services / api / app / models / container.rb
index 79c98681897ef449665b3b296ca440c6dfcd6687..787047df68e877bc8944b3c23186cc400c3ae227 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
 
@@ -25,6 +25,7 @@ class Container < ArvadosModel
     t.add :container_image
     t.add :cwd
     t.add :environment
+    t.add :exit_code
     t.add :finished_at
     t.add :log
     t.add :mounts
@@ -57,16 +58,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
@@ -122,7 +125,7 @@ class Container < ArvadosModel
 
     when Complete
       if self.state_changed?
-        permitted.push :state, :finished_at, :output, :log
+        permitted.push :state, :finished_at, :output, :log, :exit_code
       else
         errors.add :state, "cannot update record"
       end
@@ -145,22 +148,21 @@ 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
       act_as_system_user do
-        # Try to close container requests associated with this container
+        # Notify container requests associated with this container
         ContainerRequest.where(container_uuid: uuid,
                                :state => ContainerRequest::Committed).each do |cr|
-          cr.state = ContainerRequest::Final
-          cr.save
+          cr.container_completed!
         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