fix: MySQL 连接池断开时主动重建而非跳过
_ensure_db_connection: SELECT 1 探活,失败则 close + reinit 重建连接池
This commit is contained in:
parent
b0a755a443
commit
f77c010330
@ -179,18 +179,29 @@ async def _get_active_proxy() -> "str | None":
|
||||
return None
|
||||
|
||||
|
||||
async def company_cleaning_job():
|
||||
"""每5分钟执行:自动清洗待处理公司数据"""
|
||||
async def _ensure_db_connection():
|
||||
"""确保 MySQL 连接池可用,不可用时重建"""
|
||||
from tortoise import Tortoise
|
||||
from app.services.company_cleaner import company_cleaner
|
||||
from app.settings import TORTOISE_ORM
|
||||
|
||||
# 检查连接池是否可用,不可用则跳过本轮
|
||||
try:
|
||||
conn = Tortoise.get_connection("default")
|
||||
await conn.execute_query("SELECT 1")
|
||||
except Exception as e:
|
||||
logger.warning(f"company_cleaning_job skipped: DB connection unavailable: {e}")
|
||||
return
|
||||
logger.warning(f"MySQL 连接池不可用({e}),尝试重建...")
|
||||
try:
|
||||
await Tortoise.close_connections()
|
||||
except Exception:
|
||||
pass
|
||||
await Tortoise.init(config=TORTOISE_ORM)
|
||||
logger.info("MySQL 连接池重建成功")
|
||||
|
||||
|
||||
async def company_cleaning_job():
|
||||
"""每5分钟执行:自动清洗待处理公司数据"""
|
||||
from app.services.company_cleaner import company_cleaner
|
||||
|
||||
await _ensure_db_connection()
|
||||
|
||||
task_id = str(uuid.uuid4())
|
||||
started_at = datetime.now()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user