Skip to content

Commit

Permalink
continue redefining what finished/completed scan is - final
Browse files Browse the repository at this point in the history
  • Loading branch information
ric-evans committed Jan 29, 2025
1 parent 4d84ca3 commit bdee3d9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ None

There are several codes for `scan_state`:

- Successful state
* `SCAN_FINISHED_SUCCESSFULLY`
- Successful state (completed scan)
* `SCAN_HAS_FINAL_RESULT`
- Non-finished scan states (in reverse order of occurrence)
* `IN_PROGRESS__PARTIAL_RESULT_GENERATED`
* `IN_PROGRESS__WAITING_ON_FIRST_PIXEL_RECO`
Expand Down
13 changes: 6 additions & 7 deletions skydriver/rest_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from .ewms import request_stop_on_ewms
from .k8s.scan_backlog import put_on_backlog
from .k8s.scanner_instance import SkyScanK8sJobFactory
from .utils import get_scan_state
from .utils import does_scan_state_indicate_final_result_received, get_scan_state

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -741,8 +741,9 @@ async def delete(self, scan_id: str) -> None:

# check DB states
manifest = await self.manifests.get(scan_id, True)
_, scan_complete = await get_scan_state(manifest, self.ewms_rc, self.results)
if scan_complete:
if does_scan_state_indicate_final_result_received(
await get_scan_state(manifest, self.ewms_rc, self.results)
):
msg = "Attempted to delete a completed scan (must use `delete_completed_scan=True`)"
raise web.HTTPError(
400,
Expand Down Expand Up @@ -1046,9 +1047,7 @@ async def get(self, scan_id: str) -> None:
LOGGER.exception(e)

# scan state
scan_state, scan_complete = await get_scan_state(
manifest, self.ewms_rc, self.results
)
scan_state = await get_scan_state(manifest, self.ewms_rc, self.results)

# ewms
if (
Expand All @@ -1065,7 +1064,7 @@ async def get(self, scan_id: str) -> None:
resp = {
"scan_state": scan_state,
"is_deleted": manifest.is_deleted,
"scan_complete": scan_complete,
"scan_complete": does_scan_state_indicate_final_result_received(scan_state),
"pods": pods_411,
"clusters": clusters,
}
Expand Down
23 changes: 13 additions & 10 deletions skydriver/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class _ScanState(enum.Enum):
"""A non-persisted scan state."""

SCAN_FINISHED_SUCCESSFULLY = enum.auto()
SCAN_HAS_FINAL_RESULT = enum.auto()
# ^^^ indicates the scanner sent finished results. in reality, the scanner or ewms
# could've crashed immediately after BUT the user only cares about the RESULTS--so,
# this would still be considered a SUCCESS in *this* context
Expand All @@ -23,20 +23,23 @@ class _ScanState(enum.Enum):
PENDING__PRESTARTUP = enum.auto()


def does_scan_state_indicate_final_result_received(state: str) -> bool:
"""Does the scan state indicate has result?"""
return state == _ScanState.SCAN_HAS_FINAL_RESULT.value


async def get_scan_state(
manifest: Manifest,
ewms_rc: RestClient,
results: database.interface.ResultClient,
) -> tuple[str, bool]:
) -> str:
"""Determine the state of the scan by parsing attributes and talking with EWMS.
Returns tuple:
1. the state as a human-readable string
2. a bool for whether the scan was successful or not
Returns the state as a human-readable string
"""
if (await results.get(manifest.scan_id)).is_final:
# NOTE: see note on 'SCAN_FINISHED_SUCCESSFULLY' above
return _ScanState.SCAN_FINISHED_SUCCESSFULLY.name, True
# NOTE: see note on 'SCAN_HAS_FINAL_RESULT' above
return _ScanState.SCAN_HAS_FINAL_RESULT.name

def _has_cleared_backlog() -> bool:
return bool(
Expand Down Expand Up @@ -80,11 +83,11 @@ def get_nonfinished_state() -> _ScanState:
and manifest.ewms_task.get("complete")
):
# we didn't have info on what kind of stop
return f"STOPPED__{state.split('__')[1]}", False
return f"STOPPED__{state.split('__')[1]}"
# has EWMS ceased running the scan workers?
elif dtype := await ewms.get_deactivated_type(ewms_rc, manifest.ewms_workflow_id):
# -> yes, the ewms workflow has been deactivated
return f"{dtype.upper()}__{state.split('__')[1]}", False
return f"{dtype.upper()}__{state.split('__')[1]}"
else:
# -> no, this is a non-finished scan
return state, False
return state
19 changes: 8 additions & 11 deletions tests/unit/test_scan_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
"processing_stats_is_finished",
[True, False],
)
async def test_00__scan_finished_successfully(
async def test_00__scan_has_final_result(
processing_stats_is_finished: bool,
) -> None:
"""Test with SCAN_FINISHED_SUCCESSFULLY.
"""Test with SCAN_HAS_FINAL_RESULT.
`processing_stats.is_finished` does not affect "SCAN_FINISHED_SUCCESSFULLY"
`processing_stats.is_finished` does not affect "SCAN_HAS_FINAL_RESULT"
"""
ewms_rc = MagicMock()
results = MagicMock(get=AsyncMock(return_value=MagicMock(is_final=True)))
Expand All @@ -41,10 +41,7 @@ async def test_00__scan_finished_successfully(
),
)

assert await get_scan_state(manifest, ewms_rc, results) == (
"SCAN_FINISHED_SUCCESSFULLY",
True,
)
assert await get_scan_state(manifest, ewms_rc, results) == "SCAN_HAS_FINAL_RESULT"


@pytest.mark.parametrize(
Expand Down Expand Up @@ -83,7 +80,7 @@ async def test_10__partial_result_generated(ewms_dtype: str | None, state: str)
)

with patch("skydriver.ewms.get_deactivated_type", return_value=ewms_dtype):
assert await get_scan_state(manifest, ewms_rc, results) == (state, False)
assert await get_scan_state(manifest, ewms_rc, results) == state


@pytest.mark.parametrize(
Expand Down Expand Up @@ -124,7 +121,7 @@ async def test_20__waiting_on_first_pixel_reco(
)

with patch("skydriver.ewms.get_deactivated_type", return_value=ewms_dtype):
assert await get_scan_state(manifest, ewms_rc, results) == (state, False)
assert await get_scan_state(manifest, ewms_rc, results) == state


@pytest.mark.parametrize(
Expand Down Expand Up @@ -155,7 +152,7 @@ async def test_40__waiting_on_scanner_server_startup(
)

with patch("skydriver.ewms.get_deactivated_type", return_value=ewms_dtype):
assert await get_scan_state(manifest, ewms_rc, results) == (state, False)
assert await get_scan_state(manifest, ewms_rc, results) == state


@pytest.mark.parametrize(
Expand Down Expand Up @@ -184,4 +181,4 @@ async def test_50__prestartup(ewms_dtype: str | None, state: str) -> None:
)

with patch("skydriver.ewms.get_deactivated_type", return_value=ewms_dtype):
assert await get_scan_state(manifest, ewms_rc, results) == (state, False)
assert await get_scan_state(manifest, ewms_rc, results) == state

0 comments on commit bdee3d9

Please sign in to comment.