Make strategy stop idempotent
This commit is contained in:
parent
c8edbf1a98
commit
4ae42baeb1
@ -22,8 +22,20 @@ def start(req: StrategyStartRequest, request: Request):
|
||||
|
||||
@router.post("/strategy/stop")
|
||||
def stop(request: Request):
|
||||
try:
|
||||
user_id = get_request_user_id(request)
|
||||
return stop_strategy(user_id)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as exc:
|
||||
print(f"[STRATEGY] unhandled stop route failure: {exc}", flush=True)
|
||||
return JSONResponse(
|
||||
status_code=200,
|
||||
content={
|
||||
"status": "stop_failed",
|
||||
"message": f"Unable to stop strategy: {exc}",
|
||||
},
|
||||
)
|
||||
|
||||
@router.post("/strategy/resume")
|
||||
def resume(request: Request):
|
||||
|
||||
@ -585,13 +585,27 @@ def resume_running_runs():
|
||||
_write_status(user_id, run_id, "RUNNING")
|
||||
|
||||
def stop_strategy(user_id: str):
|
||||
run_id = get_active_run_id(user_id)
|
||||
run_id = get_running_run_id(user_id)
|
||||
if not run_id:
|
||||
latest_run_id = get_active_run_id(user_id)
|
||||
return {"status": "already_stopped", "run_id": latest_run_id}
|
||||
|
||||
engine_external = os.getenv("ENGINE_EXTERNAL", "").strip().lower() in {"1", "true", "yes"}
|
||||
stop_warning = None
|
||||
if not engine_external:
|
||||
try:
|
||||
stop_engine(user_id, run_id, timeout=15.0)
|
||||
except Exception as exc:
|
||||
print(f"[STRATEGY] stop_engine failed for {user_id}/{run_id}: {exc}", flush=True)
|
||||
stop_warning = str(exc)
|
||||
deactivate_strategy_config(user_id, run_id)
|
||||
stop_run(user_id, run_id, reason="user_request")
|
||||
try:
|
||||
_write_status(user_id, run_id, "STOPPED")
|
||||
except Exception as exc:
|
||||
print(f"[STRATEGY] engine status update failed during stop for {user_id}/{run_id}: {exc}", flush=True)
|
||||
if not stop_warning:
|
||||
stop_warning = str(exc)
|
||||
update_run_status(user_id, run_id, "STOPPED", meta={"reason": "user_request"})
|
||||
|
||||
try:
|
||||
@ -602,7 +616,10 @@ def stop_strategy(user_id: str):
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return {"status": "stopped"}
|
||||
result = {"status": "stopped", "run_id": run_id}
|
||||
if stop_warning:
|
||||
result["warning"] = stop_warning
|
||||
return result
|
||||
|
||||
|
||||
def resume_strategy(user_id: str):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user