优化(直播履约): 即时选号并收敛订单快照同步

This commit is contained in:
yml
2026-11-18 16:58:08 +08:00
parent b7b09c90bd
commit dc95d83b32
13 changed files with 933 additions and 70 deletions
@@ -28,6 +28,21 @@ def _table_exists(table: str) -> bool:
)
def _column_exists(table: str, column: str) -> bool:
return bool(
op.get_bind()
.execute(
sa.text(
"SELECT COUNT(*) FROM information_schema.columns "
"WHERE table_schema = DATABASE() "
"AND table_name = :table AND column_name = :column"
),
{"table": table, "column": column},
)
.scalar()
)
def upgrade() -> None:
op.alter_column(
"account_leases",
@@ -50,12 +65,19 @@ def upgrade() -> None:
),
sa.PrimaryKeyConstraint("id"),
)
order_match_column = (
", order_match_started_at"
if _column_exists("platform_fulfillment_settings", "order_match_started_at")
else ""
)
order_match_value = ", NOW()" if order_match_column else ""
op.execute(
sa.text(
"""
f"""
INSERT INTO platform_fulfillment_settings (
id, lease_duration_minutes, updated_by, updated_at
) VALUES (1, NULL, NULL, NOW())
{order_match_column}
) VALUES (1, NULL, NULL, NOW() {order_match_value})
ON DUPLICATE KEY UPDATE id = id
"""
)
@@ -0,0 +1,90 @@
"""直播账号履约即时选号与状态版本
- 新任务通过 ``next_allocate_at`` 立即进入选号 worker;
- 失败任务由 worker 自己设置下一次重试时间,不再统一等待 45 秒;
- ``state_version`` 为回传 order_site 的状态提供单调版本,防止乱序覆盖。
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision: str = "20261118_0089"
down_revision: str | None = "20261118_0088"
branch_labels: str | None = None
depends_on: str | None = None
TABLE = "platform_fulfillment_tasks"
NEXT_ALLOCATE_INDEX = "ix_platform_fulfillment_next_allocate"
def _column_exists(column: str) -> bool:
return bool(
op.get_bind()
.execute(
sa.text(
"SELECT COUNT(*) FROM information_schema.columns "
"WHERE table_schema = DATABASE() "
"AND table_name = :table AND column_name = :column"
),
{"table": TABLE, "column": column},
)
.scalar()
)
def _index_exists(index: str) -> bool:
return bool(
op.get_bind()
.execute(
sa.text(
"SELECT COUNT(*) FROM information_schema.statistics "
"WHERE table_schema = DATABASE() "
"AND table_name = :table AND index_name = :index"
),
{"table": TABLE, "index": index},
)
.scalar()
)
def upgrade() -> None:
if not _column_exists("state_version"):
op.add_column(
TABLE,
sa.Column(
"state_version",
sa.BigInteger(),
nullable=False,
server_default=sa.text("0"),
),
)
if not _column_exists("next_allocate_at"):
op.add_column(
TABLE,
sa.Column("next_allocate_at", sa.DateTime(), nullable=True),
)
op.get_bind().execute(
sa.text(
"UPDATE platform_fulfillment_tasks "
"SET next_allocate_at = UTC_TIMESTAMP() "
"WHERE execution_status IN ('ready', 'manual_required') "
"AND account_id IS NULL"
)
)
if not _index_exists(NEXT_ALLOCATE_INDEX):
op.create_index(
NEXT_ALLOCATE_INDEX,
TABLE,
["execution_status", "next_allocate_at", "id"],
)
def downgrade() -> None:
if _index_exists(NEXT_ALLOCATE_INDEX):
op.drop_index(NEXT_ALLOCATE_INDEX, table_name=TABLE)
if _column_exists("next_allocate_at"):
op.drop_column(TABLE, "next_allocate_at")
if _column_exists("state_version"):
op.drop_column(TABLE, "state_version")