Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit b068845

Browse files
Takashi Matsuobusunkim96
Takashi Matsuo
andauthored
fix(monitoring): also retry upon DeadlineExceeded [(#4202)](GoogleCloudPlatform/python-docs-samples#4202)
fixes #4180 Co-authored-by: Bu Sun Kim <[email protected]>
1 parent 01657d6 commit b068845

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

samples/snippets/v3/alerts-client/snippets_test.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import time
2020

2121
from google.api_core.exceptions import Aborted
22+
from google.api_core.exceptions import DeadlineExceeded
2223
from google.api_core.exceptions import NotFound
2324
from google.api_core.exceptions import ServiceUnavailable
2425
from google.cloud import monitoring_v3
@@ -38,12 +39,13 @@ def random_name(length):
3839
[random.choice(string.ascii_lowercase) for i in range(length)])
3940

4041

41-
def retry_if_aborted(exception):
42-
return isinstance(exception, (Aborted, ServiceUnavailable))
42+
def retry_on_exceptions(exception):
43+
return isinstance(
44+
exception, (Aborted, ServiceUnavailable, DeadlineExceeded))
4345

4446

4547
def delay_on_aborted(err, *args):
46-
if retry_if_aborted(err[1]):
48+
if retry_on_exceptions(err[1]):
4749
# add randomness for avoiding continuous conflict
4850
time.sleep(5 + (random.randint(0, 9) * 0.1))
4951
return True
@@ -64,7 +66,8 @@ def __init__(self):
6466

6567
def __enter__(self):
6668
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000,
67-
stop_max_attempt_number=10, retry_on_exception=retry_if_aborted)
69+
stop_max_attempt_number=10,
70+
retry_on_exception=retry_on_exceptions)
6871
def setup():
6972
# Create a policy.
7073
policy = monitoring_v3.types.alert_pb2.AlertPolicy()
@@ -89,7 +92,8 @@ def setup():
8992
def __exit__(self, type, value, traceback):
9093
# Delete the policy and channel we created.
9194
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000,
92-
stop_max_attempt_number=10, retry_on_exception=retry_if_aborted)
95+
stop_max_attempt_number=10,
96+
retry_on_exception=retry_on_exceptions)
9397
def teardown():
9498
try:
9599
self.alert_policy_client.delete_alert_policy(

0 commit comments

Comments
 (0)