feat: track duty email confirmations and add throttled account email tests

This commit is contained in:
2026-11-06 15:07:48 +08:00
parent 867443163a
commit 0a7a641430
11 changed files with 352 additions and 16 deletions
+9 -7
View File
@@ -168,7 +168,8 @@ def due_batches(schedule, now):
return batches
async def send_due(schedule, now, accounts, send_email, state_path, overrides=None):
async def send_due(schedule, now, accounts, send_email, state_path, overrides=None, base_url="http://127.0.0.1:37777"):
from .duty_receipts import send_notice
result = {"sent": 0, "failed": 0, "missing": {}}
if not schedule["enabled"]:
return result
@@ -180,28 +181,29 @@ async def send_due(schedule, now, accounts, send_email, state_path, overrides=No
if not await asyncio.to_thread(claim, state_path, key, email, now.timestamp()):
continue
clocks = schedule.get("slot_times", {})
lines = [f"{name}:" + "、".join(f"{slot}({clocks[slot]} 开始)" if slot in clocks else slot for slot in slots) for name, slots in names.items()]
body = f"{label}\n\n日期:{target.isoformat()}(周{'一二三四五六日'[target.weekday()]},{'单' if week == 'odd' else '双'}周)\n" + "\n".join(lines) + "\n\n以上时间均为北京时间。请准时值班;如需调班,请提前联系值班负责人。\nWorkLog"
slots = {slot for items in names.values() for slot in items}
ends = [datetime.combine(target, datetime.strptime(clocks[slot], "%H:%M").time(), BEIJING) + timedelta(minutes=schedule.get("duration_minutes", 105)) for slot in slots]
body = f"日期:{target.isoformat()}(周{'一二三四五六日'[target.weekday()]},{'单' if week == 'odd' else '双'}周)\n" + "、".join(f"{slot} {clocks[slot]} 开始" for slot in sorted(slots)) + "\n以上时间均为北京时间。请准时值班。"
try:
await send_email(email, f"{label}|{target.isoformat()}", body)
sent = await send_notice(state_path, target.isoformat(), names, email, max(ends).timestamp(), send_email, f"{label}|{target.isoformat()}", body, base_url, now.timestamp())
except Exception:
result["failed"] += 1
await asyncio.to_thread(finish, state_path, key, email, "failed")
log.exception("值班邮件发送失败")
else:
result["sent"] += 1
result["sent"] += int(sent)
await asyncio.to_thread(finish, state_path, key, email, "sent")
return result
async def reminder_loop(schedule_path, state_path, get_accounts, send_email, overrides_path=None):
async def reminder_loop(schedule_path, state_path, get_accounts, send_email, overrides_path=None, base_url="http://127.0.0.1:37777"):
previous_missing = None
while True:
try:
schedule = load_schedule(schedule_path)
if schedule["enabled"]:
overrides = json.loads(Path(overrides_path).read_text(encoding="utf-8-sig")) if overrides_path and Path(overrides_path).exists() else {}
result = await send_due(schedule, datetime.now(BEIJING), await get_accounts(), send_email, state_path, overrides)
result = await send_due(schedule, datetime.now(BEIJING), await get_accounts(), send_email, state_path, overrides, base_url)
if result["missing"] and result["missing"] != previous_missing:
log.warning("值班名单无法匹配邮箱:%s", result["missing"])
previous_missing = result["missing"]