Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
timeout and retry for slack
Browse files
main.py
CHANGED
|
@@ -3,6 +3,7 @@ import os
|
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
import logging
|
|
|
|
| 6 |
|
| 7 |
# Set up logging
|
| 8 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
@@ -17,19 +18,76 @@ MODEL_CATALOG_WEBHOOK_SECRET = os.environ.get("MODEL_CATALOG_WEBHOOK_SECRET")
|
|
| 17 |
SIMSHIP_WEBHOOK = os.environ.get("SIMSHIP_WEBHOOK")
|
| 18 |
SIMSHIP_WEBHOOK_SECRET = os.environ.get("SIMSHIP_WEBHOOK_SECRET")
|
| 19 |
|
| 20 |
-
def send_slack_message(message: str):
|
| 21 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
if not SLACK_WEBHOOK_URL:
|
| 23 |
logger.warning(f"No Slack webhook URL configured. Message: {message}")
|
| 24 |
return
|
| 25 |
|
| 26 |
payload = {"text": message}
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def process_model_catalog_webhook(data: dict):
|
| 35 |
"""
|
|
|
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
import logging
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
# Set up logging
|
| 9 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
| 18 |
SIMSHIP_WEBHOOK = os.environ.get("SIMSHIP_WEBHOOK")
|
| 19 |
SIMSHIP_WEBHOOK_SECRET = os.environ.get("SIMSHIP_WEBHOOK_SECRET")
|
| 20 |
|
| 21 |
+
def send_slack_message(message: str, max_retries: int = 3, timeout: int = 10, retry_delay: int = 2):
|
| 22 |
+
"""
|
| 23 |
+
Send a message to Slack using webhook URL with timeout and retry mechanism
|
| 24 |
+
|
| 25 |
+
Args:
|
| 26 |
+
message: The message to send to Slack
|
| 27 |
+
max_retries: Maximum number of retry attempts (default: 3)
|
| 28 |
+
timeout: Request timeout in seconds (default: 30)
|
| 29 |
+
retry_delay: Delay between retries in seconds (default: 2)
|
| 30 |
+
"""
|
| 31 |
if not SLACK_WEBHOOK_URL:
|
| 32 |
logger.warning(f"No Slack webhook URL configured. Message: {message}")
|
| 33 |
return
|
| 34 |
|
| 35 |
payload = {"text": message}
|
| 36 |
+
|
| 37 |
+
for attempt in range(max_retries + 1): # +1 for initial attempt
|
| 38 |
+
try:
|
| 39 |
+
logger.info(f"Sending Slack message (attempt {attempt + 1}/{max_retries + 1})")
|
| 40 |
+
response = requests.post(
|
| 41 |
+
SLACK_WEBHOOK_URL,
|
| 42 |
+
json=payload,
|
| 43 |
+
timeout=timeout
|
| 44 |
+
)
|
| 45 |
+
response.raise_for_status()
|
| 46 |
+
logger.info(f"Slack message sent successfully: {message}")
|
| 47 |
+
return # Success, exit the function
|
| 48 |
+
|
| 49 |
+
except requests.exceptions.Timeout as e:
|
| 50 |
+
logger.warning(f"Slack request timeout on attempt {attempt + 1}: {e}")
|
| 51 |
+
if attempt < max_retries:
|
| 52 |
+
logger.info(f"Retrying in {retry_delay} seconds...")
|
| 53 |
+
time.sleep(retry_delay)
|
| 54 |
+
else:
|
| 55 |
+
logger.error(f"Failed to send Slack message after {max_retries + 1} attempts due to timeout")
|
| 56 |
+
|
| 57 |
+
except requests.exceptions.HTTPError as e:
|
| 58 |
+
# For HTTP errors, check if it's worth retrying
|
| 59 |
+
if hasattr(e.response, 'status_code'):
|
| 60 |
+
status_code = e.response.status_code
|
| 61 |
+
if status_code in [429, 500, 502, 503, 504]: # Retry-able errors
|
| 62 |
+
logger.warning(f"Slack HTTP error {status_code} on attempt {attempt + 1}: {e}")
|
| 63 |
+
if attempt < max_retries:
|
| 64 |
+
logger.info(f"Retrying in {retry_delay} seconds...")
|
| 65 |
+
time.sleep(retry_delay)
|
| 66 |
+
else:
|
| 67 |
+
logger.error(f"Failed to send Slack message after {max_retries + 1} attempts due to HTTP error {status_code}")
|
| 68 |
+
else:
|
| 69 |
+
# Non-retryable HTTP error (4xx except 429)
|
| 70 |
+
logger.error(f"Failed to send Slack message due to non-retryable HTTP error {status_code}: {e}")
|
| 71 |
+
break
|
| 72 |
+
else:
|
| 73 |
+
logger.error(f"Failed to send Slack message due to HTTP error: {e}")
|
| 74 |
+
break
|
| 75 |
+
|
| 76 |
+
except requests.exceptions.ConnectionError as e:
|
| 77 |
+
logger.warning(f"Slack connection error on attempt {attempt + 1}: {e}")
|
| 78 |
+
if attempt < max_retries:
|
| 79 |
+
logger.info(f"Retrying in {retry_delay} seconds...")
|
| 80 |
+
time.sleep(retry_delay)
|
| 81 |
+
else:
|
| 82 |
+
logger.error(f"Failed to send Slack message after {max_retries + 1} attempts due to connection error")
|
| 83 |
+
|
| 84 |
+
except requests.exceptions.RequestException as e:
|
| 85 |
+
logger.warning(f"Slack request error on attempt {attempt + 1}: {e}")
|
| 86 |
+
if attempt < max_retries:
|
| 87 |
+
logger.info(f"Retrying in {retry_delay} seconds...")
|
| 88 |
+
time.sleep(retry_delay)
|
| 89 |
+
else:
|
| 90 |
+
logger.error(f"Failed to send Slack message after {max_retries + 1} attempts due to request error")
|
| 91 |
|
| 92 |
def process_model_catalog_webhook(data: dict):
|
| 93 |
"""
|