- 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
109 lines
3.9 KiB
Python
109 lines
3.9 KiB
Python
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, LOG_LEVEL
|
|
from feed_handler import grab_latest_chapter_information
|
|
from send_notification import send_notification
|
|
from metrics_server import REQUEST_COUNTER
|
|
import asyncio
|
|
|
|
|
|
logger = setup_logger(__name__)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
logger.info("[App] Starting application...")
|
|
|
|
logger.info("[DB] Creating MySQL connection pool...")
|
|
create_connection_pool()
|
|
|
|
start_healthcheck_thread()
|
|
logger.info("[DB] MySQL healthcheck thread started.")
|
|
|
|
yield
|
|
logger.info("[App] Closing MySQL connection pool...")
|
|
close_connection_pool()
|
|
logger.info("[App] Shutdown complete.")
|
|
|
|
api = FastAPI(
|
|
title="Docker Repository Query",
|
|
description="Queries Dockerhub and GHCR for new docker images",
|
|
version="1.0.0",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
@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("/royalroad")
|
|
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")
|
|
|
|
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:
|
|
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(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():
|
|
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())
|