- def process_tree_priority
- if self.priority_changed?
- if self.priority == 0
- act_as_system_user do
- ContainerRequest.where(requesting_container_uuid: uuid).each do |cr|
- cr.priority = self.priority
- cr.save!
- end
+ def assign_auth
+ if self.auth_uuid_changed?
+ return errors.add :auth_uuid, 'is readonly'
+ end
+ if not [Locked, Running].include? self.state
+ # don't need one
+ self.auth.andand.update_attributes(expires_at: db_current_time)
+ self.auth = nil
+ return
+ elsif self.auth
+ # already have one
+ return
+ end
+ cr = ContainerRequest.
+ where('container_uuid=? and priority>0', self.uuid).
+ order('priority desc').
+ first
+ if !cr
+ return errors.add :auth_uuid, "cannot be assigned because priority <= 0"
+ end
+ self.auth = ApiClientAuthorization.
+ create!(user_id: User.find_by_uuid(cr.modified_by_user_uuid).id,
+ api_client_id: 0)
+ end
+
+ 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
+ # Notify container requests associated with this container
+ ContainerRequest.where(container_uuid: uuid,
+ :state => ContainerRequest::Committed).each do |cr|
+ cr.container_completed!
+ 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