Skip to content

Commit 5606ee5

Browse files
author
Jon Wayne Parrott
authored
Add ability to specify retry for Operation and polling.Future. (#4922)
1 parent c0f12e2 commit 5606ee5

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

google/api_core/future/polling.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class _OperationNotComplete(Exception):
2828
pass
2929

3030

31+
RETRY_PREDICATE = retry.if_exception_type(_OperationNotComplete)
32+
DEFAULT_RETRY = retry.Retry(predicate=RETRY_PREDICATE)
33+
34+
3135
class PollingFuture(base.Future):
3236
"""A Future that needs to poll some service to check its status.
3337
@@ -36,9 +40,16 @@ class PollingFuture(base.Future):
3640
3741
.. note: Privacy here is intended to prevent the final class from
3842
overexposing, not to prevent subclasses from accessing methods.
43+
44+
Args:
45+
retry (google.api_core.retry.Retry): The retry configuration used
46+
when polling. This can be used to control how often :meth:`done`
47+
is polled. Regardless of the retry's ``deadline``, it will be
48+
overridden by the ``timeout`` argument to :meth:`result`.
3949
"""
40-
def __init__(self):
50+
def __init__(self, retry=DEFAULT_RETRY):
4151
super(PollingFuture, self).__init__()
52+
self._retry = retry
4253
self._result = None
4354
self._exception = None
4455
self._result_set = False
@@ -77,9 +88,7 @@ def _blocking_poll(self, timeout=None):
7788
if self._result_set:
7889
return
7990

80-
retry_ = retry.Retry(
81-
predicate=retry.if_exception_type(_OperationNotComplete),
82-
deadline=timeout)
91+
retry_ = self._retry.with_deadline(timeout)
8392

8493
try:
8594
retry_(self._done_or_raise)()

google/api_core/operation.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,16 @@ class Operation(polling.PollingFuture):
6161
result.
6262
metadata_type (func:`type`): The protobuf type for the operation's
6363
metadata.
64+
retry (google.api_core.retry.Retry): The retry configuration used
65+
when polling. This can be used to control how often :meth:`done`
66+
is polled. Regardless of the retry's ``deadline``, it will be
67+
overridden by the ``timeout`` argument to :meth:`result`.
6468
"""
6569

6670
def __init__(
6771
self, operation, refresh, cancel,
68-
result_type, metadata_type=None):
69-
super(Operation, self).__init__()
72+
result_type, metadata_type=None, retry=polling.DEFAULT_RETRY):
73+
super(Operation, self).__init__(retry=retry)
7074
self._operation = operation
7175
self._refresh = refresh
7276
self._cancel = cancel

0 commit comments

Comments
 (0)