fix: pace failed calendar refresh retries
ci / gitleaks (push) Successful in 7s
ci / docker (push) Successful in 3m11s

This commit is contained in:
2026-09-21 15:38:52 +08:00
parent 97c1bd4e87
commit a68d6ecc5e
2 changed files with 66 additions and 5 deletions
+11 -5
View File
@@ -10,7 +10,7 @@ from sqlalchemy import or_, select
from sqlalchemy.ext.asyncio import AsyncSession
from . import calendar as calendar_service
from .models import CalendarSubscription, utcnow
from .models import CalendarSubscription
logger = logging.getLogger(__name__)
DEFAULT_REFRESH_INTERVAL = timedelta(minutes=15)
@@ -22,7 +22,9 @@ async def refresh_subscription_cache(
row: CalendarSubscription,
*,
fail_without_cache: bool = True,
attempted_at: datetime | None = None,
) -> bool:
attempt_time = attempted_at or datetime.now(UTC)
requested_url = row.url
requested_version = getattr(row, "updated_at", None)
try:
@@ -51,7 +53,8 @@ async def refresh_subscription_cache(
row.ics_cache = result.content.decode("utf-8-sig")
row.etag = result.etag
row.last_modified = result.last_modified
row.refreshed_at = utcnow()
row.refreshed_at = attempt_time
row.updated_at = attempt_time
row.last_error = None
await db.commit()
return True
@@ -61,6 +64,7 @@ async def refresh_subscription_cache(
await db.rollback()
raise HTTPException(502, error) from exc
row.last_error = error
row.updated_at = attempt_time
await db.commit()
return False
@@ -76,13 +80,15 @@ async def refresh_due_subscriptions(
rows = (await db.scalars(select(CalendarSubscription).where(
CalendarSubscription.enabled.is_(True),
or_(
CalendarSubscription.refreshed_at.is_(None),
CalendarSubscription.refreshed_at <= cutoff,
CalendarSubscription.updated_at.is_(None),
CalendarSubscription.updated_at <= cutoff,
),
).order_by(CalendarSubscription.refreshed_at, CalendarSubscription.created_at))).all()
for row in rows:
try:
await refresh_subscription_cache(db, row, fail_without_cache=False)
await refresh_subscription_cache(
db, row, fail_without_cache=False, attempted_at=current
)
except Exception:
await db.rollback()
logger.exception("Unexpected calendar refresh failure", extra={"subscription_id": str(row.id)})