feat: add alternating-week duty roster and email reminders

This commit is contained in:
2026-11-06 13:04:53 +08:00
parent e556a51b93
commit b40841e2fb
10 changed files with 762 additions and 0 deletions
+16
View File
@@ -36,6 +36,7 @@ from sqlalchemy.dialects.sqlite import insert as sqlite_insert
from .models import activities, auth_limits, email_verifications, join_links, metadata, participations, sessions, users
from .security import hash_password, new_token, token_digest, verify_password
from .backup import backup_loop, parse_time
from .duty import reminder_loop
BASE_DIR = Path(__file__).resolve().parent
PROJECT_DIR = BASE_DIR.parent
@@ -534,9 +535,24 @@ async def lifespan(_app: FastAPI):
if BACKUP_ENABLED:
hour, minute = parse_time(BACKUP_TIME)
backup_task = asyncio.create_task(backup_loop(_db_path, hour, minute, BACKUP_KEEP))
duty_task: asyncio.Task | None = None
duty_path = Path(os.getenv("WORKLOG_DUTY_SCHEDULE", str(PROJECT_DIR / "duty/schedule.json")))
if duty_path.exists() and os.getenv("WORKLOG_DUTY_REMINDERS", "1").lower() not in ("0", "false", "no"):
async def duty_accounts():
async with SessionLocal() as db:
rows = await db.execute(select(users.c.display_name, users.c.email))
return [dict(row) for row in rows.mappings()]
duty_task = asyncio.create_task(reminder_loop(
duty_path, _db_path.with_name("duty-reminders.db"), duty_accounts, _send_email,
os.getenv("WORKLOG_DUTY_EMAILS", str(_db_path.parent / "duty-emails.json")),
))
try:
yield
finally:
if duty_task is not None:
duty_task.cancel()
await asyncio.gather(duty_task, return_exceptions=True)
if backup_task is not None:
backup_task.cancel()
await engine.dispose()