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:
1 parent
14f974f4ed
commit
abc894869f
8 files changed
+217
-107
No files matched your search
+48
-24
@@ -2,7 +2,7 @@ from fastapi import FastAPI, Depends, HTTPException, Response, Request
|
||||
import uvicorn
|
||||
from contextlib import asynccontextmanager
|
||||
from db import get_db, create_connection_pool, close_connection_pool, start_healthcheck_thread
|
||||
from logger_handler import setup_logger
|
||||
from logger_handler import setup_logger, LOG_LEVEL
|
||||
from feed_handler import grab_latest_chapter_information
|
||||
from send_notification import send_notification
|
||||
from metrics_server import REQUEST_COUNTER
|
||||
@@ -14,17 +14,18 @@ logger = setup_logger(__name__)
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
logger.info("Starting application...")
|
||||
logger.info("[App] Starting application...")
|
||||
|
||||
logger.info("Creating MySQL connection pool...")
|
||||
logger.info("[DB] Creating MySQL connection pool...")
|
||||
create_connection_pool()
|
||||
|
||||
start_healthcheck_thread()
|
||||
logger.info("MySQL healthcheck thread started.")
|
||||
logger.info("[DB] MySQL healthcheck thread started.")
|
||||
|
||||
yield
|
||||
logger.info("Closing MySQL connection pool...")
|
||||
logger.info("[App] Closing MySQL connection pool...")
|
||||
close_connection_pool()
|
||||
logger.info("[App] Shutdown complete.")
|
||||
|
||||
api = FastAPI(
|
||||
title="Docker Repository Query",
|
||||
@@ -35,50 +36,73 @@ api = FastAPI(
|
||||
|
||||
@api.middleware("http")
|
||||
async def prometheus_middleware(request: Request, call_next):
|
||||
logger.debug(f"[Metrics] Incoming request: {request.method} {request.url.path}")
|
||||
status = 500
|
||||
try:
|
||||
response = await call_next(request)
|
||||
status = response.status_code
|
||||
logger.debug(f"[Metrics] Request processed with status {status}")
|
||||
except Exception:
|
||||
logger.error(f"[Metrics] Exception occurred: {e}", exc_info=True)
|
||||
raise
|
||||
finally:
|
||||
REQUEST_COUNTER.labels(request.method, request.url.path, status).inc()
|
||||
logger.debug(f"[Metrics] Counter incremented for {request.method} {request.url.path} [{status}]")
|
||||
return response
|
||||
|
||||
|
||||
@api.get("/health")
|
||||
def return_health():
|
||||
return Response(status_code=200)
|
||||
|
||||
@api.get("/royalroad")
|
||||
def get_chapters(
|
||||
request: Request,
|
||||
db = Depends(get_db)
|
||||
):
|
||||
try:
|
||||
logger.info("Checking for new Royalroad chapters")
|
||||
def get_chapters(request: Request, db = Depends(get_db)):
|
||||
"""
|
||||
Checks for new Royalroad chapters and updates the database accordingly.
|
||||
|
||||
Behaviour:
|
||||
- Fetches all active stories and their last known chapter number.
|
||||
- Queries Royalroad for each story's latest chapter.
|
||||
- Updates `lastChapter` if a new chapter is found.
|
||||
- Sends a notification for any newly discovered chapters.
|
||||
|
||||
Returns:
|
||||
A JSON object indicating the check status.
|
||||
"""
|
||||
logger.info("[Royalroad] Checking for new chapters...")
|
||||
try:
|
||||
cursor = db.cursor()
|
||||
cursor.execute("SELECT id,royalroadId,lastChapter FROM stories where active=1")
|
||||
for id,royalroadId,last_chapter_db in cursor.fetchall():
|
||||
chapter_number,chapter_link,title = grab_latest_chapter_information(royalroadId)
|
||||
cursor.execute("SELECT id, royalroadId, lastChapter FROM stories WHERE active=1")
|
||||
|
||||
stories = cursor.fetchall()
|
||||
logger.debug(f"[Royalroad] Found {len(stories)} active stories to check.")
|
||||
|
||||
for id, royalroadId, last_chapter_db in stories:
|
||||
chapter_number, chapter_link, story_title = grab_latest_chapter_information(royalroadId)
|
||||
logger.debug(f"[Royalroad] Story {id}: last={last_chapter_db}, latest={chapter_number}")
|
||||
|
||||
if chapter_number > last_chapter_db:
|
||||
cursor.execute("UPDATE stories SET lastChapter = %s WHERE id = %s",
|
||||
(chapter_number, id))
|
||||
logger.info(f"[Royalroad] New chapter detected for story ID {id}: {story_title}")
|
||||
cursor.execute("UPDATE stories SET lastChapter = %s WHERE id = %s", (chapter_number, id))
|
||||
db.commit()
|
||||
send_notification(title,chapter_number,chapter_link)
|
||||
send_notification(story_title, chapter_number, chapter_link)
|
||||
logger.debug(f"[Royalroad] Notification sent for story ID {id}")
|
||||
|
||||
logger.info("[Royalroad] Chapter check completed successfully.")
|
||||
return {"status": "checked"}
|
||||
except ValueError as e:
|
||||
logger.error(f"[Royalroad] Failed to fetch feed {royalroadId}: {e}")
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"[Royalroad] Error during chapter check: {e}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
async def start_servers():
|
||||
config_main = uvicorn.Config("main:api", host="0.0.0.0", port=5000, log_level="info")
|
||||
config_metrics = uvicorn.Config("metrics_server:metrics_api", host="0.0.0.0", port=9000, log_level="info")
|
||||
logger.info("[Server] Starting main API (port 5000) and metrics server (port 9000)...")
|
||||
config_main = uvicorn.Config("main:api", host="0.0.0.0", port=5000, log_level=LOG_LEVEL.lower())
|
||||
config_metrics = uvicorn.Config("metrics_server:metrics_api", host="0.0.0.0", port=9000, log_level=LOG_LEVEL.lower())
|
||||
|
||||
server_main = uvicorn.Server(config_main)
|
||||
server_metrics = uvicorn.Server(config_metrics)
|
||||
|
||||
await asyncio.gather(server_main.serve(), server_metrics.serve())
|
||||
logger.info("[Server] Both servers started successfully.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(start_servers())
|
||||
Reference in new issue
Block a user