19
19
import time
20
20
21
21
from google .api_core .exceptions import Aborted
22
+ from google .api_core .exceptions import DeadlineExceeded
22
23
from google .api_core .exceptions import NotFound
23
24
from google .api_core .exceptions import ServiceUnavailable
24
25
from google .cloud import monitoring_v3
@@ -38,12 +39,13 @@ def random_name(length):
38
39
[random .choice (string .ascii_lowercase ) for i in range (length )])
39
40
40
41
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 ))
43
45
44
46
45
47
def delay_on_aborted (err , * args ):
46
- if retry_if_aborted (err [1 ]):
48
+ if retry_on_exceptions (err [1 ]):
47
49
# add randomness for avoiding continuous conflict
48
50
time .sleep (5 + (random .randint (0 , 9 ) * 0.1 ))
49
51
return True
@@ -64,7 +66,8 @@ def __init__(self):
64
66
65
67
def __enter__ (self ):
66
68
@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 )
68
71
def setup ():
69
72
# Create a policy.
70
73
policy = monitoring_v3 .types .alert_pb2 .AlertPolicy ()
@@ -89,7 +92,8 @@ def setup():
89
92
def __exit__ (self , type , value , traceback ):
90
93
# Delete the policy and channel we created.
91
94
@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 )
93
97
def teardown ():
94
98
try :
95
99
self .alert_policy_client .delete_alert_policy (
0 commit comments