code: def _get_task_with_policy(queue_name, task_id, owner):
      
      now = datetime.datetime.utcnow()
      task = (
          WorkQueue.query
          .filter_by(queue_name=queue_name, task_id=task_id)
          .with_lockmode('update')
          .first())
      if not task:
          raise TaskDoesNotExistError('task_id=%r' % task_id)
  
      # Lease delta should be positive, meaning it has not yet expired!
      lease_delta = now - task.eta
      if lease_delta > datetime.timedelta(0):
          db.session.rollback()
          raise LeaseExpiredError('queue=%r, task_id=%r expired %s' % (
                                  task.queue_name, task_id, lease_delta))
  
      if task.last_owner != owner:
          db.session.rollback()
          raise NotOwnerError('queue=%r, task_id=%r, owner=%r' % (
                              task.queue_name, task_id, task.last_owner))
  
      return task
docs: Fetches the specified task and enforces ownership policy.
  
      Args:
          queue_name: Name of the queue the work item is on.
          task_id: ID of the task that is finished.
          owner: Who or what has the current lease on the task.
  
      Returns:
          The valid WorkQueue task that is currently owned.
  
      Raises:
          TaskDoesNotExistError if the task does not exist.
          LeaseExpiredError if the lease is no longer active.
          NotOwnerError if the specified owner no longer owns the task.