修复监控握锁卖币导致中控快照超时、实例全部离线。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -375,15 +375,16 @@ def sync_open_options_trades(
|
|||||||
pass
|
pass
|
||||||
if notify_cfg is not None:
|
if notify_cfg is not None:
|
||||||
_notify_recent_unsent_expiry_closes(conn, notify_cfg)
|
_notify_recent_unsent_expiry_closes(conn, notify_cfg)
|
||||||
_sell_spot_after_synced_closes(notify_cfg, closed_inst_ids)
|
# 卖币不在此处做:调用方须先 commit/close 本连接,再调 sell_spot_after_options_sync,
|
||||||
|
# 否则监控线程握锁时中控快照会超时,表现为全部离线。
|
||||||
return updated
|
return updated
|
||||||
|
|
||||||
|
|
||||||
def _sell_spot_after_synced_closes(
|
def sell_spot_after_options_sync(
|
||||||
cfg: dict[str, Any],
|
cfg: dict[str, Any] | None,
|
||||||
closed_inst_ids: list[str],
|
closed_inst_ids: list[str] | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""同步关仓(含到期)后卖回币本位残留;空仓时再扫一遍交易户."""
|
"""在释放业务 DB 连接后调用:同步关仓/到期后的币本位卖回."""
|
||||||
if not isinstance(cfg, dict):
|
if not isinstance(cfg, dict):
|
||||||
return
|
return
|
||||||
ex = cfg.get("exchange_options")
|
ex = cfg.get("exchange_options")
|
||||||
@@ -540,9 +541,14 @@ def options_monitor_loop(
|
|||||||
pe_cfg = dict(profit_exit_cfg or {})
|
pe_cfg = dict(profit_exit_cfg or {})
|
||||||
pe_cfg.setdefault("send_wechat", send_wechat)
|
pe_cfg.setdefault("send_wechat", send_wechat)
|
||||||
pe_cfg.setdefault("account_label", account_label)
|
pe_cfg.setdefault("account_label", account_label)
|
||||||
|
# 平仓/卖币会另开 DB 连接;先提交释放写锁,避免中控快照被锁超时→全部离线
|
||||||
if target_close_fn is not None:
|
if target_close_fn is not None:
|
||||||
from lib.options.options_target_lib import run_options_target_closes
|
from lib.options.options_target_lib import run_options_target_closes
|
||||||
|
|
||||||
|
try:
|
||||||
|
conn.commit()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
run_options_target_closes(
|
run_options_target_closes(
|
||||||
conn,
|
conn,
|
||||||
positions,
|
positions,
|
||||||
@@ -554,6 +560,10 @@ def options_monitor_loop(
|
|||||||
if profit_exit_close_fn is not None:
|
if profit_exit_close_fn is not None:
|
||||||
from lib.options.options_profit_exit_lib import run_options_profit_exits
|
from lib.options.options_profit_exit_lib import run_options_profit_exits
|
||||||
|
|
||||||
|
try:
|
||||||
|
conn.commit()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
run_options_profit_exits(
|
run_options_profit_exits(
|
||||||
conn,
|
conn,
|
||||||
positions,
|
positions,
|
||||||
@@ -564,10 +574,22 @@ def options_monitor_loop(
|
|||||||
ex=pe_cfg.get("exchange_options"),
|
ex=pe_cfg.get("exchange_options"),
|
||||||
)
|
)
|
||||||
if sync_trades_fn is not None:
|
if sync_trades_fn is not None:
|
||||||
|
try:
|
||||||
|
conn.commit()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
sync_trades_fn(conn)
|
sync_trades_fn(conn)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
# 币本位扫尾卖回:必须在监控连接关闭之后
|
||||||
|
try:
|
||||||
|
pe_sell = dict(profit_exit_cfg or {})
|
||||||
|
pe_sell.setdefault("send_wechat", send_wechat)
|
||||||
|
pe_sell.setdefault("account_label", account_label)
|
||||||
|
sell_spot_after_options_sync(pe_sell)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
# 平仓限价挂单超时撤单(独立于 DB 事务)
|
# 平仓限价挂单超时撤单(独立于 DB 事务)
|
||||||
if stale_pending_fn is not None:
|
if stale_pending_fn is not None:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -459,9 +459,9 @@ def _sync_options_trades(
|
|||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
try:
|
try:
|
||||||
from lib.options.options_coin_open_lib import sweep_spot_after_options_flat
|
from lib.options.options_monitor_lib import sell_spot_after_options_sync
|
||||||
|
|
||||||
sweep_spot_after_options_flat(cfg, ex)
|
sell_spot_after_options_sync(cfg)
|
||||||
_mark_balances_stale(cfg)
|
_mark_balances_stale(cfg)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@@ -1991,20 +1991,12 @@ def _start_monitor_thread(app: Flask, cfg: dict[str, Any]) -> None:
|
|||||||
return 0
|
return 0
|
||||||
live_ids = {str(p.get("instId") or "") for p in raw if str(p.get("instId") or "")}
|
live_ids = {str(p.get("instId") or "") for p in raw if str(p.get("instId") or "")}
|
||||||
reconcile_live_open_trades(conn, live_inst_ids=live_ids)
|
reconcile_live_open_trades(conn, live_inst_ids=live_ids)
|
||||||
n = sync_open_options_trades(
|
return sync_open_options_trades(
|
||||||
conn,
|
conn,
|
||||||
live_inst_ids=live_ids,
|
live_inst_ids=live_ids,
|
||||||
fetch_history_fn=lambda inst_id: fetch_option_position_history(ex, inst_id),
|
fetch_history_fn=lambda inst_id: fetch_option_position_history(ex, inst_id),
|
||||||
notify_cfg=cfg,
|
notify_cfg=cfg,
|
||||||
)
|
)
|
||||||
try:
|
|
||||||
from lib.options.options_coin_open_lib import sweep_spot_after_options_flat
|
|
||||||
|
|
||||||
sweep_spot_after_options_flat(cfg, ex)
|
|
||||||
_mark_balances_stale(cfg)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
return n
|
|
||||||
|
|
||||||
def _target_close(inst_id: str) -> dict[str, Any]:
|
def _target_close(inst_id: str) -> dict[str, Any]:
|
||||||
from lib.options.options_target_lib import close_option_by_bid_depth
|
from lib.options.options_target_lib import close_option_by_bid_depth
|
||||||
|
|||||||
Reference in New Issue
Block a user