Skip to content

Commit b9cc737

Browse files
committed
feat(providers): added retry mechanism to google chat provider to overcome rate limiting
1 parent 8cbf56d commit b9cc737

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

keep/providers/google_chat_provider/google_chat_provider.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,21 @@ def _notify(self, message="", **kwargs: dict):
6969
raise ProviderException("Message is required")
7070

7171
def __send_message(url, body, headers, retries=3):
72-
for _ in range(retries):
72+
for attempt in range(retries):
7373
try:
7474
resp = requests.post(url, json=body, headers=headers)
7575
if resp.status_code == http.HTTPStatus.OK:
7676
return resp
77+
78+
self.logger.warning(f"Attempt {attempt + 1} failed with status code {resp.status_code}")
79+
7780
except requests.exceptions.RequestException as e:
78-
self.logger.error(f"Failed to send message to Google Chat: {e}")
79-
time.sleep(1)
81+
self.logger.error(f"Attempt {attempt + 1} failed: {e}")
82+
83+
if attempt < retries - 1:
84+
time.sleep(1)
85+
86+
raise requests.exceptions.RequestException(f"Failed to notify message after {retries} attempts")
8087

8188
payload = {
8289
"text": message,
@@ -85,11 +92,8 @@ def __send_message(url, body, headers, retries=3):
8592
request_headers = {"Content-Type": "application/json; charset=UTF-8"}
8693

8794
response = __send_message(webhook_url, body=payload, headers=request_headers)
88-
89-
if not response.ok:
90-
raise ProviderException(
91-
f"Failed to notify message to Google Chat: {response.text}"
92-
)
95+
if response.status_code != http.HTTPStatus.OK:
96+
raise ProviderException(f"Failed to notify message to Google Chat: {response.text}")
9397

9498
self.logger.debug("Alert message sent to Google Chat successfully")
9599
return "Alert message sent to Google Chat successfully"

0 commit comments

Comments
 (0)