From 409e22be90b9d7d116ead52acd09bad85e311598 Mon Sep 17 00:00:00 2001 From: yml Date: Fri, 20 Nov 2026 22:11:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(deploy):=20=E4=BC=98=E5=8C=96=E5=A4=A7?= =?UTF-8?q?=E8=A1=A8=E8=BF=81=E7=A7=BB=E5=B9=B6=E4=BF=AE=E5=A4=8D=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E4=BA=8B=E4=BB=B6=E5=BE=AA=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 1 + deploy.sh | 9 +++++++-- tests/test_platform_fulfillment.py | 14 ++++++++++++++ .../20261120_0101_douyu_task_durable_schedule.py | 2 +- web/backend/models.py | 2 +- web/backend/services/douyu_batch_launcher.py | 3 ++- .../services/platform_fulfillment/service.py | 2 ++ 7 files changed, 28 insertions(+), 5 deletions(-) diff --git a/.env.example b/.env.example index 6b8b312..9f38f9c 100644 --- a/.env.example +++ b/.env.example @@ -68,6 +68,7 @@ DB_POOL_SIZE=20 DB_MAX_OVERFLOW=20 WORKER_DB_POOL_SIZE=12 WORKER_DB_MAX_OVERFLOW=4 +DEPLOY_STARTUP_TIMEOUT_SECONDS=1800 DB_POOL_TIMEOUT=30 DB_POOL_RECYCLE=1800 # 行锁 10 秒仍未拿到就失败,避免把连接池和请求线程长期堵住。 diff --git a/deploy.sh b/deploy.sh index 79efac0..79ecd08 100755 --- a/deploy.sh +++ b/deploy.sh @@ -160,10 +160,13 @@ cmd_deploy() { echo "" echo "等待服务启动..." + local startup_timeout="${DEPLOY_STARTUP_TIMEOUT_SECONDS:-1800}" + local max_attempts=$(((startup_timeout + 1) / 2)) + echo " 启动超时: ${startup_timeout}s(数据库大表迁移可能持续数分钟)" # 健康检查 # 30 秒不够:mysql 一起重建时应用就绪约 35 秒,会误报「启动超时」。 - for i in $(seq 1 40); do + for i in $(seq 1 "$max_attempts"); do if curl -s "http://localhost:${APP_PORT}/api/health" | grep -q "ok" 2>/dev/null; then echo "" echo "服务已响应,等待 Docker 健康检查..." @@ -189,7 +192,9 @@ cmd_deploy() { echo "==============================" exit 0 fi - echo " 等待中... ($i/40)" + if [ $((i % 15)) -eq 0 ] || [ "$i" = "$max_attempts" ]; then + echo " 等待中... ($i/$max_attempts,已等待 $((i * 2))s)" + fi sleep 2 done diff --git a/tests/test_platform_fulfillment.py b/tests/test_platform_fulfillment.py index 3e7bc96..311ba4b 100644 --- a/tests/test_platform_fulfillment.py +++ b/tests/test_platform_fulfillment.py @@ -1464,6 +1464,20 @@ class TestBindingAndCompletion(_Base): self.session.refresh(task) assert task.bind_task_id == bind_tasks[0].id + def test_customer_started_binding_does_not_capture_request_loop(self, monkeypatch): + task = self._allocated() + captured: dict[str, object] = {} + monkeypatch.setattr( + service, + "start_douyu_batch", + lambda **kwargs: captured.update(kwargs), + ) + + asyncio.run(service.customer_start_binding(self.session, task_id=task.id)) + + assert captured["capture_event_loop"] is False + assert "loop" not in captured + def test_concurrent_start_binding_creates_one_task(self, monkeypatch): task = self._allocated() monkeypatch.setattr(service, "start_douyu_batch", lambda **_kwargs: None) diff --git a/web/backend/migrations/versions/20261120_0101_douyu_task_durable_schedule.py b/web/backend/migrations/versions/20261120_0101_douyu_task_durable_schedule.py index b586d0b..0c08ad4 100644 --- a/web/backend/migrations/versions/20261120_0101_douyu_task_durable_schedule.py +++ b/web/backend/migrations/versions/20261120_0101_douyu_task_durable_schedule.py @@ -79,7 +79,7 @@ def upgrade() -> None: "AND platform_task_id IS NOT NULL " "AND status IN ('planned', 'pending', 'running') " "THEN CONCAT('bind:', platform_task_id) ELSE NULL END", - persisted=True, + persisted=False, ), nullable=True, ), diff --git a/web/backend/models.py b/web/backend/models.py index f086ae5..d7afbd9 100644 --- a/web/backend/models.py +++ b/web/backend/models.py @@ -390,7 +390,7 @@ class DouyuTask(Base): "AND platform_task_id IS NOT NULL " "AND status IN ('planned', 'pending', 'running') " "THEN CONCAT('bind:', platform_task_id) ELSE NULL END", - persisted=True, + persisted=False, ), nullable=True, ) diff --git a/web/backend/services/douyu_batch_launcher.py b/web/backend/services/douyu_batch_launcher.py index a8b53bc..7f0aebb 100644 --- a/web/backend/services/douyu_batch_launcher.py +++ b/web/backend/services/douyu_batch_launcher.py @@ -38,9 +38,10 @@ def start_douyu_batch( concurrency: int, loop: asyncio.AbstractEventLoop | None = None, log_queue: asyncio.Queue | None = None, + capture_event_loop: bool = True, ) -> None: """Create the batch coordinator; websocket logging is optional.""" - if loop is None: + if loop is None and capture_event_loop: try: loop = asyncio.get_running_loop() except RuntimeError: diff --git a/web/backend/services/platform_fulfillment/service.py b/web/backend/services/platform_fulfillment/service.py index 281ccd7..65fe65f 100644 --- a/web/backend/services/platform_fulfillment/service.py +++ b/web/backend/services/platform_fulfillment/service.py @@ -2052,6 +2052,7 @@ async def start_binding( task_type="get_bind_qr", payload={}, concurrency=1, + capture_event_loop=False, ) return get_task_detail(db, task.id) @@ -2269,6 +2270,7 @@ async def confirm_binding( task_type="confirm_bind", payload={}, concurrency=1, + capture_event_loop=False, ) return get_task_detail(db, task.id)