Upgraded modules

This commit is contained in:
florian committed 2026-10-18 11:32:25 +02:00
1 parent f48ac98638
commit 7dda7ffe07
4 files changed
+56 -113

No files matched your search

+29 -20
View File
@@ -8,22 +8,21 @@ from db_module import close_connection_pool, create_connection_pool, get_db, sta
from feed_handler import grab_gitea_release_information, grab_royal_road_chapter_information
from metrics_server import REQUEST_COUNTER, metrics_api
from secret_manager import cleanup_secret_files
from send_notification import send_notification
from send_notifications import NotificationClient
from simple_logger_handler import LOG_LEVEL, setup_logger
from uvicorn_logger_config import LOGGING_CONFIG
logger = setup_logger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info("[App] Starting application...")
logger.info("[DB] Creating MySQL connection pool...")
logger.info("[FastAPI] Starting application...")
app.state.notifier = NotificationClient()
logger.info("[FastAPI] Creating MySQL connection pool...")
create_connection_pool("royalRoadPool")
start_healthcheck_thread()
logger.info("[DB] MySQL healthcheck thread started.")
logger.info("[FastAPI] MySQL healthcheck thread started.")
SECRET_PATHS = frozenset({
"/etc/secrets/api_key",
@@ -33,9 +32,9 @@ async def lifespan(app: FastAPI):
cleanup_secret_files(SECRET_PATHS)
yield
logger.info("[App] Closing MySQL connection pool...")
logger.info("[FastAPI] Closing MySQL connection pool...")
close_connection_pool()
logger.info("[App] Shutdown complete.")
logger.info("[FastAPI] Shutdown complete.")
api = FastAPI(
title="Docker Repository Query",
@@ -46,22 +45,22 @@ api = FastAPI(
@api.middleware("http")
async def prometheus_middleware(request: Request, call_next):
logger.debug(f"[Metrics] Incoming request: {request.method} {request.url.path}")
logger.debug(f"[FastAPI] 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}")
logger.debug(f"[FastAPI] Request processed with status {status}")
except Exception as e:
logger.error(f"[Metrics] Exception occurred: {e}", exc_info=True)
logger.error(f"[FastAPI] 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}]")
logger.debug(f"[FastAPI] Counter incremented for {request.method} {request.url.path} [{status}]")
return response
@api.get("/gitea")
def get_release_information(db = Depends(get_db)):
def get_release_information(request: Request, db = Depends(get_db)):
"""
Checks for new Gitea releases and updates the database accordingly.
@@ -90,14 +89,19 @@ def get_release_information(db = Depends(get_db)):
for feed_id, rss_feed, feed_name, last_entry in feeds:
results["checked"] += 1
try:
title, link = grab_gitea_release_information(rss_feed)
release_title, link = grab_gitea_release_information(rss_feed)
logger.debug(f"[FastAPI] ID:{feed_id},RSS Feed {rss_feed}: db={last_entry}, online={link}")
if link == last_entry:
continue
logger.info(f"[FastAPI] Release change for ID {feed_id}: {title}")
send_notification(feed_name, title, link)
logger.info(f"[FastAPI] Release change for ID {feed_id}: {release_title}")
request.app.state.notifier.send(
title=feed_name,
body=release_title,
link=link,
category="utility"
)
cursor.execute(
"UPDATE rss_feeds SET last_entry = %s WHERE id = %s",
(link, feed_id),
@@ -122,7 +126,7 @@ def get_release_information(db = Depends(get_db)):
return results
@api.get("/royalroad")
def get_chapters(db = Depends(get_db)):
def get_chapters(request: Request, db = Depends(get_db)):
"""
Checks for new Royalroad chapters and updates the database accordingly.
@@ -158,7 +162,12 @@ def get_chapters(db = Depends(get_db)):
continue
logger.info(f"[FastAPI] New chapter detected for story ID {feed_id}: {story_title}")
send_notification(story_title, chapter_title, link)
request.app.state.notifier.send(
title=story_title,
body=chapter_title,
link=link,
category="royal-road"
)
cursor.execute(
"UPDATE rss_feeds SET last_entry = %s WHERE id = %s",
(online_latest_entry, feed_id),
@@ -184,8 +193,8 @@ def get_chapters(db = Depends(get_db)):
async def start_servers():
logger.info("[Server] Starting main API (port 5000) and metrics server (port 9000)...")
config_main = uvicorn.Config(api, host="0.0.0.0", port=5000, log_level=LOG_LEVEL.lower(), log_config=LOGGING_CONFIG)
config_metrics = uvicorn.Config(metrics_api, host="0.0.0.0", port=9000, log_level=LOG_LEVEL.lower(), log_config=LOGGING_CONFIG)
config_main = uvicorn.Config(api, host="0.0.0.0", port=5000, log_level=LOG_LEVEL.lower(), log_config=None)
config_metrics = uvicorn.Config(metrics_api, host="0.0.0.0", port=9000, log_level=LOG_LEVEL.lower(), log_config=None)
server_main = uvicorn.Server(config_main)
server_metrics = uvicorn.Server(config_metrics)