|
| 1 | +# Copyright 2016 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 | + |
| 15 | +"""Futures for long-running operations returned from Google Cloud APIs. |
| 16 | +
|
| 17 | +These futures can be used to synchronously wait for the result of a |
| 18 | +long-running operation using :meth:`Operation.result`: |
| 19 | +
|
| 20 | +
|
| 21 | +.. code-block:: python |
| 22 | +
|
| 23 | + operation = my_api_client.long_running_method() |
| 24 | + result = operation.result() |
| 25 | +
|
| 26 | +Or asynchronously using callbacks and :meth:`Operation.add_done_callback`: |
| 27 | +
|
| 28 | +.. code-block:: python |
| 29 | +
|
| 30 | + operation = my_api_client.long_running_method() |
| 31 | +
|
| 32 | + def my_callback(future): |
| 33 | + result = future.result() |
| 34 | +
|
| 35 | + operation.add_done_callback(my_callback) |
| 36 | +
|
| 37 | +""" |
| 38 | + |
| 39 | +import functools |
| 40 | +import threading |
| 41 | + |
| 42 | +from google.api_core import exceptions |
| 43 | +from google.api_core import protobuf_helpers |
| 44 | +from google.api_core.future import async_future |
| 45 | +from google.longrunning import operations_pb2 |
| 46 | +from google.rpc import code_pb2 |
| 47 | + |
| 48 | + |
| 49 | +class AsyncOperation(async_future.AsyncFuture): |
| 50 | + """A Future for interacting with a Google API Long-Running Operation. |
| 51 | +
|
| 52 | + Args: |
| 53 | + operation (google.longrunning.operations_pb2.Operation): The |
| 54 | + initial operation. |
| 55 | + refresh (Callable[[], ~.api_core.operation.Operation]): A callable that |
| 56 | + returns the latest state of the operation. |
| 57 | + cancel (Callable[[], None]): A callable that tries to cancel |
| 58 | + the operation. |
| 59 | + result_type (func:`type`): The protobuf type for the operation's |
| 60 | + result. |
| 61 | + metadata_type (func:`type`): The protobuf type for the operation's |
| 62 | + metadata. |
| 63 | + retry (google.api_core.retry.Retry): The retry configuration used |
| 64 | + when polling. This can be used to control how often :meth:`done` |
| 65 | + is polled. Regardless of the retry's ``deadline``, it will be |
| 66 | + overridden by the ``timeout`` argument to :meth:`result`. |
| 67 | + """ |
| 68 | + |
| 69 | + def __init__( |
| 70 | + self, |
| 71 | + operation, |
| 72 | + refresh, |
| 73 | + cancel, |
| 74 | + result_type, |
| 75 | + metadata_type=None, |
| 76 | + retry=async_future.DEFAULT_RETRY, |
| 77 | + ): |
| 78 | + super().__init__(retry=retry) |
| 79 | + self._operation = operation |
| 80 | + self._refresh = refresh |
| 81 | + self._cancel = cancel |
| 82 | + self._result_type = result_type |
| 83 | + self._metadata_type = metadata_type |
| 84 | + self._completion_lock = threading.Lock() |
| 85 | + # Invoke this in case the operation came back already complete. |
| 86 | + self._set_result_from_operation() |
| 87 | + |
| 88 | + @property |
| 89 | + def operation(self): |
| 90 | + """google.longrunning.Operation: The current long-running operation.""" |
| 91 | + return self._operation |
| 92 | + |
| 93 | + @property |
| 94 | + def metadata(self): |
| 95 | + """google.protobuf.Message: the current operation metadata.""" |
| 96 | + if not self._operation.HasField("metadata"): |
| 97 | + return None |
| 98 | + |
| 99 | + return protobuf_helpers.from_any_pb( |
| 100 | + self._metadata_type, self._operation.metadata |
| 101 | + ) |
| 102 | + |
| 103 | + @classmethod |
| 104 | + def deserialize(cls, payload): |
| 105 | + """Deserialize a ``google.longrunning.Operation`` protocol buffer. |
| 106 | +
|
| 107 | + Args: |
| 108 | + payload (bytes): A serialized operation protocol buffer. |
| 109 | +
|
| 110 | + Returns: |
| 111 | + ~.operations_pb2.Operation: An Operation protobuf object. |
| 112 | + """ |
| 113 | + return operations_pb2.Operation.FromString(payload) |
| 114 | + |
| 115 | + def _set_result_from_operation(self): |
| 116 | + """Set the result or exception from the operation if it is complete.""" |
| 117 | + # This must be done in a lock to prevent the async_future thread |
| 118 | + # and main thread from both executing the completion logic |
| 119 | + # at the same time. |
| 120 | + with self._completion_lock: |
| 121 | + # If the operation isn't complete or if the result has already been |
| 122 | + # set, do not call set_result/set_exception again. |
| 123 | + # Note: self._result_set is set to True in set_result and |
| 124 | + # set_exception, in case those methods are invoked directly. |
| 125 | + if not self._operation.done or self._future.done(): |
| 126 | + return |
| 127 | + |
| 128 | + if self._operation.HasField("response"): |
| 129 | + response = protobuf_helpers.from_any_pb( |
| 130 | + self._result_type, self._operation.response |
| 131 | + ) |
| 132 | + self.set_result(response) |
| 133 | + elif self._operation.HasField("error"): |
| 134 | + exception = exceptions.GoogleAPICallError( |
| 135 | + self._operation.error.message, |
| 136 | + errors=(self._operation.error,), |
| 137 | + response=self._operation, |
| 138 | + ) |
| 139 | + self.set_exception(exception) |
| 140 | + else: |
| 141 | + exception = exceptions.GoogleAPICallError( |
| 142 | + "Unexpected state: Long-running operation had neither " |
| 143 | + "response nor error set." |
| 144 | + ) |
| 145 | + self.set_exception(exception) |
| 146 | + |
| 147 | + async def _refresh_and_update(self, retry=async_future.DEFAULT_RETRY): |
| 148 | + """Refresh the operation and update the result if needed. |
| 149 | +
|
| 150 | + Args: |
| 151 | + retry (google.api_core.retry.Retry): (Optional) How to retry the RPC. |
| 152 | + """ |
| 153 | + # If the currently cached operation is done, no need to make another |
| 154 | + # RPC as it will not change once done. |
| 155 | + if not self._operation.done: |
| 156 | + self._operation = await self._refresh(retry=retry) |
| 157 | + self._set_result_from_operation() |
| 158 | + |
| 159 | + async def done(self, retry=async_future.DEFAULT_RETRY): |
| 160 | + """Checks to see if the operation is complete. |
| 161 | +
|
| 162 | + Args: |
| 163 | + retry (google.api_core.retry.Retry): (Optional) How to retry the RPC. |
| 164 | +
|
| 165 | + Returns: |
| 166 | + bool: True if the operation is complete, False otherwise. |
| 167 | + """ |
| 168 | + await self._refresh_and_update(retry) |
| 169 | + return self._operation.done |
| 170 | + |
| 171 | + async def cancel(self): |
| 172 | + """Attempt to cancel the operation. |
| 173 | +
|
| 174 | + Returns: |
| 175 | + bool: True if the cancel RPC was made, False if the operation is |
| 176 | + already complete. |
| 177 | + """ |
| 178 | + result = await self.done() |
| 179 | + if result: |
| 180 | + return False |
| 181 | + else: |
| 182 | + await self._cancel() |
| 183 | + return True |
| 184 | + |
| 185 | + async def cancelled(self): |
| 186 | + """True if the operation was cancelled.""" |
| 187 | + await self._refresh_and_update() |
| 188 | + return ( |
| 189 | + self._operation.HasField("error") |
| 190 | + and self._operation.error.code == code_pb2.CANCELLED |
| 191 | + ) |
| 192 | + |
| 193 | + |
| 194 | +def from_gapic(operation, operations_client, result_type, **kwargs): |
| 195 | + """Create an operation future from a gapic client. |
| 196 | +
|
| 197 | + This interacts with the long-running operations `service`_ (specific |
| 198 | + to a given API) via a gapic client. |
| 199 | +
|
| 200 | + .. _service: https://github.com/googleapis/googleapis/blob/\ |
| 201 | + 050400df0fdb16f63b63e9dee53819044bffc857/\ |
| 202 | + google/longrunning/operations.proto#L38 |
| 203 | +
|
| 204 | + Args: |
| 205 | + operation (google.longrunning.operations_pb2.Operation): The operation. |
| 206 | + operations_client (google.api_core.operations_v1.OperationsClient): |
| 207 | + The operations client. |
| 208 | + result_type (:func:`type`): The protobuf result type. |
| 209 | + kwargs: Keyword args passed into the :class:`Operation` constructor. |
| 210 | +
|
| 211 | + Returns: |
| 212 | + ~.api_core.operation.Operation: The operation future to track the given |
| 213 | + operation. |
| 214 | + """ |
| 215 | + refresh = functools.partial(operations_client.get_operation, operation.name) |
| 216 | + cancel = functools.partial(operations_client.cancel_operation, operation.name) |
| 217 | + return AsyncOperation(operation, refresh, cancel, result_type, **kwargs) |
0 commit comments