fix(douyu): 完善协作状态回写并锁定构建镜像
This commit is contained in:
+9
-6
@@ -1,6 +1,11 @@
|
|||||||
# syntax=docker/dockerfile:1.7
|
# syntax=docker/dockerfile:1.7
|
||||||
|
|
||||||
|
# 基础镜像锁 digest:tag 仅用于可读性,实际内容不会随上游 tag 漂移。
|
||||||
|
ARG NODE_BASE_IMAGE=node:20-slim@sha256:2cf067cfed83d5ea958367df9f966191a942351a2df77d6f0193e162b5febfc0
|
||||||
|
ARG PYTHON_BASE_IMAGE=python:3.12-slim@sha256:5e2bdb1b0d542d5039cffb8181c65930093949194a65c365dfb1b0fe13a4fa5b
|
||||||
|
|
||||||
# ===== 阶段1: 构建前端 =====
|
# ===== 阶段1: 构建前端 =====
|
||||||
FROM node:20-slim AS frontend-builder
|
FROM ${NODE_BASE_IMAGE} AS frontend-builder
|
||||||
|
|
||||||
WORKDIR /build
|
WORKDIR /build
|
||||||
COPY web/frontend/package.json web/frontend/package-lock.json* ./
|
COPY web/frontend/package.json web/frontend/package-lock.json* ./
|
||||||
@@ -13,7 +18,7 @@ COPY web/frontend/ ./
|
|||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
# ===== 阶段2: 构建应用宝 Worker 的 Node 依赖 =====
|
# ===== 阶段2: 构建应用宝 Worker 的 Node 依赖 =====
|
||||||
FROM node:20-slim AS yyb-node-builder
|
FROM ${NODE_BASE_IMAGE} AS yyb-node-builder
|
||||||
|
|
||||||
WORKDIR /build
|
WORKDIR /build
|
||||||
COPY services/yyb-worker/package.json services/yyb-worker/package-lock.json ./
|
COPY services/yyb-worker/package.json services/yyb-worker/package-lock.json ./
|
||||||
@@ -24,7 +29,7 @@ RUN --mount=type=cache,target=/root/.npm \
|
|||||||
(npm config set registry https://registry.npmjs.org && npm ci --omit=dev)
|
(npm config set registry https://registry.npmjs.org && npm ci --omit=dev)
|
||||||
|
|
||||||
# ===== 阶段3: Python 运行时 =====
|
# ===== 阶段3: Python 运行时 =====
|
||||||
FROM python:3.12-slim AS runtime
|
FROM ${PYTHON_BASE_IMAGE} AS runtime
|
||||||
|
|
||||||
ARG USE_CHINA_MIRRORS=true
|
ARG USE_CHINA_MIRRORS=true
|
||||||
ARG UV_VERSION=0.13.5
|
ARG UV_VERSION=0.13.5
|
||||||
@@ -35,10 +40,8 @@ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
|
|||||||
--mount=type=cache,target=/var/lib/apt,sharing=locked \
|
--mount=type=cache,target=/var/lib/apt,sharing=locked \
|
||||||
if [ "$USE_CHINA_MIRRORS" = "true" ]; then \
|
if [ "$USE_CHINA_MIRRORS" = "true" ]; then \
|
||||||
cp /etc/apt/sources.list.d/debian.sources /tmp/debian.sources && \
|
cp /etc/apt/sources.list.d/debian.sources /tmp/debian.sources && \
|
||||||
sed -i 's/deb.debian.org/mirrors.cloud.aliyuncs.com/g' /etc/apt/sources.list.d/debian.sources && \
|
sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources && \
|
||||||
(apt-get update -o APT::Update::Error-Mode=any || \
|
(apt-get update -o APT::Update::Error-Mode=any || \
|
||||||
(sed -i 's/mirrors.cloud.aliyuncs.com/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources && \
|
|
||||||
apt-get update -o APT::Update::Error-Mode=any) || \
|
|
||||||
(cp /tmp/debian.sources /etc/apt/sources.list.d/debian.sources && \
|
(cp /tmp/debian.sources /etc/apt/sources.list.d/debian.sources && \
|
||||||
apt-get update -o APT::Update::Error-Mode=any)); \
|
apt-get update -o APT::Update::Error-Mode=any)); \
|
||||||
else \
|
else \
|
||||||
|
|||||||
@@ -484,6 +484,148 @@ class TestDouyuCollabOrders:
|
|||||||
assert lease.status == "released"
|
assert lease.status == "released"
|
||||||
assert lease.release_reason == "douyu_collab_done"
|
assert lease.release_reason == "douyu_collab_done"
|
||||||
|
|
||||||
|
def test_status_change_does_not_steal_active_platform_lease(self):
|
||||||
|
order = collab.create_order(
|
||||||
|
self.session,
|
||||||
|
self.admin,
|
||||||
|
order_no="DY-PLATFORM-LEASE-STATUS",
|
||||||
|
shop="店铺A",
|
||||||
|
account_username="douyu-account-a",
|
||||||
|
gift_name="礼包A",
|
||||||
|
required_points=100,
|
||||||
|
home_images=["collab/home-platform-lease.png"],
|
||||||
|
client=FakeClient(),
|
||||||
|
)
|
||||||
|
collab_lease = self.session.query(AccountLease).one()
|
||||||
|
collab_lease.status = "released"
|
||||||
|
collab_lease.release_reason = "test_handoff"
|
||||||
|
platform_lease = AccountLease(
|
||||||
|
account_id=self.account.id,
|
||||||
|
holder_user_id=self.admin.id,
|
||||||
|
scope="elite",
|
||||||
|
purpose="platform_fulfillment",
|
||||||
|
batch_id="pf:999",
|
||||||
|
status="active",
|
||||||
|
)
|
||||||
|
self.session.add(platform_lease)
|
||||||
|
self.session.commit()
|
||||||
|
|
||||||
|
changed = collab.set_status(
|
||||||
|
self.session,
|
||||||
|
self.admin,
|
||||||
|
order,
|
||||||
|
status=collab.STATUS_RECHARGED,
|
||||||
|
reason="仅补记充值状态",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert changed.status == collab.STATUS_RECHARGED
|
||||||
|
self.session.refresh(platform_lease)
|
||||||
|
assert platform_lease.status == "active"
|
||||||
|
assert (
|
||||||
|
self.session.query(AccountLease)
|
||||||
|
.filter(
|
||||||
|
AccountLease.account_id == self.account.id,
|
||||||
|
AccountLease.status == "active",
|
||||||
|
AccountLease.purpose == collab.LEASE_PURPOSE_DOUYU_COLLAB,
|
||||||
|
)
|
||||||
|
.count()
|
||||||
|
== 0
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_status_change_backfills_missing_collab_lease(self):
|
||||||
|
order = collab.create_order(
|
||||||
|
self.session,
|
||||||
|
self.admin,
|
||||||
|
order_no="DY-BACKFILL-LEASE-STATUS",
|
||||||
|
shop="店铺A",
|
||||||
|
account_username="douyu-account-a",
|
||||||
|
gift_name="礼包A",
|
||||||
|
required_points=100,
|
||||||
|
home_images=["collab/home-backfill-lease.png"],
|
||||||
|
client=FakeClient(),
|
||||||
|
)
|
||||||
|
lease = self.session.query(AccountLease).one()
|
||||||
|
lease.status = "released"
|
||||||
|
lease.release_reason = "test_missing"
|
||||||
|
self.session.commit()
|
||||||
|
|
||||||
|
changed = collab.set_status(
|
||||||
|
self.session,
|
||||||
|
self.admin,
|
||||||
|
order,
|
||||||
|
status=collab.STATUS_RECHARGED,
|
||||||
|
reason="补记充值状态并恢复占用",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert changed.status == collab.STATUS_RECHARGED
|
||||||
|
restored = (
|
||||||
|
self.session.query(AccountLease)
|
||||||
|
.filter(
|
||||||
|
AccountLease.account_id == self.account.id,
|
||||||
|
AccountLease.status == "active",
|
||||||
|
)
|
||||||
|
.one()
|
||||||
|
)
|
||||||
|
assert restored.purpose == collab.LEASE_PURPOSE_DOUYU_COLLAB
|
||||||
|
|
||||||
|
def test_recharged_status_can_backfill_without_bind_role(self):
|
||||||
|
order = collab.create_order(
|
||||||
|
self.session,
|
||||||
|
self.admin,
|
||||||
|
order_no="DY-RECHARGED-NO-ROLE",
|
||||||
|
shop="店铺A",
|
||||||
|
account_username="douyu-account-a",
|
||||||
|
draft=True,
|
||||||
|
)
|
||||||
|
collab.update_order(
|
||||||
|
self.session,
|
||||||
|
self.admin,
|
||||||
|
order,
|
||||||
|
{
|
||||||
|
"account_username": "douyu-account-a",
|
||||||
|
"gift_name": "礼包A",
|
||||||
|
"commodity_id": "gift-a",
|
||||||
|
"required_points": 100,
|
||||||
|
"home_images": ["collab/home-recharged-no-role.png"],
|
||||||
|
},
|
||||||
|
client=FakeNoBindClient(),
|
||||||
|
)
|
||||||
|
self.session.refresh(order)
|
||||||
|
assert order.status == collab.STATUS_INCOMPLETE
|
||||||
|
assert not order.bind_game_role
|
||||||
|
|
||||||
|
with pytest.raises(collab.CollabError) as excinfo:
|
||||||
|
collab.set_status(
|
||||||
|
self.session,
|
||||||
|
self.admin,
|
||||||
|
order,
|
||||||
|
status=collab.STATUS_TO_RECHARGE,
|
||||||
|
reason="进入待充值",
|
||||||
|
)
|
||||||
|
assert excinfo.value.code == "recharge_precondition"
|
||||||
|
assert "绑定角色(先校验账号)" in str(excinfo.value)
|
||||||
|
|
||||||
|
changed = collab.set_status(
|
||||||
|
self.session,
|
||||||
|
self.admin,
|
||||||
|
order,
|
||||||
|
status=collab.STATUS_RECHARGED,
|
||||||
|
reason="充值已完成,补记状态",
|
||||||
|
)
|
||||||
|
assert changed.status == collab.STATUS_RECHARGED
|
||||||
|
assert changed.recharged_at is not None
|
||||||
|
|
||||||
|
with pytest.raises(collab.CollabError) as excinfo:
|
||||||
|
collab.set_status(
|
||||||
|
self.session,
|
||||||
|
self.admin,
|
||||||
|
order,
|
||||||
|
status=collab.STATUS_SCRIPTED,
|
||||||
|
reason="角色资料仍未补齐",
|
||||||
|
)
|
||||||
|
assert excinfo.value.code == "recharge_precondition"
|
||||||
|
assert "绑定角色(先校验账号)" in str(excinfo.value)
|
||||||
|
|
||||||
def test_account_role_error_keeps_lease_and_requires_explicit_rollback(self):
|
def test_account_role_error_keeps_lease_and_requires_explicit_rollback(self):
|
||||||
order = collab.create_order(
|
order = collab.create_order(
|
||||||
self.session,
|
self.session,
|
||||||
|
|||||||
@@ -650,9 +650,16 @@ def _sync_order_lease(
|
|||||||
*,
|
*,
|
||||||
reason: str,
|
reason: str,
|
||||||
force_active: bool = False,
|
force_active: bool = False,
|
||||||
|
reuse_active_lease: bool = False,
|
||||||
commit: bool = False,
|
commit: bool = False,
|
||||||
) -> AccountLease | None:
|
) -> AccountLease | None:
|
||||||
if force_active or _order_occupies_account(order):
|
if force_active or _order_occupies_account(order):
|
||||||
|
if reuse_active_lease:
|
||||||
|
# 纯状态回写允许沿用平台履约或其他业务的有效占用,不抢占、不转换。
|
||||||
|
lease = active_lease_for_account(db, order.account_id)
|
||||||
|
if lease is not None:
|
||||||
|
return lease
|
||||||
|
# 没有任何有效占用的历史异常行仍需补建协作租约,避免自动分配选中。
|
||||||
return _ensure_order_account_lease(
|
return _ensure_order_account_lease(
|
||||||
db,
|
db,
|
||||||
current,
|
current,
|
||||||
@@ -1578,7 +1585,12 @@ def _status_audit_target(order: DouyuExternalOrder) -> str:
|
|||||||
return f"douyu_external_order:{order.id}"
|
return f"douyu_external_order:{order.id}"
|
||||||
|
|
||||||
|
|
||||||
def _assert_ready_for_recharge(order: DouyuExternalOrder) -> None:
|
def _assert_ready_for_recharge(
|
||||||
|
order: DouyuExternalOrder,
|
||||||
|
*,
|
||||||
|
entering_waiting: bool,
|
||||||
|
allow_missing_role: bool = False,
|
||||||
|
) -> None:
|
||||||
missing: list[str] = []
|
missing: list[str] = []
|
||||||
if not order.account_id or not order.account_username:
|
if not order.account_id or not order.account_username:
|
||||||
missing.append("斗鱼账号")
|
missing.append("斗鱼账号")
|
||||||
@@ -1586,11 +1598,12 @@ def _assert_ready_for_recharge(order: DouyuExternalOrder) -> None:
|
|||||||
missing.append("礼包")
|
missing.append("礼包")
|
||||||
if not order.home_images:
|
if not order.home_images:
|
||||||
missing.append("主页截图")
|
missing.append("主页截图")
|
||||||
if not order.bind_game_role:
|
if not allow_missing_role and not order.bind_game_role:
|
||||||
missing.append("绑定角色(先校验账号)")
|
missing.append("绑定角色(先校验账号)")
|
||||||
if missing:
|
if missing:
|
||||||
|
action = "进入待充值前" if entering_waiting else "更新充值状态前"
|
||||||
raise CollabError(
|
raise CollabError(
|
||||||
f"进入待充值前还缺:{'、'.join(missing)}",
|
f"{action}还缺:{'、'.join(missing)}",
|
||||||
code="recharge_precondition",
|
code="recharge_precondition",
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -1616,9 +1629,11 @@ def _restore_void_status(
|
|||||||
|
|
||||||
target_index = STAGE_INDEX.get(target)
|
target_index = STAGE_INDEX.get(target)
|
||||||
if target_index is not None and target_index >= STAGE_INDEX[STATUS_TO_RECHARGE]:
|
if target_index is not None and target_index >= STAGE_INDEX[STATUS_TO_RECHARGE]:
|
||||||
_assert_ready_for_recharge(order)
|
_assert_ready_for_recharge(
|
||||||
if target in COLLAB_LEASE_STATUSES:
|
order,
|
||||||
_ensure_order_account_lease(db, current, order, force_active=True)
|
entering_waiting=target == STATUS_TO_RECHARGE,
|
||||||
|
allow_missing_role=target == STATUS_RECHARGED,
|
||||||
|
)
|
||||||
|
|
||||||
old_void_reason = order.void_reason or ""
|
old_void_reason = order.void_reason or ""
|
||||||
# 取消作废等于重新进入状态机:先清空旧流程时间,再按目标状态补当前时间。
|
# 取消作废等于重新进入状态机:先清空旧流程时间,再按目标状态补当前时间。
|
||||||
@@ -1705,6 +1720,7 @@ def set_status(
|
|||||||
current,
|
current,
|
||||||
order,
|
order,
|
||||||
reason="douyu_collab_void",
|
reason="douyu_collab_void",
|
||||||
|
reuse_active_lease=True,
|
||||||
)
|
)
|
||||||
if commit:
|
if commit:
|
||||||
db.commit()
|
db.commit()
|
||||||
@@ -1727,6 +1743,7 @@ def set_status(
|
|||||||
current,
|
current,
|
||||||
order,
|
order,
|
||||||
reason="douyu_collab_failed",
|
reason="douyu_collab_failed",
|
||||||
|
reuse_active_lease=True,
|
||||||
)
|
)
|
||||||
if commit:
|
if commit:
|
||||||
db.commit()
|
db.commit()
|
||||||
@@ -1749,6 +1766,7 @@ def set_status(
|
|||||||
current,
|
current,
|
||||||
order,
|
order,
|
||||||
reason="douyu_collab_points_error",
|
reason="douyu_collab_points_error",
|
||||||
|
reuse_active_lease=True,
|
||||||
)
|
)
|
||||||
if commit:
|
if commit:
|
||||||
db.commit()
|
db.commit()
|
||||||
@@ -1771,6 +1789,7 @@ def set_status(
|
|||||||
current,
|
current,
|
||||||
order,
|
order,
|
||||||
reason="douyu_collab_role_error",
|
reason="douyu_collab_role_error",
|
||||||
|
reuse_active_lease=True,
|
||||||
)
|
)
|
||||||
if commit:
|
if commit:
|
||||||
db.commit()
|
db.commit()
|
||||||
@@ -1793,7 +1812,11 @@ def set_status(
|
|||||||
if target_index < current_index and not rollback:
|
if target_index < current_index and not rollback:
|
||||||
raise CollabError("回退状态必须显式确认", code="rollback_required")
|
raise CollabError("回退状态必须显式确认", code="rollback_required")
|
||||||
if target_index >= STAGE_INDEX[STATUS_TO_RECHARGE]:
|
if target_index >= STAGE_INDEX[STATUS_TO_RECHARGE]:
|
||||||
_assert_ready_for_recharge(order)
|
_assert_ready_for_recharge(
|
||||||
|
order,
|
||||||
|
entering_waiting=target == STATUS_TO_RECHARGE,
|
||||||
|
allow_missing_role=target == STATUS_RECHARGED,
|
||||||
|
)
|
||||||
|
|
||||||
if rollback:
|
if rollback:
|
||||||
for name, field in STATUS_TIMESTAMP_FIELDS.items():
|
for name, field in STATUS_TIMESTAMP_FIELDS.items():
|
||||||
@@ -1822,6 +1845,7 @@ def set_status(
|
|||||||
db,
|
db,
|
||||||
current,
|
current,
|
||||||
order,
|
order,
|
||||||
|
reuse_active_lease=True,
|
||||||
reason=(
|
reason=(
|
||||||
"douyu_collab_done"
|
"douyu_collab_done"
|
||||||
if target == STATUS_DONE
|
if target == STATUS_DONE
|
||||||
|
|||||||
Reference in New Issue
Block a user