fix(worker): 优化连接池容量拆分、并发入队与事件写入
This commit is contained in:
@@ -10,7 +10,7 @@ from dataclasses import dataclass
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import and_, case, literal, or_, select
|
||||
from sqlalchemy import and_, case, func, literal, or_, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session, aliased
|
||||
|
||||
@@ -39,7 +39,7 @@ from ..account_query_filters import (
|
||||
valid_ck_filter,
|
||||
)
|
||||
from ..account_sync_runner import WORK_STOREHOME_SYNC
|
||||
from ..account_work_queue import active_account_work_exists
|
||||
from ..account_work_queue import active_account_work_exists, active_account_work_ids
|
||||
from .policy import (
|
||||
PRIORITY_KEY_BUNDLE_MATCH,
|
||||
PRIORITY_KEY_CK_FRESHNESS,
|
||||
@@ -666,52 +666,65 @@ def capability_refresh_candidate_accounts(
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
refresh_needed = []
|
||||
for commodity_id in commodities:
|
||||
state_exists = (
|
||||
select(AccountCommodityState.id)
|
||||
.where(
|
||||
AccountCommodityState.account_id == Account.id,
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodity_id,
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
state_due = (
|
||||
select(AccountCommodityState.id)
|
||||
.where(
|
||||
AccountCommodityState.account_id == Account.id,
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodity_id,
|
||||
or_(
|
||||
AccountCommodityState.valid_until.is_(None),
|
||||
AccountCommodityState.valid_until <= moment,
|
||||
AccountCommodityState.next_check_at.is_(None),
|
||||
AccountCommodityState.next_check_at <= moment,
|
||||
),
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
refresh_needed.append(
|
||||
state_due = (
|
||||
select(AccountCommodityState.id)
|
||||
.where(
|
||||
AccountCommodityState.account_id == Account.id,
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id.in_(commodities),
|
||||
or_(
|
||||
and_(~fresh_storehome, ~state_exists),
|
||||
state_due,
|
||||
AccountCommodityState.valid_until.is_(None),
|
||||
AccountCommodityState.valid_until <= moment,
|
||||
AccountCommodityState.next_check_at.is_(None),
|
||||
AccountCommodityState.next_check_at <= moment,
|
||||
),
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
if len(commodities) == 1:
|
||||
missing_state = ~(
|
||||
select(AccountCommodityState.id)
|
||||
.where(
|
||||
AccountCommodityState.account_id == Account.id,
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodities[0],
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
else:
|
||||
# (account_id, platform, scope, commodity_id) 有唯一约束 uq_account_commodity_state,
|
||||
# 行数少于待履约商品数即表示至少缺失一个商品的快照行。
|
||||
missing_state = (
|
||||
select(func.count(AccountCommodityState.id))
|
||||
.where(
|
||||
AccountCommodityState.account_id == Account.id,
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id.in_(commodities),
|
||||
)
|
||||
.scalar_subquery()
|
||||
< len(commodities)
|
||||
)
|
||||
|
||||
query = db.query(Account).filter(
|
||||
or_(
|
||||
state_due,
|
||||
and_(~fresh_storehome, missing_state),
|
||||
),
|
||||
cookie_present_filter(),
|
||||
valid_ck_filter(),
|
||||
automatable_account_filter(),
|
||||
assigned_to_pool_filter(),
|
||||
)
|
||||
active_work_ids = active_account_work_ids(db, work_type=WORK_STOREHOME_SYNC)
|
||||
if active_work_ids is None:
|
||||
query = query.filter(~active_account_work_exists(work_type=WORK_STOREHOME_SYNC))
|
||||
elif active_work_ids:
|
||||
query = query.filter(~Account.id.in_(active_work_ids))
|
||||
return (
|
||||
db.query(Account)
|
||||
.filter(
|
||||
or_(*refresh_needed),
|
||||
cookie_present_filter(),
|
||||
valid_ck_filter(),
|
||||
automatable_account_filter(),
|
||||
assigned_to_pool_filter(),
|
||||
~active_account_work_exists(work_type=WORK_STOREHOME_SYNC),
|
||||
)
|
||||
.order_by(*_refresh_candidate_order_by(moment))
|
||||
query.order_by(*_refresh_candidate_order_by(moment))
|
||||
.limit(max(1, min(int(limit), 100)))
|
||||
.all()
|
||||
)
|
||||
@@ -791,8 +804,12 @@ def storehome_refresh_candidate_accounts(
|
||||
valid_ck_filter(),
|
||||
automatable_account_filter(),
|
||||
assigned_to_pool_filter(),
|
||||
~active_account_work_exists(work_type=WORK_STOREHOME_SYNC),
|
||||
)
|
||||
active_work_ids = active_account_work_ids(db, work_type=WORK_STOREHOME_SYNC)
|
||||
if active_work_ids is None:
|
||||
query = query.filter(~active_account_work_exists(work_type=WORK_STOREHOME_SYNC))
|
||||
elif active_work_ids:
|
||||
query = query.filter(~Account.id.in_(active_work_ids))
|
||||
if excluded_ids:
|
||||
query = query.filter(~Account.id.in_(excluded_ids))
|
||||
order_by: list[Any] = []
|
||||
|
||||
Reference in New Issue
Block a user