Ejemplo n.º 1
0
def WaitOperation(name):
    """Wait till the operation is done.

  Args:
    name: The name of operation.

  Raises:
    exceptions.OperationErrorException: when the getting operation API fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the peering
        service.

  Returns:
    The result of the peering operation
  """
    def _CheckOp(name, result):  # pylint: disable=missing-docstring
        op = GetOperation(name)
        if op.done:
            result.append(op)
        return not op.done

    # Wait for no more than 30 minutes while retrying the Operation retrieval
    result = []
    try:
        retry.Retryer(exponential_sleep_multiplier=1.1,
                      wait_ceiling_ms=10000,
                      max_wait_ms=30 * 60 * 1000).RetryOnResult(
                          _CheckOp, [name, result],
                          should_retry_if=True,
                          sleep_ms=2000)
    except retry.MaxRetrialsException:
        raise exceptions.TimeoutError('Timed out while waiting for '
                                      'operation {0}. Note that the operation '
                                      'is still pending.'.format(name))
    return result[0] if result else None
Ejemplo n.º 2
0
def WaitForOperation(operation_ref, client):
    """Waits for an operation to complete.

  Args:
    operation_ref: A reference to the operation on which to wait.
    client: The client object that contains the GetOperation request object.

  Raises:
    TimeoutError: if the operation does not complete in time.
    OperationErrorException: if the operation fails.

  Returns:
    The Operation object, if successful. Raises an exception on failure.
  """
    WaitForOperation.operation_response = None
    messages = GetMessagesModule()
    operation_id = operation_ref.operationsId

    def _CheckOperation(operation_id):  # pylint: disable=missing-docstring
        request = messages.ServicemanagementOperationsGetRequest(
            operationsId=operation_id, )

        result = client.operations.Get(request)

        if result.done:
            WaitForOperation.operation_response = result
            return True
        else:
            return False

    # Wait for no more than 30 minutes while retrying the Operation retrieval
    try:
        retry.Retryer(exponential_sleep_multiplier=1.1,
                      wait_ceiling_ms=10000,
                      max_wait_ms=30 * 60 * 1000).RetryOnResult(
                          _CheckOperation, [operation_id],
                          should_retry_if=False,
                          sleep_ms=1500)
    except retry.MaxRetrialsException:
        raise exceptions.TimeoutError('Timed out while waiting for '
                                      'operation {0}. Note that the operation '
                                      'is still pending.'.format(operation_id))

    # Check to see if the operation resulted in an error
    if WaitForOperation.operation_response.error is not None:
        raise exceptions.OperationErrorException(
            'The operation with ID {0} resulted in a failure.'.format(
                operation_id))

    # If we've gotten this far, the operation completed successfully,
    # so return the Operation object
    return WaitForOperation.operation_response