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:
dekun
2026-07-01 08:11:42 +08:00
parent 39eac983ff
commit 52aca456e9
23 changed files with 1208 additions and 150 deletions
+25 -2
View File
@@ -616,10 +616,25 @@ def reconcile_monitors_without_position(conn, mode: str, *, grace_sec: int = 120
sym = (p.get("symbol") or "").lower()
direction = p.get("direction") or "long"
position_keys.add((sym, direction))
try:
from ctp_trading_state import trading_state
margin_used = ctp_account_margin_used(mode) or 0.0
for p in trading_state.get_positions() or []:
lots = int(p.get("lots") or 0)
if lots <= 0:
continue
sym = (p.get("symbol") or "").lower()
direction = p.get("direction") or "long"
position_keys.add((sym, direction))
except Exception:
pass
margin_raw = ctp_account_margin_used(mode)
if margin_raw is None:
return 0
margin_used = float(margin_raw or 0.0)
if not position_keys:
if margin_used > 100:
if margin_used > 0:
return 0
try:
bridge = get_bridge()
@@ -686,6 +701,14 @@ def _execute_local_close(
positions = ctp_list_positions(mode)
pos = _find_position(positions, sym, direction)
if not pos:
margin_raw = ctp_account_margin_used(mode)
if margin_raw is not None and float(margin_raw) > 0:
logger.debug(
"skip close monitor=%s: vnpy empty but margin=%.2f",
mon.get("id"),
float(margin_raw),
)
return
_close_all_monitors_for_symbol(conn, sym, direction)
reconcile_monitors_without_position(conn, mode)
return