Skip to content

Commit 562d5b4

Browse files
fix: add pagination for list incomplete tasks
1 parent 13866bf commit 562d5b4

File tree

2 files changed

+64
-14
lines changed

2 files changed

+64
-14
lines changed

src/taskgraph/util/taskcluster.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -411,20 +411,27 @@ def send_email(address, subject, content, link):
411411
def list_task_group_incomplete_tasks(task_group_id):
412412
"""Generate the incomplete tasks in a task group"""
413413
queue = get_taskcluster_client("queue")
414-
response = queue.listTaskGroup(task_group_id)
415-
416-
if not response or "tasks" not in response:
417-
return
418-
419-
tasks = response.get("tasks", [])
420-
for task in tasks:
421-
if (status := task.get("status")) is not None: # type: ignore
422-
if (task_id := status.get("taskId")) and status.get("state") in [
423-
"running",
424-
"pending",
425-
"unscheduled",
426-
]:
427-
yield task_id
414+
415+
incomplete_tasks = []
416+
417+
def pagination_handler(response):
418+
if not response or "tasks" not in response:
419+
return
420+
421+
tasks = response.get("tasks", [])
422+
for task in tasks:
423+
if (status := task.get("status")) is not None: # type: ignore
424+
if (task_id := status.get("taskId")) and status.get("state") in [
425+
"running",
426+
"pending",
427+
"unscheduled",
428+
]:
429+
incomplete_tasks.append(task_id)
430+
431+
queue.listTaskGroup(task_group_id, paginationHandler=pagination_handler)
432+
433+
for task_id in incomplete_tasks:
434+
yield from task_id
428435

429436

430437
@functools.lru_cache(maxsize=None)

test/test_util_taskcluster.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,49 @@ def test_list_task_group_incomplete_tasks(responses, root_url):
412412
assert result == ["1", "2", "3"]
413413

414414

415+
def test_list_task_group_incomplete_tasks_with_pagination(responses, root_url):
416+
tgid = "abc123"
417+
tc.get_taskcluster_client.cache_clear()
418+
419+
responses.get(
420+
f"{root_url}/api/queue/v1/task-group/{tgid}/list",
421+
json={
422+
"tasks": [
423+
{"status": {"taskId": "1", "state": "pending"}},
424+
{"status": {"taskId": "2", "state": "completed"}},
425+
{"status": {"taskId": "3", "state": "running"}},
426+
],
427+
"continuationToken": "page2",
428+
},
429+
)
430+
431+
responses.get(
432+
f"{root_url}/api/queue/v1/task-group/{tgid}/list",
433+
json={
434+
"tasks": [
435+
{"status": {"taskId": "4", "state": "unscheduled"}},
436+
{"status": {"taskId": "5", "state": "failed"}},
437+
{"status": {"taskId": "6", "state": "pending"}},
438+
],
439+
"continuationToken": "page3",
440+
},
441+
)
442+
443+
responses.get(
444+
f"{root_url}/api/queue/v1/task-group/{tgid}/list",
445+
json={
446+
"tasks": [
447+
{"status": {"taskId": "7", "state": "running"}},
448+
{"status": {"taskId": "8", "state": "completed"}},
449+
],
450+
"continuationToken": None,
451+
},
452+
)
453+
454+
result = list(tc.list_task_group_incomplete_tasks(tgid))
455+
assert result == ["1", "3", "4", "6", "7"]
456+
457+
415458
def test_get_ancestors(responses, root_url):
416459
tc.get_task_definition.cache_clear()
417460
tc._get_deps.cache_clear()

0 commit comments

Comments
 (0)