Cancel waits up to 30 s for a run that is still starting (Dagster cannot terminate it then); still starting is a 409 with Retry-After
This commit is contained in:
@@ -108,6 +108,7 @@ def dagster() -> DagsterGraphQLClient:
|
||||
|
||||
LOCKED = "database is locked" # Dagster's SQLite run storage, busy while a run writes its events
|
||||
LOCK_RETRIES = 5
|
||||
STARTING_WAIT_S = 30
|
||||
|
||||
|
||||
def _locked(payload: dict) -> bool:
|
||||
@@ -243,8 +244,16 @@ def _status(client: DagsterGraphQLClient, run_id: str) -> str:
|
||||
@router.post("/{run_id}/cancel")
|
||||
def cancel_run(run_id: str, client: DagsterGraphQLClient = Depends(dagster)) -> Cancel:
|
||||
status = _status(client, run_id)
|
||||
deadline = time.monotonic() + STARTING_WAIT_S # Dagster cannot terminate a run still starting
|
||||
while status == "STARTING" and time.monotonic() < deadline:
|
||||
time.sleep(0.5)
|
||||
status = _status(client, run_id)
|
||||
if status in ("SUCCESS", "FAILURE", "CANCELED"):
|
||||
raise HTTPException(409, detail=f"the run already ended: {STATE[status]}")
|
||||
if status == "STARTING":
|
||||
raise HTTPException(
|
||||
409, detail="the run is still starting; try again", headers={"Retry-After": "5"}
|
||||
)
|
||||
try:
|
||||
client.terminate_run(run_id)
|
||||
except DagsterGraphQLClientError as e:
|
||||
|
||||
Reference in New Issue
Block a user