Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cfa/cloudops/batch_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ def monitor_tasks(
- success (int): Number of tasks that completed successfully
- fail (int): Number of tasks that failed

Raises:
RuntimeError: If the job does not complete within the specified timeout period.
On timeout the dictionary is returned with completed set to False and the
counts from the last poll.

Example:
Monitor a job with default timeout:
Expand Down Expand Up @@ -338,6 +338,7 @@ def monitor_tasks(
logger.debug(
f"Final status: {completions} completed, {incompletions} remaining"
)
break

end_time = datetime.datetime.now().replace(microsecond=0)

Expand Down
33 changes: 33 additions & 0 deletions tests/test_batch_helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import json
from types import SimpleNamespace
from unittest.mock import MagicMock, mock_open, patch
Expand Down Expand Up @@ -86,6 +87,38 @@ def test_monitor_tasks_missing_job_execution_info():
assert result["terminate_reason"] is None


def test_monitor_tasks_returns_when_timeout_expires():
start = datetime.datetime(2026, 1, 1, 12, 0, 0)
clock = [
start,
start + datetime.timedelta(seconds=5),
start + datetime.timedelta(seconds=5),
start + datetime.timedelta(minutes=2),
start + datetime.timedelta(minutes=2),
]
mock_batch_client = MagicMock()
mock_batch_client.get_job.return_value = MagicMock(
state=models.BatchJobState.ACTIVE
)
mock_batch_client.list_tasks.return_value = [
MagicMock(state=models.BatchTaskState.RUNNING)
]

with (
patch("time.sleep", return_value=None),
patch("cfa.cloudops.batch_helpers.datetime") as mock_datetime,
):
mock_datetime.timedelta = datetime.timedelta
mock_datetime.datetime.now.side_effect = clock
result = monitor_tasks(
job_name="my-job", timeout=1, batch_client=mock_batch_client
)

assert result["completed"] is False
assert result["tasks complete"] == 0
assert result["tasks remaining"] == 1


def test_add_task():
mock_batch_client = MagicMock()
task_id_base = "task-base"
Expand Down