feat(douyu): 建立账号同步队列与供给库存

This commit is contained in:
yml
2026-11-21 21:48:51 +08:00
parent 1d262cdfd9
commit 3baf15afda
16 changed files with 1158 additions and 33 deletions
+29 -2
View File
@@ -42,7 +42,9 @@ def enqueue_account_work(
account_id=account_id,
work_type=work_type[:32],
priority=int(priority),
due_at=due_at or datetime.now(UTC),
# MySQL DATETIME 不保存微秒;立即到期的工作项必须落在当前秒,
# 否则四舍五入到下一秒后,本轮 claim 会误判“还没到期”。
due_at=(due_at or datetime.now(UTC)).replace(microsecond=0),
max_attempts=max(1, int(max_attempts)),
dedupe_key=dedupe_key[:191],
reason=reason[:255],
@@ -149,14 +151,39 @@ def fail_account_work(
item.finished_at = moment
else:
item.status = "pending"
item.due_at = moment + timedelta(seconds=max(1, retry_after_seconds))
item.due_at = (
moment + timedelta(seconds=max(1, retry_after_seconds))
).replace(microsecond=0)
db.flush()
def recover_running_account_work(
db: Session,
*,
now: datetime | None = None,
) -> int:
"""服务重启后把中断的 running 工作项退回 pending。"""
moment = (now or datetime.now(UTC)).replace(microsecond=0)
items = (
db.query(AccountWorkItem)
.filter(AccountWorkItem.status == "running")
.all()
)
for item in items:
item.status = "pending"
item.due_at = moment
item.last_error = "工作执行中断(服务重启),已重新排队"
item.updated_at = moment
if items:
db.commit()
return len(items)
__all__ = [
"claim_account_work",
"complete_account_work",
"complete_account_work_type",
"enqueue_account_work",
"fail_account_work",
"recover_running_account_work",
]