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
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
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
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