def WaitForOperation(sql_client, operation_ref, message): """Wait for a Cloud SQL operation to complete. Args: sql_client: apitools.BaseApiClient, The client used to make requests. operation_ref: resources.Resource, A reference for the operation to poll. message: str, The string to print while polling. Returns: True if the operation succeeded without error. Raises: OperationError: If the operation has an error code. """ with console_io.ProgressTracker(message, autotick=False) as pt: while True: op = sql_client.operations.Get(operation_ref.Request()) if op.error: raise OperationError(op.error[0].code) pt.Tick() if op.state == 'DONE': return True if op.state == 'UNKNOWN': return False # TODO(user): As the cloud sql people for the best retry schedule. time.sleep(2)
def WaitForOperation(operation_name, project, context, operation_description, timeout=None): """Wait for an operation to complete. Polls the operation requested approximately every second, showing a progress indicator. Returns when the operation has completed. Args: operation_name: The name of the operation to wait on, as returned by operations.list. project: The name of the project that this operation belongs to. context: Context object with messages and client to access the deploymentmanager service. operation_description: A short description of the operation to wait on, such as 'create' or 'delete'. Will be displayed to the user. timeout: Optional (approximate) timeout in seconds, after which wait will return failure. Raises: HttpException: A http error response was received while executing api request. Will be raised if the operation cannot be found. DeploymentManagerError: The operation finished with error(s) or exceeded the timeout without completing. """ client = context['deploymentmanager-v2beta2'] messages = context['deploymentmanager-v2beta2-messages'] ticks = 0 message = ('Waiting for ' + ('{0} '.format(operation_description) if operation_description else '') + operation_name) with console_io.ProgressTracker(message, autotick=False) as ticker: while timeout is None or ticks < timeout: ticks += 1 try: operation = client.operations.Get( messages.DeploymentmanagerOperationsGetRequest( project=project, operation=operation_name, )) except apitools_base.HttpError as error: raise HttpException(GetError(error)) ticker.Tick() # Operation status will be one of PENDING, RUNNING, DONE if operation.status == 'DONE': if operation.error: raise DeploymentManagerError('Error in Operation ' + operation_name + ': ' + str(operation.error)) else: # Operation succeeded return time.sleep(1) # wait one second and try again # Timeout exceeded raise DeploymentManagerError('Wait for Operation ' + operation_name + ' exceeded timeout.')
def WaitForOp(context, op_id, text): cli = context['clusteradmin'] msg = context[ 'clusteradmin-msgs'].BigtableclusteradminOperationsGetRequest( name=op_id) with console_io.ProgressTracker(text, autotick=False) as pt: while not cli.operations.Get(msg).done: pt.Tick() time.sleep(0.5)
def WaitForOperation(sql_client, operation_ref, message): with console_io.ProgressTracker(message, autotick=False) as pt: while True: op = sql_client.operations.Get(operation_ref.Request()) pt.Tick() # TODO(user): Make sure we recognize operation failures as well. if op.state == 'DONE': return True if op.state == 'UNKNOWN': return False # TODO(user): As the cloud sql people for the best retry schedule. time.sleep(2)
def WaitForOperation(operation, project_id, context, message, timeout_s=1200, poll_period_s=5): """Poll container Operation until its status is done or timeout reached. Args: operation: Operation message of the operation to be polled. project_id: str, project which owns this operation. context: dict, container Command context. message: str, message to display to user while polling. timeout_s: number, seconds to poll with retries before timing out. poll_period_s: number, delay in seconds between requests. Returns: Operation: the return value of the last successful operations.get request. Raises: Error: if the operation times out or finishes with an error. """ client = context['container_client'] messages = context['container_messages'] req = messages.ContainerProjectsZonesOperationsGetRequest( operationId=operation.name, projectId=project_id, zoneId=operation.zone) with console_io.ProgressTracker(message, autotick=True): start_time = time.clock() while timeout_s > (time.clock() - start_time): try: operation = client.projects_zones_operations.Get(req) if operation.status == messages.Operation.StatusValueValuesEnum.done: # Success! log.info('Operation %s succeeded after %.3f seconds', operation, (time.clock() - start_time)) break except apitools_base.HttpError as error: log.debug('GetOperation failed: %s', error) # Keep trying until we timeout in case error is transient. time.sleep(poll_period_s) if operation.status != messages.Operation.StatusValueValuesEnum.done: log.err.Print('Timed out waiting for operation %s' % operation) raise Error( 'Operation [{0}] is still running'.format(operation)) if operation.errorMessage: raise Error('Operation [{0}] finished with error: {1}'.format( operation, operation.errorMessage)) return operation
def WaitForOperation(self, operation_ref, message, timeout_s=1200, poll_period_s=5): """Poll container Operation until its status is done or timeout reached. Args: operation_ref: operation resource. message: str, message to display to user while polling. timeout_s: number, seconds to poll with retries before timing out. poll_period_s: number, delay in seconds between requests. Returns: Operation: the return value of the last successful operations.get request. Raises: Error: if the operation times out or finishes with an error. """ with console_io.ProgressTracker(message, autotick=True): start_time = time.clock() while timeout_s > (time.clock() - start_time): try: operation = self.GetOperation(operation_ref) if self.IsOperationFinished(operation): # Success! log.info('Operation %s succeeded after %.3f seconds', operation, (time.clock() - start_time)) break except apitools_base.HttpError as error: log.debug('GetOperation failed: %s', error) # Keep trying until we timeout in case error is transient. time.sleep(poll_period_s) if not self.IsOperationFinished(operation): log.err.Print( 'Timed out waiting for operation {0}'.format(operation)) raise util.Error( 'Operation [{0}] is still running'.format(operation)) if self.GetOperationError(operation): raise util.Error('Operation [{0}] finished with error: {1}'.format( operation, self.GetOperationError(operation))) return operation
def GetToolResultsIds(matrix, testing_api_helper, status_interval=STATUS_INTERVAL_SECS): """Gets the Tool Results history ID and execution ID for a test matrix. Sometimes the IDs are available immediately after a test matrix is created. If not, we keep checking the matrix until the Testing and Tool Results services have had enough time to create/assign the IDs, giving the user continuous feedback using gcloud core's ProgressTracker class. Args: matrix: a TestMatrix which was just created by the Testing service. testing_api_helper: a TestingApiHelper object. status_interval: float, number of seconds to sleep between status checks. Returns: A ToolResultsIds tuple containing the history ID and execution ID, which are shared by all TestExecutions in the TestMatrix. Raises: ToolException if either ID is missing from the TestMatrix. """ history_id = None execution_id = None msg = 'Creating individual test executions' with console_io.ProgressTracker(msg, autotick=False) as pt: while True: if matrix.resultStorage.toolResultsExecution: history_id = matrix.resultStorage.toolResultsExecution.historyId execution_id = matrix.resultStorage.toolResultsExecution.executionId if history_id and execution_id: break pt.Tick() time.sleep(status_interval) matrix = testing_api_helper.GetTestMatrixStatus( matrix.testMatrixId) return ToolResultsIds(history_id=history_id, execution_id=execution_id)