fix(deploy): 优化大表迁移并修复后台事件循环

This commit is contained in:
yml
2026-11-20 22:11:54 +08:00
parent 071d45ad1d
commit 409e22be90
7 changed files with 28 additions and 5 deletions
+1
View File
@@ -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 秒仍未拿到就失败,避免把连接池和请求线程长期堵住。
+7 -2
View File
@@ -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
+14
View File
@@ -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)
@@ -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,
),
+1 -1
View File
@@ -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,
)
+2 -1
View File
@@ -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:
@@ -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)