fix(worker): 优化连接池容量拆分、并发入队与事件写入
This commit is contained in:
@@ -130,6 +130,7 @@ class DouyuBatchRunnerCore:
|
||||
self.log_sink = log_sink
|
||||
self._stop = threading.Event()
|
||||
self._counter_lock = threading.Lock()
|
||||
self._hook_local = threading.local()
|
||||
self._started = 0
|
||||
# 代理接入 (配置源 = 代理配置页, 与 CK 检测/虎牙注册同款):
|
||||
# 仅兑换/锁单/支付/充值/送礼等写操作任务走代理, 查询/刷新/绑定类直连
|
||||
@@ -245,25 +246,39 @@ class DouyuBatchRunnerCore:
|
||||
except Exception: # noqa: BLE001 - 日志落库不能中断任务
|
||||
logger.exception("写入斗鱼任务事件失败: {}", self.batch_id)
|
||||
|
||||
def _sync_platform_fulfillment(self, douyu_task_id: int, *, enabled: bool) -> None:
|
||||
def _sync_platform_fulfillment(
|
||||
self,
|
||||
douyu_task_id: int,
|
||||
*,
|
||||
enabled: bool,
|
||||
) -> None:
|
||||
"""绑定/确认任务完成后立即刷新履约投影并写 Outbox。"""
|
||||
if not enabled:
|
||||
return
|
||||
active_db: Session | None = getattr(self._hook_local, "db", None)
|
||||
sync_db: Session | None = None
|
||||
try:
|
||||
from ..database import WorkerSessionLocal
|
||||
from .platform_fulfillment.service import sync_task_from_binding_runner
|
||||
|
||||
# 当前批次会话刚刚提交过任务结果。履约投影使用独立短会话,避免
|
||||
# get_task_detail 留下的只读事务在下一轮外部 HTTP 轮询期间占连接。
|
||||
if active_db is not None:
|
||||
sync_task_from_binding_runner(active_db, douyu_task_id=int(douyu_task_id))
|
||||
return
|
||||
|
||||
from ..database import WorkerSessionLocal
|
||||
|
||||
sync_db = WorkerSessionLocal()
|
||||
try:
|
||||
sync_task_from_binding_runner(sync_db, douyu_task_id=int(douyu_task_id))
|
||||
finally:
|
||||
sync_db.close()
|
||||
sync_task_from_binding_runner(sync_db, douyu_task_id=int(douyu_task_id))
|
||||
except Exception: # noqa: BLE001 - 履约投影失败不能覆盖任务执行结果
|
||||
if active_db is not None:
|
||||
active_db.rollback()
|
||||
elif sync_db is not None:
|
||||
sync_db.rollback()
|
||||
logger.exception(
|
||||
"[douyu] 同步直播账号履约状态失败: task_id={}", douyu_task_id
|
||||
)
|
||||
finally:
|
||||
if sync_db is not None:
|
||||
sync_db.close()
|
||||
|
||||
def _verify_platform_binding(
|
||||
self,
|
||||
@@ -274,27 +289,39 @@ class DouyuBatchRunnerCore:
|
||||
"""确认绑定完成后,再去斗鱼实时查询复核一次。"""
|
||||
if not enabled:
|
||||
return
|
||||
active_db: Session | None = getattr(self._hook_local, "db", None)
|
||||
verify_db: Session | None = None
|
||||
try:
|
||||
from ..database import WorkerSessionLocal
|
||||
from .platform_fulfillment.service import verify_active_binding_for_douyu_task
|
||||
from .platform_fulfillment.service import (
|
||||
verify_active_binding_for_douyu_task,
|
||||
)
|
||||
|
||||
verify_db = WorkerSessionLocal()
|
||||
try:
|
||||
task = verify_db.get(DouyuTask, int(douyu_task_id))
|
||||
if task is None or task.status != "success":
|
||||
return
|
||||
verify_active_binding_for_douyu_task(
|
||||
verify_db,
|
||||
douyu_task_id=int(douyu_task_id),
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
finally:
|
||||
verify_db.close()
|
||||
target_db = active_db
|
||||
if target_db is None:
|
||||
from ..database import WorkerSessionLocal
|
||||
|
||||
verify_db = WorkerSessionLocal()
|
||||
target_db = verify_db
|
||||
task = target_db.get(DouyuTask, int(douyu_task_id))
|
||||
if task is None or task.status != "success":
|
||||
return
|
||||
verify_active_binding_for_douyu_task(
|
||||
target_db,
|
||||
douyu_task_id=int(douyu_task_id),
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
except Exception: # noqa: BLE001 - 复核异常不能覆盖主任务提交
|
||||
if active_db is not None:
|
||||
active_db.rollback()
|
||||
elif verify_db is not None:
|
||||
verify_db.rollback()
|
||||
logger.exception(
|
||||
"[douyu] 自动复核直播履约绑定状态失败: task_id={}",
|
||||
douyu_task_id,
|
||||
)
|
||||
finally:
|
||||
if verify_db is not None:
|
||||
verify_db.close()
|
||||
|
||||
def _refresh_platform_exchange_eligibility(
|
||||
self,
|
||||
@@ -305,30 +332,40 @@ class DouyuBatchRunnerCore:
|
||||
"""账号积分/宝典变化后,自动刷新当前履约任务的兑换投影。"""
|
||||
if not enabled:
|
||||
return
|
||||
active_db: Session | None = getattr(self._hook_local, "db", None)
|
||||
refresh_db: Session | None = None
|
||||
try:
|
||||
from ..database import WorkerSessionLocal
|
||||
from .platform_fulfillment.service import (
|
||||
refresh_active_exchange_eligibility_for_account,
|
||||
)
|
||||
|
||||
refresh_db = WorkerSessionLocal()
|
||||
try:
|
||||
task = refresh_db.get(DouyuTask, int(douyu_task_id))
|
||||
if task is None or task.status != "success":
|
||||
return
|
||||
refresh_active_exchange_eligibility_for_account(
|
||||
refresh_db,
|
||||
account_id=int(task.account_id),
|
||||
scope=str(task.handbook_scope or "elite"),
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
finally:
|
||||
refresh_db.close()
|
||||
target_db = active_db
|
||||
if target_db is None:
|
||||
from ..database import WorkerSessionLocal
|
||||
|
||||
refresh_db = WorkerSessionLocal()
|
||||
target_db = refresh_db
|
||||
task = target_db.get(DouyuTask, int(douyu_task_id))
|
||||
if task is None or task.status != "success":
|
||||
return
|
||||
refresh_active_exchange_eligibility_for_account(
|
||||
target_db,
|
||||
account_id=int(task.account_id),
|
||||
scope=str(task.handbook_scope or "elite"),
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
except Exception: # noqa: BLE001 - 自动刷新不能覆盖任务执行结果
|
||||
if active_db is not None:
|
||||
active_db.rollback()
|
||||
elif refresh_db is not None:
|
||||
refresh_db.rollback()
|
||||
logger.exception(
|
||||
"[douyu] 自动刷新直播履约兑换资格失败: task_id={}",
|
||||
douyu_task_id,
|
||||
)
|
||||
finally:
|
||||
if refresh_db is not None:
|
||||
refresh_db.close()
|
||||
|
||||
def _sync_platform_fulfillment_operation(
|
||||
self,
|
||||
@@ -432,21 +469,26 @@ class DouyuBatchRunnerCore:
|
||||
logger.exception(
|
||||
"[douyu] 写入账号任务结果事件失败: task_id={}", task.id
|
||||
)
|
||||
self._sync_platform_fulfillment(
|
||||
task_id,
|
||||
enabled=sync_platform_fulfillment,
|
||||
)
|
||||
self._verify_platform_binding(
|
||||
task_id,
|
||||
enabled=(status == "success" and task.task_type == "confirm_bind"),
|
||||
)
|
||||
self._refresh_platform_exchange_eligibility(
|
||||
task_id,
|
||||
enabled=(
|
||||
status == "success"
|
||||
and task.task_type in AUTO_EXCHANGE_ELIGIBILITY_TASK_TYPES
|
||||
),
|
||||
)
|
||||
previous_db = getattr(self._hook_local, "db", None)
|
||||
self._hook_local.db = db
|
||||
try:
|
||||
self._sync_platform_fulfillment(
|
||||
task_id,
|
||||
enabled=sync_platform_fulfillment,
|
||||
)
|
||||
self._verify_platform_binding(
|
||||
task_id,
|
||||
enabled=(status == "success" and task.task_type == "confirm_bind"),
|
||||
)
|
||||
self._refresh_platform_exchange_eligibility(
|
||||
task_id,
|
||||
enabled=(
|
||||
status == "success"
|
||||
and task.task_type in AUTO_EXCHANGE_ELIGIBILITY_TASK_TYPES
|
||||
),
|
||||
)
|
||||
finally:
|
||||
self._hook_local.db = previous_db
|
||||
|
||||
def _schedule_payment_poll(
|
||||
self,
|
||||
@@ -488,7 +530,15 @@ class DouyuBatchRunnerCore:
|
||||
db.commit()
|
||||
self._push_task_event(task)
|
||||
if sync_platform:
|
||||
self._sync_platform_fulfillment(task_id, enabled=has_platform_task)
|
||||
previous_db = getattr(self._hook_local, "db", None)
|
||||
self._hook_local.db = db
|
||||
try:
|
||||
self._sync_platform_fulfillment(
|
||||
task_id,
|
||||
enabled=has_platform_task,
|
||||
)
|
||||
finally:
|
||||
self._hook_local.db = previous_db
|
||||
|
||||
def _upsert_goods(self, db: Session, goods: list[dict]) -> None:
|
||||
upsert_goods_snapshot(db, goods)
|
||||
|
||||
Reference in New Issue
Block a user