fix: MySQL 连接池配置优化,防止定时任务连接断开

- config.py: 从 DSN 字符串改为 dict 配置,加入 minsize/maxsize/connect_timeout
- scheduler.py: company_cleaning_job 执行前先 SELECT 1 检查连接可用性
This commit is contained in:
win 2026-03-23 10:55:13 +08:00
parent 78eee99c2f
commit b0a755a443
2 changed files with 23 additions and 1 deletions

View File

@ -181,8 +181,17 @@ async def _get_active_proxy() -> "str | None":
async def company_cleaning_job():
"""每5分钟执行自动清洗待处理公司数据"""
from tortoise import Tortoise
from app.services.company_cleaner import company_cleaner
# 检查连接池是否可用,不可用则跳过本轮
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
task_id = str(uuid.uuid4())
started_at = datetime.now()
task_name = "company_cleaning_job"

View File

@ -49,7 +49,20 @@ class Settings(BaseSettings):
ALERT_WINDOW_MINUTES: int = 10
TORTOISE_ORM: dict = {
"connections": {
"default": "mysql://root:jobdata123@121.4.126.241:3306/job_data"
"default": {
"engine": "tortoise.backends.mysql",
"credentials": {
"host": "121.4.126.241",
"port": 3306,
"user": "root",
"password": "jobdata123",
"database": "job_data",
"minsize": 1,
"maxsize": 10,
"connect_timeout": 10,
"charset": "utf8mb4",
},
},
},
"apps": {
"models": {