Moved to new category type, overhauled sending notifications #3
No files matched your search
+2
-1
@@ -13,7 +13,8 @@ httpx==0.28.1
|
||||
idna==3.20
|
||||
lib-db-module==0.4.0
|
||||
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
|
||||
msgspec==0.22.0
|
||||
mysql-connector-python==26.10.0
|
||||
|
||||
+21
-44
@@ -1,13 +1,14 @@
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from seadex import SeaDexEntry
|
||||
from shared_models import Category, Message
|
||||
|
||||
from auth_config import Caller, auth
|
||||
from db_module import (
|
||||
get_db,
|
||||
)
|
||||
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
|
||||
|
||||
logger = setup_logger(__name__)
|
||||
@@ -41,10 +42,7 @@ def check_seadex(request:Request, db=Depends(get_db)):
|
||||
if current == last_updated:
|
||||
continue
|
||||
|
||||
logger.info(
|
||||
"[FastAPI] Anime: %s updated, Anilist ID: %s",
|
||||
anime_name,anilist_id,
|
||||
)
|
||||
logger.info("[FastAPI] Anime: %s updated, Anilist ID: %s",anime_name,anilist_id,)
|
||||
with db.cursor() as cursor:
|
||||
cursor.execute(
|
||||
"SELECT user_id, custom_name "
|
||||
@@ -54,51 +52,30 @@ def check_seadex(request:Request, db=Depends(get_db)):
|
||||
)
|
||||
subscribers = cursor.fetchall()
|
||||
|
||||
sent = 0
|
||||
failed = 0
|
||||
notifications = []
|
||||
for user_id, custom_name in subscribers:
|
||||
try:
|
||||
request.app.state.notifier.send(
|
||||
title=custom_name or anime_name,
|
||||
body="SeaDex entry updated",
|
||||
link=f"https://seadex.moe/{anilist_id}/",
|
||||
user_id=user_id,
|
||||
)
|
||||
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)
|
||||
notifications.append((user_id,Message(
|
||||
title=custom_name or anime_name,
|
||||
body="SeaDex entry updated",
|
||||
link=f"https://seadex.moe/{anilist_id}/",
|
||||
category=Category.WATCHING,
|
||||
)))
|
||||
_,failed = send_to_users(request.app.state.notifier,notifications)
|
||||
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:
|
||||
db.rollback()
|
||||
results["errors"] += 1
|
||||
logger.exception(
|
||||
"[FastAPI] Failed for id: %s Anilist: %s",
|
||||
seadex_state_id, anilist_id,
|
||||
)
|
||||
logger.exception("[FastAPI] Failed for id: %s Anilist: %s",seadex_state_id, anilist_id,)
|
||||
|
||||
if results["errors"]:
|
||||
logger.warning("[FastAPI] SeaDex check finished with errors: %s", results)
|
||||
|
||||
Reference in new issue
Block a user