开发机迁移

This commit is contained in:
Neo
2026-04-10 06:24:13 +08:00
parent f65c1d038b
commit 79d3c2e97e
50 changed files with 1565 additions and 318 deletions

View File

@@ -1,9 +1,15 @@
# -*- coding: utf-8 -*-
"""数据库连接管理器(限制最大连接超时时间)。"""
import time
import psycopg2
import psycopg2.extras
# 连接重试参数:应对 PostgreSQL 瞬时不可用
_CONNECT_MAX_RETRIES = 3
_CONNECT_RETRY_DELAY = 1.0
class DatabaseConnection:
"""封装 psycopg2 连接,支持会话参数和超时保护。"""
@@ -24,11 +30,25 @@ class DatabaseConnection:
# assumptions: libpq 默认使用系统 locale 的 client_encodingWindows 中文系统为 GBK/CP936
# 边界: 显式指定 client_encoding=utf8 确保连接层始终使用 UTF-8与数据库 server_encoding 一致
# 验证: web-admin 手动触发 ETL 全量 flow不再出现 0xd6 解码错误
conn = psycopg2.connect(
self._dsn,
connect_timeout=timeout_val,
options="-c client_encoding=utf8",
)
last_exc = None
for attempt in range(_CONNECT_MAX_RETRIES):
try:
conn = psycopg2.connect(
self._dsn,
connect_timeout=timeout_val,
options="-c client_encoding=utf8",
keepalives=1,
keepalives_idle=60,
keepalives_interval=10,
keepalives_count=3,
)
break
except psycopg2.OperationalError as e:
last_exc = e
if attempt < _CONNECT_MAX_RETRIES - 1:
time.sleep(_CONNECT_RETRY_DELAY * (attempt + 1))
else:
raise last_exc
conn.autocommit = False
# 会话参数(时区、语句超时等)