Skip to content

Commit ecf594c

Browse files
committed
Add limit to list workflows
1 parent a90f6d4 commit ecf594c

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

temporalio/client.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ def list_workflows(
796796
next_page_token: Optional[bytes] = None,
797797
rpc_metadata: Mapping[str, str] = {},
798798
rpc_timeout: Optional[timedelta] = None,
799+
limit: Optional[int] = None,
799800
) -> WorkflowExecutionAsyncIterator:
800801
"""List workflows.
801802
@@ -813,6 +814,8 @@ def list_workflows(
813814
rpc_metadata: Headers used on each RPC call. Keys here override
814815
client-level RPC metadata keys.
815816
rpc_timeout: Optional RPC deadline to set for each RPC call.
817+
limit: Maximum number of workflows to return. If unset, all
818+
workflows are returned.
816819
817820
Returns:
818821
An async iterator that can be used with ``async for``.
@@ -824,6 +827,7 @@ def list_workflows(
824827
next_page_token=next_page_token,
825828
rpc_metadata=rpc_metadata,
826829
rpc_timeout=rpc_timeout,
830+
limit=limit,
827831
)
828832
)
829833

@@ -2483,6 +2487,8 @@ def __init__(
24832487
self._next_page_token = input.next_page_token
24842488
self._current_page: Optional[Sequence[WorkflowExecution]] = None
24852489
self._current_page_index = 0
2490+
self._limit = input.limit
2491+
self._yielded = 0
24862492

24872493
@property
24882494
def current_page_index(self) -> int:
@@ -2508,10 +2514,14 @@ async def fetch_next_page(self, *, page_size: Optional[int] = None) -> None:
25082514
page_size: Override the page size this iterator was originally
25092515
created with.
25102516
"""
2517+
page_size = page_size or self._input.page_size
2518+
if self._limit is not None and self._limit - self._yielded < page_size:
2519+
page_size = self._limit - self._yielded
2520+
25112521
resp = await self._client.workflow_service.list_workflow_executions(
25122522
temporalio.api.workflowservice.v1.ListWorkflowExecutionsRequest(
25132523
namespace=self._client.namespace,
2514-
page_size=page_size or self._input.page_size,
2524+
page_size=page_size,
25152525
next_page_token=self._next_page_token or b"",
25162526
query=self._input.query or "",
25172527
),
@@ -2534,6 +2544,8 @@ async def __anext__(self) -> WorkflowExecution:
25342544
"""Get the next execution on this iterator, fetching next page if
25352545
necessary.
25362546
"""
2547+
if self._limit is not None and self._yielded >= self._limit:
2548+
raise StopAsyncIteration
25372549
while True:
25382550
# No page? fetch and continue
25392551
if self._current_page is None:
@@ -2551,6 +2563,7 @@ async def __anext__(self) -> WorkflowExecution:
25512563
# Get current, increment page index, and return
25522564
ret = self._current_page[self._current_page_index]
25532565
self._current_page_index += 1
2566+
self._yielded += 1
25542567
return ret
25552568

25562569
async def map_histories(
@@ -4573,6 +4586,7 @@ class ListWorkflowsInput:
45734586
next_page_token: Optional[bytes]
45744587
rpc_metadata: Mapping[str, str]
45754588
rpc_timeout: Optional[timedelta]
4589+
limit: Optional[int]
45764590

45774591

45784592
@dataclass

tests/test_client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,14 @@ async def test_list_workflows_and_fetch_history(
618618
)
619619
assert actual_id_and_input == expected_id_and_input
620620

621+
# Verify listing can limit results
622+
limited = [w async for w in client.list_workflows(f"WorkflowId = '{workflow_id}'", limit=3)]
623+
assert len(limited) == 3
624+
# With a weird page size
625+
limited = [w async for w in client.list_workflows(f"WorkflowId = '{workflow_id}'", page_size=2, limit=3)]
626+
assert len(limited) == 3
627+
628+
621629

622630
@workflow.defn
623631
class CountableWorkflow:

0 commit comments

Comments
 (0)