42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
import asyncio
|
|
import logging
|
|
|
|
from sqlalchemy import select
|
|
|
|
from app.db.base import SessionLocal
|
|
from app.db.models import User
|
|
from app.pomodoro.completion import PomodoroCompletionHandler
|
|
from app.pomodoro.service import PomodoroService
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
WATCH_INTERVAL_SEC = 2
|
|
|
|
|
|
async def pomodoro_watcher_loop() -> None:
|
|
while True:
|
|
try:
|
|
await asyncio.sleep(WATCH_INTERVAL_SEC)
|
|
await _tick()
|
|
except asyncio.CancelledError:
|
|
raise
|
|
except Exception:
|
|
logger.exception("Pomodoro watcher error")
|
|
|
|
|
|
async def _tick() -> None:
|
|
db = SessionLocal()
|
|
try:
|
|
users = db.scalars(select(User).where(User.is_active.is_(True))).all()
|
|
for user in users:
|
|
service = PomodoroService(db, user.id)
|
|
service.get_status()
|
|
pending = service.get_pending_completions()
|
|
if not pending:
|
|
continue
|
|
handler = PomodoroCompletionHandler(db, user.id)
|
|
for session in pending:
|
|
await handler.process(session)
|
|
finally:
|
|
db.close()
|