fix(worker): 优化连接池容量拆分、并发入队与事件写入
This commit is contained in:
@@ -26,12 +26,36 @@ def active_account_work_exists(*, work_type: str):
|
||||
.where(
|
||||
AccountWorkItem.account_id == Account.id,
|
||||
AccountWorkItem.work_type == work_type,
|
||||
AccountWorkItem.status.in_(("pending", "running")),
|
||||
AccountWorkItem.active_dedupe_key.isnot(None),
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
|
||||
|
||||
def active_account_work_ids(
|
||||
db: Session,
|
||||
*,
|
||||
work_type: str,
|
||||
limit: int = 5000,
|
||||
) -> tuple[int, ...] | None:
|
||||
"""读取指定类型的在途账号 ID 集合(仅扫 active_dedupe_key 非空行)。
|
||||
|
||||
当在途数量超过 ``limit`` 时返回 ``None``,由调用方回退到 SQL 子查询。
|
||||
"""
|
||||
rows = (
|
||||
db.query(AccountWorkItem.account_id)
|
||||
.filter(
|
||||
AccountWorkItem.active_dedupe_key.isnot(None),
|
||||
AccountWorkItem.work_type == work_type,
|
||||
)
|
||||
.limit(max(1, int(limit)) + 1)
|
||||
.all()
|
||||
)
|
||||
if len(rows) > limit:
|
||||
return None
|
||||
return tuple(dict.fromkeys(int(account_id) for (account_id,) in rows))
|
||||
|
||||
|
||||
def active_account_work_count(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -44,7 +68,7 @@ def active_account_work_count(
|
||||
return 0
|
||||
query = db.query(func.count(AccountWorkItem.id)).filter(
|
||||
AccountWorkItem.work_type.in_(work_types),
|
||||
AccountWorkItem.status.in_(("pending", "running")),
|
||||
AccountWorkItem.active_dedupe_key.isnot(None),
|
||||
)
|
||||
if reason_prefix:
|
||||
query = query.filter(AccountWorkItem.reason.like(f"{reason_prefix}%"))
|
||||
@@ -83,29 +107,48 @@ def enqueue_account_work(
|
||||
existing.due_at = due_at
|
||||
db.flush()
|
||||
return existing
|
||||
item = AccountWorkItem(
|
||||
account_id=account_id,
|
||||
work_type=work_type[:32],
|
||||
priority=int(priority),
|
||||
# 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],
|
||||
payload_json=payload,
|
||||
)
|
||||
try:
|
||||
with db.begin_nested():
|
||||
db.add(item)
|
||||
db.flush()
|
||||
return item
|
||||
except IntegrityError:
|
||||
return (
|
||||
db.query(AccountWorkItem)
|
||||
.filter(AccountWorkItem.active_dedupe_key == dedupe_key[:191])
|
||||
.one()
|
||||
bind = db.get_bind()
|
||||
last_exc: IntegrityError | None = None
|
||||
for _ in range(3):
|
||||
item = AccountWorkItem(
|
||||
account_id=account_id,
|
||||
work_type=work_type[:32],
|
||||
priority=int(priority),
|
||||
# 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],
|
||||
payload_json=payload,
|
||||
)
|
||||
try:
|
||||
with db.begin_nested():
|
||||
db.add(item)
|
||||
db.flush()
|
||||
return item
|
||||
except IntegrityError as exc:
|
||||
last_exc = exc
|
||||
existing = (
|
||||
db.query(AccountWorkItem)
|
||||
.filter(AccountWorkItem.active_dedupe_key == dedupe_key[:191])
|
||||
.first()
|
||||
)
|
||||
if existing is not None:
|
||||
return existing
|
||||
# MySQL REPEATABLE READ 快照看不到并发事务刚提交的行(或对方刚消费完成并
|
||||
# 清空 active_dedupe_key),用同 bind 的新只读会话查最新已提交状态。
|
||||
with Session(bind=bind) as probe:
|
||||
existing_latest = (
|
||||
probe.query(AccountWorkItem)
|
||||
.filter(AccountWorkItem.active_dedupe_key == dedupe_key[:191])
|
||||
.first()
|
||||
)
|
||||
if existing_latest is not None:
|
||||
probe.expunge(existing_latest)
|
||||
return existing_latest
|
||||
assert last_exc is not None
|
||||
raise last_exc
|
||||
|
||||
|
||||
def claim_account_work(
|
||||
|
||||
Reference in New Issue
Block a user