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:
@@ -255,3 +255,26 @@ def test_a_locked_run_storage_is_read_again(monkeypatch):
|
|||||||
with pytest.raises(HTTPException) as raised:
|
with pytest.raises(HTTPException) as raised:
|
||||||
runs.graphql("q", {})
|
runs.graphql("q", {})
|
||||||
assert raised.value.status_code == 503
|
assert raised.value.status_code == 503
|
||||||
|
|
||||||
|
|
||||||
|
def test_cancel_waits_for_a_starting_run(monkeypatch):
|
||||||
|
"""Dagster refuses to terminate a run that is still STARTING: cancel waits for it to start."""
|
||||||
|
from webapi import runs
|
||||||
|
|
||||||
|
statuses = iter(["STARTING", "STARTING", "STARTED", "CANCELING"])
|
||||||
|
terminated = []
|
||||||
|
|
||||||
|
class Client:
|
||||||
|
def get_run_status(self, run_id):
|
||||||
|
class S:
|
||||||
|
value = next(statuses)
|
||||||
|
|
||||||
|
return S()
|
||||||
|
|
||||||
|
def terminate_run(self, run_id):
|
||||||
|
terminated.append(run_id)
|
||||||
|
|
||||||
|
monkeypatch.setattr(runs.time, "sleep", lambda s: None)
|
||||||
|
answer = runs.cancel_run("r1", client=Client())
|
||||||
|
assert terminated == ["r1"]
|
||||||
|
assert answer.status == "CANCELING"
|
||||||
|
|||||||
@@ -108,6 +108,7 @@ def dagster() -> DagsterGraphQLClient:
|
|||||||
|
|
||||||
LOCKED = "database is locked" # Dagster's SQLite run storage, busy while a run writes its events
|
LOCKED = "database is locked" # Dagster's SQLite run storage, busy while a run writes its events
|
||||||
LOCK_RETRIES = 5
|
LOCK_RETRIES = 5
|
||||||
|
STARTING_WAIT_S = 30
|
||||||
|
|
||||||
|
|
||||||
def _locked(payload: dict) -> bool:
|
def _locked(payload: dict) -> bool:
|
||||||
@@ -243,8 +244,16 @@ def _status(client: DagsterGraphQLClient, run_id: str) -> str:
|
|||||||
@router.post("/{run_id}/cancel")
|
@router.post("/{run_id}/cancel")
|
||||||
def cancel_run(run_id: str, client: DagsterGraphQLClient = Depends(dagster)) -> Cancel:
|
def cancel_run(run_id: str, client: DagsterGraphQLClient = Depends(dagster)) -> Cancel:
|
||||||
status = _status(client, run_id)
|
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"):
|
if status in ("SUCCESS", "FAILURE", "CANCELED"):
|
||||||
raise HTTPException(409, detail=f"the run already ended: {STATE[status]}")
|
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:
|
try:
|
||||||
client.terminate_run(run_id)
|
client.terminate_run(run_id)
|
||||||
except DagsterGraphQLClientError as e:
|
except DagsterGraphQLClientError as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user