Merge pull request 'Moved to new category type, overhauled sending notifications' (#3) from feature/new-category-type into main
Generate openapi.json / generate-docs (push) Successful in 38s
pip-audit / audit (push) Successful in 34s
pip-audit / notify (push) Skipped

Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
florian committed 2026-11-16 17:39:27 +00:00
commit edb67f3e18
2 files changed
+23 -45

No files matched your search

+2 -1
View File
@@ -13,7 +13,8 @@ httpx==0.28.1
idna==3.20 idna==3.20
lib-db-module==0.4.0 lib-db-module==0.4.0
lib-secret-manager==0.3.6 lib-secret-manager==0.3.6
lib-send-notifications==0.2.2 lib-send-notifications==0.3.0
lib-shared-models==0.2.0
lib-service-auth==0.1.0 lib-service-auth==0.1.0
msgspec==0.22.0 msgspec==0.22.0
mysql-connector-python==26.10.0 mysql-connector-python==26.10.0
+21 -44
View File
@@ -1,13 +1,14 @@
from fastapi import APIRouter, Depends, HTTPException, Request from fastapi import APIRouter, Depends, HTTPException, Request
from seadex import SeaDexEntry from seadex import SeaDexEntry
from shared_models import Category, Message
from auth_config import Caller, auth from auth_config import Caller, auth
from db_module import ( from db_module import (
get_db, get_db,
) )
from seadex_client import check_for_update from seadex_client import check_for_update
from send_notifications import NotificationError, NotificationUnavailableError from send_notifications import send_to_users
from simple_logger_handler import setup_logger from simple_logger_handler import setup_logger
logger = setup_logger(__name__) logger = setup_logger(__name__)
@@ -41,10 +42,7 @@ def check_seadex(request:Request, db=Depends(get_db)):
if current == last_updated: if current == last_updated:
continue continue
logger.info( logger.info("[FastAPI] Anime: %s updated, Anilist ID: %s",anime_name,anilist_id,)
"[FastAPI] Anime: %s updated, Anilist ID: %s",
anime_name,anilist_id,
)
with db.cursor() as cursor: with db.cursor() as cursor:
cursor.execute( cursor.execute(
"SELECT user_id, custom_name " "SELECT user_id, custom_name "
@@ -54,51 +52,30 @@ def check_seadex(request:Request, db=Depends(get_db)):
) )
subscribers = cursor.fetchall() subscribers = cursor.fetchall()
sent = 0 notifications = []
failed = 0
for user_id, custom_name in subscribers: for user_id, custom_name in subscribers:
try: notifications.append((user_id,Message(
request.app.state.notifier.send( title=custom_name or anime_name,
title=custom_name or anime_name, body="SeaDex entry updated",
body="SeaDex entry updated", link=f"https://seadex.moe/{anilist_id}/",
link=f"https://seadex.moe/{anilist_id}/", category=Category.WATCHING,
user_id=user_id, )))
) _,failed = send_to_users(request.app.state.notifier,notifications)
sent += 1
except NotificationUnavailableError:
failed = len(subscribers) - sent
logger.error("[FastAPI] Notifications backend unavailable, skipping "
"%s remaining user(s) for Anime: %s",
failed, anime_name,
)
break
except NotificationError as e:
failed += 1
logger.error(
"[FastAPI] Failed to send notification to user_id=%s for Anime: %s, status=%s",
user_id, anime_name, getattr(e, 'status_code',None),
)
if subscribers and sent == 0:
results["errors"] += 1
logger.error("[FastAPI] All notifications failed for Anime: %s, will retry later", anime_name)
continue
with db.cursor() as cursor:
cursor.execute(
"UPDATE seadex_state SET last_updated = %s WHERE id = %s",
(current, seadex_state_id),
)
db.commit()
logger.debug("[FastAPI] Notification sent and database updated for Anime: %s", anime_name)
results["new"] += 1 results["new"] += 1
results["errors"] += failed
if failed == 0:
with db.cursor() as cursor:
cursor.execute(
"UPDATE seadex_state SET last_updated = %s WHERE id = %s",
(current, seadex_state_id),
)
db.commit()
logger.debug("[FastAPI] Notification sent and database updated for Anime: %s", anime_name)
except Exception: except Exception:
db.rollback() db.rollback()
results["errors"] += 1 results["errors"] += 1
logger.exception( logger.exception("[FastAPI] Failed for id: %s Anilist: %s",seadex_state_id, anilist_id,)
"[FastAPI] Failed for id: %s Anilist: %s",
seadex_state_id, anilist_id,
)
if results["errors"]: if results["errors"]:
logger.warning("[FastAPI] SeaDex check finished with errors: %s", results) logger.warning("[FastAPI] SeaDex check finished with errors: %s", results)