Skip to content

Commit

Permalink
Wait for batch process API job to update status from PROCESSING to so…
Browse files Browse the repository at this point in the history
…mething else (#536)

* add a waiting section for batch job status update

* mock also the batch request update part, fix log and sleep call counts

---------

Co-authored-by: Matic Lubej <[email protected]>
  • Loading branch information
mlubej and Matic Lubej authored Aug 1, 2024
1 parent ca4103c commit bc9abf1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions sentinelhub/api/batch/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ def monitor_batch_job(
failed_tiles_num = finished_count - success_count
if failed_tiles_num:
LOGGER.info("Batch job failed for %d tiles", failed_tiles_num)

LOGGER.info("Waiting on batch job status update.")
while batch_request.status is BatchRequestStatus.PROCESSING:
time.sleep(sleep_time)
batch_request = batch_client.get_request(batch_request)

LOGGER.info("Batch job finished with status %s", batch_request.status.value)
return tiles_per_status


Expand Down
8 changes: 6 additions & 2 deletions tests/api/batch/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ def test_monitor_batch_process_job(
monitor_analysis_mock = mocker.patch("sentinelhub.api.batch.utils.monitor_batch_analysis")
monitor_analysis_mock.return_value = batch_request

updated_batch_request = BatchRequest.from_dict({**batch_request.to_dict(), "status": BatchRequestStatus.DONE})
batch_request_update_mock = mocker.patch("sentinelhub.SentinelHubBatch.get_request")
batch_request_update_mock.return_value = updated_batch_request

batch_tiles_mock = mocker.patch("sentinelhub.SentinelHubBatch.iter_tiles")
batch_tiles_mock.side_effect = tiles_sequence

Expand All @@ -94,12 +98,12 @@ def test_monitor_batch_process_job(
assert batch_tiles_mock.call_count == progress_loop_counts + 1
assert all(call.args == (batch_request,) and call.kwargs == {} for call in batch_tiles_mock.mock_calls)

assert sleep_mock.call_count == progress_loop_counts
assert sleep_mock.call_count == progress_loop_counts + batch_request_update_mock.call_count
assert all(call.args == (sleep_time,) and call.kwargs == {} for call in sleep_mock.mock_calls)

is_processing_logged = batch_status is BatchRequestStatus.PROCESSING
is_failure_logged = BatchTileStatus.FAILED in tile_status_sequence[-1]
assert logging_mock.call_count == int(is_processing_logged) + int(is_failure_logged)
assert logging_mock.call_count == int(is_processing_logged) + int(is_failure_logged) + 2


def _tile_status_counts_to_tiles(tile_status_counts: dict[BatchTileStatus, int]) -> list[dict[str, str]]:
Expand Down

0 comments on commit bc9abf1

Please sign in to comment.