X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/6f6ce90eb894bde190fd7522cbec037fe61fc25c..704dd826a498db11a052fe77a4953b0caa245c10:/services/api/app/models/container.rb diff --git a/services/api/app/models/container.rb b/services/api/app/models/container.rb index 83765fb1dc..b013776b98 100644 --- a/services/api/app/models/container.rb +++ b/services/api/app/models/container.rb @@ -24,6 +24,7 @@ class Container < ArvadosModel before_validation :fill_field_defaults, :if => :new_record? before_validation :set_timestamps validates :command, :container_image, :output_path, :cwd, :priority, :presence => true + validates :priority, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 1000 } validate :validate_state_change validate :validate_change validate :validate_lock @@ -31,6 +32,7 @@ class Container < ArvadosModel after_validation :assign_auth before_save :sort_serialized_attrs after_save :handle_completed + after_save :propagate_priority has_many :container_requests, :foreign_key => :container_uuid, :class_name => 'ContainerRequest', :primary_key => :uuid belongs_to :auth, :class_name => 'ApiClientAuthorization', :foreign_key => :auth_uuid, :primary_key => :uuid @@ -88,11 +90,25 @@ class Container < ArvadosModel self.priority = ContainerRequest. where(container_uuid: uuid, state: ContainerRequest::Committed). - maximum('priority') + maximum('priority') || 0 self.save! end end + def propagate_priority + if self.priority_changed? + act_as_system_user do + # Update the priority of child container requests to match new priority + # of the parent container. + ContainerRequest.where(requesting_container_uuid: self.uuid, + state: ContainerRequest::Committed).each do |cr| + cr.priority = self.priority + cr.save + end + end + end + end + # Create a new container (or find an existing one) to satisfy the # given container request. def self.resolve(req) @@ -318,7 +334,7 @@ class Container < ArvadosModel self.runtime_constraints ||= {} self.mounts ||= {} self.cwd ||= "." - self.priority ||= 1 + self.priority ||= 0 self.scheduling_parameters ||= {} end @@ -421,12 +437,10 @@ class Container < ArvadosModel # that a container cannot "claim" a collection that it doesn't otherwise # have access to just by setting the output field to the collection PDH. if output_changed? - c = Collection.unscoped do - Collection. - readable_by(current_user). + c = Collection. + readable_by(current_user, {include_trash: true}). where(portable_data_hash: self.output). first - end if !c errors.add :output, "collection must exist and be readable by current user." end @@ -501,7 +515,7 @@ class Container < ArvadosModel cr.with_lock do # Use row locking because this increments container_count cr.container_uuid = c.uuid - cr.save + cr.save! end end end @@ -512,11 +526,21 @@ class Container < ArvadosModel cr.finalize! end - # Try to cancel any outstanding container requests made by this container. - ContainerRequest.where(requesting_container_uuid: uuid, - state: ContainerRequest::Committed).each do |cr| - cr.priority = 0 - cr.save + # Cancel outstanding container requests made by this container. + ContainerRequest. + includes(:container). + where(requesting_container_uuid: uuid, + state: ContainerRequest::Committed).each do |cr| + cr.update_attributes!(priority: 0) + cr.container.reload + if cr.container.state == Container::Queued || cr.container.state == Container::Locked + # If the child container hasn't started yet, finalize the + # child CR now instead of leaving it "on hold", i.e., + # Queued with priority 0. (OTOH, if the child is already + # running, leave it alone so it can get cancelled the + # usual way, get a copy of the log collection, etc.) + cr.update_attributes!(state: ContainerRequest::Final) + end end end end