|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# flake8: noqa |
| 15 | + |
| 16 | + |
| 17 | +# This file is automatically generated. Please do not modify it directly. |
| 18 | +# Find the relevant recipe file in the samples/recipes or samples/ingredients |
| 19 | +# directory and apply your changes there. |
| 20 | + |
| 21 | + |
| 22 | +# [START compute_operation_extended_wait] |
| 23 | +import sys |
| 24 | +from typing import Any |
| 25 | + |
| 26 | +from google.api_core.extended_operation import ExtendedOperation |
| 27 | + |
| 28 | + |
| 29 | +def wait_for_extended_operation( |
| 30 | + operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300 |
| 31 | +) -> Any: |
| 32 | + """ |
| 33 | + Waits for the extended (long-running) operation to complete. |
| 34 | +
|
| 35 | + If the operation is successful, it will return its result. |
| 36 | + If the operation ends with an error, an exception will be raised. |
| 37 | + If there were any warnings during the execution of the operation |
| 38 | + they will be printed to sys.stderr. |
| 39 | +
|
| 40 | + Args: |
| 41 | + operation: a long-running operation you want to wait on. |
| 42 | + verbose_name: (optional) a more verbose name of the operation, |
| 43 | + used only during error and warning reporting. |
| 44 | + timeout: how long (in seconds) to wait for operation to finish. |
| 45 | + If None, wait indefinitely. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + Whatever the operation.result() returns. |
| 49 | +
|
| 50 | + Raises: |
| 51 | + This method will raise the exception received from `operation.exception()` |
| 52 | + or RuntimeError if there is no exception set, but there is an `error_code` |
| 53 | + set for the `operation`. |
| 54 | +
|
| 55 | + In case of an operation taking longer than `timeout` seconds to complete, |
| 56 | + a `concurrent.futures.TimeoutError` will be raised. |
| 57 | + """ |
| 58 | + result = operation.result(timeout=timeout) |
| 59 | + |
| 60 | + if operation.error_code: |
| 61 | + print( |
| 62 | + f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}", |
| 63 | + file=sys.stderr, |
| 64 | + flush=True, |
| 65 | + ) |
| 66 | + print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True) |
| 67 | + raise operation.exception() or RuntimeError(operation.error_message) |
| 68 | + |
| 69 | + if operation.warnings: |
| 70 | + print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True) |
| 71 | + for warning in operation.warnings: |
| 72 | + print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True) |
| 73 | + |
| 74 | + return result |
| 75 | + |
| 76 | + |
| 77 | +# [END compute_operation_extended_wait] |
0 commit comments