robust charset decoding + retry-failed endpoint

- fix: fetch_mail crashed on MIME pseudo-encodings like 'unknown-8bit',
  'x-unknown', '8bit'. New _safe_decode helper maps those (and any
  LookupError from unknown codecs) to latin-1, which never fails on
  8-bit input. Used in _extract_body and _decode_header_value.
- Consequence of the crash: scheduler marked the affected UIDs as
  processed to avoid retry loops, so those mails never got sorted
  even after the underlying issue was fixable.
- feat: POST /api/filters/retry-failed drops the ProcessedMail markers
  for UIDs that appear in "Fehler beim Abrufen" error logs, so the
  next poll re-evaluates them. Reachable via a button in the log UI
  ("Fehlgeschlagene neu einlesen"), optionally scoped per account.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-09-01 11:32:54 +02:00
co-authored by Claude Opus 4.7
parent e4669cfccd
commit 54183fdc3a
3 changed files with 74 additions and 6 deletions
+39 -1
View File
@@ -5,7 +5,15 @@ from sqlalchemy.orm import Session
import logging
from app.database import get_db
from app.models.db_models import Account, FilterAction, FilterCondition, FilterRule, ProcessedMail
from app.models.db_models import (
Account,
FilterAction,
FilterCondition,
FilterLog,
FilterRule,
LogLevel,
ProcessedMail,
)
from app.schemas.schemas import FilterRuleCreate, FilterRuleResponse, FilterRuleUpdate
logger = logging.getLogger(__name__)
@@ -25,6 +33,36 @@ def _reset_processed_for_rule(db: Session, rule_id: int) -> int:
router = APIRouter(prefix="/api/filters", tags=["filters"])
@router.post("/retry-failed")
def retry_failed_fetches(account_id: int | None = None, db: Session = Depends(get_db)):
"""Löscht ProcessedMail-Einträge für UIDs, für die es einen ERROR-Log
mit 'Fehler beim Abrufen' gibt — damit der Scheduler sie beim nächsten
Poll neu versucht (z.B. nach einem Codec-Fix)."""
log_q = db.query(FilterLog.mail_uid, FilterLog.account_id).filter(
FilterLog.level == LogLevel.ERROR,
FilterLog.message.like("Fehler beim Abrufen%"),
FilterLog.mail_uid.isnot(None),
)
if account_id is not None:
log_q = log_q.filter(FilterLog.account_id == account_id)
pairs = {(row[0], row[1]) for row in log_q.all()}
if not pairs:
return {"reset": 0, "unique_uids": 0}
total = 0
for uid, acc_id in pairs:
q = db.query(ProcessedMail).filter(ProcessedMail.mail_uid == uid)
if acc_id is not None:
q = q.filter(ProcessedMail.account_id == acc_id)
total += q.delete(synchronize_session=False)
db.commit()
logger.info(
"Retry-Failed: %d ProcessedMail-Einträge für %d UIDs entfernt", total, len(pairs)
)
return {"reset": total, "unique_uids": len(pairs)}
@router.get("/account/{account_id}", response_model=list[FilterRuleResponse])
def list_filters(account_id: int, db: Session = Depends(get_db)):
account = db.get(Account, account_id)