Unified logging behaviour and added exponential backoff

- Logger doesn't start with log level DEBUG by default, instead reads a environment variable
- Secret handler raises exceptions instead of using the module os to exit
- Added extensive debug logging
- Added detailed function descriptions
- Added exponential backoff when parsing the RSS feed
This commit is contained in:
florian committed 2025-10-17 15:12:02 +02:00
1 parent 14f974f4ed
commit abc894869f
8 files changed
+217 -107

No files matched your search

+34 -22
View File
@@ -12,43 +12,55 @@ api_key= return_credentials("/etc/secrets/api_key")
logger = setup_logger(__name__)
def send_notification(title:str,
chapter:int,
link:str,
max_retries: int = 5,
timeout: int = 5
):
def send_notification(title: str,chapter: int,link: str,max_retries: int = 5,timeout: int = 5):
"""
Sends a notification to the internal backend service when a new Royalroad chapter is released.
Parameters:
title: Name of the story.
chapter: Latest chapter number.
link: Direct link to the new chapter.
"""
headers = {
"X-API-Key-Internal": api_key,
"Content-Type": "application/json"
}
}
data = {
"receipent_user_id": 1,
"message": {
"title": title,
"info": f"Chapter {chapter} has been released",
"link": link
}}
"receipent_user_id": 1,
"message": {
"title": title,
"info": f"Chapter {chapter} has been released",
"link": link,
"category":"royal-road"
}
}
logger.debug(f"[Notify] Preparing to send notification: title='{title}', chapter={chapter}, link='{link}'")
with requests.Session() as session:
for attempt in range(1,max_retries+1):
for attempt in range(1, max_retries + 1):
try:
logger.debug(f"[Notify] Sending request to backend (attempt {attempt}/{max_retries})")
response = session.post(backend_api_url, headers=headers, json=data, timeout=timeout)
response.raise_for_status()
logger.info("Notification sent successfully")
logger.info(f"[Notify] Notification sent successfully for '{title}' (chapter {chapter})")
return response.text
except (Timeout,ConnectionError) as e:
logger.warning(f"Attempt {attempt}/{max_retries} failed: {type(e).__name__}")
except (Timeout, ConnectionError) as e:
logger.warning(f"[Notify] Attempt {attempt}/{max_retries} failed: {type(e).__name__}")
if attempt == max_retries:
logger.error(f"[Notify] All retry attempts failed for '{title}'")
raise HTTPException(status_code=503, detail=f"Notification service unavailable: {type(e).__name__}")
time.sleep(2 ** (attempt - 1))
sleep_time = 2 ** (attempt - 1)
logger.debug(f"[Notify] Retrying in {sleep_time} seconds...")
time.sleep(sleep_time)
except HTTPError as e:
logger.error(f"HTTP {e.response.status_code}: {e.response.text}")
logger.error(f"[Notify] HTTP {e.response.status_code}: {e.response.text}")
raise HTTPException(status_code=e.response.status_code, detail=e.response.text)
except RequestException as e:
logger.error(f"Unexpected request failure: {e}")
logger.error(f"[Notify] Unexpected request failure: {e}")
raise HTTPException(status_code=500, detail=f"Request failed: {str(e)}")