fixed reminder

This commit is contained in:
2026-06-11 12:22:37 +03:00
parent 4108d737e3
commit 41cbef61a9
12 changed files with 410 additions and 59 deletions
+14 -29
View File
@@ -4,10 +4,9 @@ from datetime import datetime, timezone
from sqlalchemy import select
from sqlalchemy.orm import Session
from app.chat.notice_inbox import post_notice_to_latest_chat
from app.db.models import Reminder
from app.reminders.completion import ReminderCompletionHandler
from app.reminders.notify import bump_notify_seq
from app.reminders.service import RECURRENCE_NONE, _advance_due, _format_local
logger = logging.getLogger(__name__)
@@ -16,7 +15,7 @@ def _utcnow() -> datetime:
return datetime.now(timezone.utc)
def check_due_reminders(db: Session) -> int:
def get_due_reminders(db: Session) -> list[Reminder]:
now = _utcnow()
stmt = (
select(Reminder)
@@ -28,33 +27,19 @@ def check_due_reminders(db: Session) -> int:
.order_by(Reminder.due_at.asc())
)
rows = list(db.scalars(stmt).all())
fired = 0
return [row for row in rows if not (row.last_fired_at and row.last_fired_at >= row.due_at)]
for row in rows:
if row.last_fired_at and row.last_fired_at >= row.due_at:
continue
local_when = _format_local(row.due_at, row.timezone, all_day=row.all_day)
notice = f"📅 **Напоминание** · {row.title}\n\n_{local_when}_"
if row.notes:
notice += f"\n{row.notes}"
async def process_due_reminders(db: Session) -> int:
due = get_due_reminders(db)
if not due:
return 0
post_notice_to_latest_chat(notice)
row.last_fired_at = now
handler = ReminderCompletionHandler(db)
for row in due:
await handler.process(row)
if row.recurrence == RECURRENCE_NONE:
row.completed_at = now
row.enabled = False
else:
row.due_at = _advance_due(row.due_at, row.recurrence)
row.last_fired_at = None
row.updated_at = now
fired += 1
if fired:
db.commit()
bump_notify_seq(db)
logger.info("Reminders fired: %d", fired)
return fired
db.commit()
bump_notify_seq(db)
logger.info("Reminders fired: %d", len(due))
return len(due)