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

Commit

Permalink
fix(monitoring): also retry upon DeadlineExceeded [(#4202)](GoogleClo…
Browse files Browse the repository at this point in the history
…udPlatform/python-docs-samples#4202)

fixes #4180

Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com>
  • Loading branch information
Takashi Matsuo and busunkim96 authored Jun 30, 2020
1 parent 01657d6 commit b068845
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions samples/snippets/v3/alerts-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import time

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


def retry_if_aborted(exception):
return isinstance(exception, (Aborted, ServiceUnavailable))
def retry_on_exceptions(exception):
return isinstance(
exception, (Aborted, ServiceUnavailable, DeadlineExceeded))


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

def __enter__(self):
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000,
stop_max_attempt_number=10, retry_on_exception=retry_if_aborted)
stop_max_attempt_number=10,
retry_on_exception=retry_on_exceptions)
def setup():
# Create a policy.
policy = monitoring_v3.types.alert_pb2.AlertPolicy()
Expand All @@ -89,7 +92,8 @@ def setup():
def __exit__(self, type, value, traceback):
# Delete the policy and channel we created.
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000,
stop_max_attempt_number=10, retry_on_exception=retry_if_aborted)
stop_max_attempt_number=10,
retry_on_exception=retry_on_exceptions)
def teardown():
try:
self.alert_policy_client.delete_alert_policy(
Expand Down

0 comments on commit b068845

Please sign in to comment.