Add PostgreSQL production backend to eliminate SQLite lock contention.
Support DATABASE_URL with connection pooling, pg_dump backups, SQLite migration script, and deploy_postgres.sh with docs. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+10
-23
@@ -3,7 +3,7 @@
|
||||
# 严禁用于:带单/代客理财、向他人推荐期货品种或买卖建议、融资配资等业务。
|
||||
# 详见 LICENSE.zh-CN.txt 与 docs/软件购买与使用协议.md
|
||||
|
||||
"""CTP 按计划自动连接/断开:盘前 30 分钟连,日盘/夜盘收盘后 30 分钟断。"""
|
||||
"""CTP 按计划自动连接:盘前 30 分钟检查;交易时段断线后台重连;不自动强制断开。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
@@ -18,11 +18,13 @@ from market_sessions import (
|
||||
is_trading_session,
|
||||
should_keep_ctp_connected,
|
||||
)
|
||||
from vnpy_bridge import ctp_disconnect, ctp_start_connect, ctp_status
|
||||
from vnpy_bridge import ctp_start_connect, ctp_status
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
CHECK_INTERVAL_SEC = 60
|
||||
TRADING_CHECK_INTERVAL_SEC = 15
|
||||
PREMARKET_CHECK_INTERVAL_SEC = 30
|
||||
DEFAULT_MINUTES_BEFORE = 30
|
||||
DEFAULT_MINUTES_AFTER = 30
|
||||
|
||||
@@ -49,14 +51,6 @@ def _scheduled_connect_enabled() -> bool:
|
||||
)
|
||||
|
||||
|
||||
def _scheduled_disconnect_enabled() -> bool:
|
||||
return (os.getenv("CTP_POSTMARKET_DISCONNECT", "true") or "true").strip().lower() in (
|
||||
"1",
|
||||
"true",
|
||||
"yes",
|
||||
)
|
||||
|
||||
|
||||
def should_auto_connect_now(*, minutes_before: int | None = None) -> bool:
|
||||
"""是否应保持/发起 CTP 连接(供重连、权限判断复用)。"""
|
||||
mins_b = premarket_minutes_before() if minutes_before is None else minutes_before
|
||||
@@ -76,7 +70,7 @@ def start_ctp_premarket_connect_worker(
|
||||
get_setting_fn: Callable[[str, str], str] | None = None,
|
||||
interval: int = CHECK_INTERVAL_SEC,
|
||||
) -> None:
|
||||
"""盘前自动连接;日盘/夜盘收盘宽限结束后自动断开。"""
|
||||
"""盘前 30 分钟:未连接则自动连;已连接则不重复发起。不自动强制断开。"""
|
||||
|
||||
def _loop() -> None:
|
||||
time.sleep(10)
|
||||
@@ -101,7 +95,7 @@ def start_ctp_premarket_connect_worker(
|
||||
logger.info("交易时段内自动连接 CTP [%s]", mode)
|
||||
elif in_postmarket_grace_window(minutes_after=mins_a):
|
||||
logger.info(
|
||||
"盘后宽限期内保持/恢复 CTP 连接 [%s](收盘后 %d 分钟内)",
|
||||
"盘后宽限期内恢复 CTP 连接 [%s](收盘后 %d 分钟内)",
|
||||
mode,
|
||||
mins_a,
|
||||
)
|
||||
@@ -111,17 +105,10 @@ def start_ctp_premarket_connect_worker(
|
||||
mode,
|
||||
mins_b,
|
||||
)
|
||||
if not is_trading_session() and in_premarket_connect_window(
|
||||
minutes_before=mins_b,
|
||||
):
|
||||
sleep_sec = 30
|
||||
elif _scheduled_disconnect_enabled() and st.get("connected"):
|
||||
ctp_disconnect()
|
||||
logger.info(
|
||||
"盘后自动断开 CTP [%s](日盘/夜盘结束 %d 分钟后)",
|
||||
mode,
|
||||
mins_a,
|
||||
)
|
||||
if is_trading_session():
|
||||
sleep_sec = TRADING_CHECK_INTERVAL_SEC
|
||||
elif in_premarket_connect_window(minutes_before=mins_b):
|
||||
sleep_sec = PREMARKET_CHECK_INTERVAL_SEC
|
||||
except Exception as exc:
|
||||
logger.warning("CTP scheduled connect worker: %s", exc)
|
||||
time.sleep(sleep_sec)
|
||||
|
||||
Reference in New Issue
Block a user