Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add limit to list workflows #377

Merged
merged 1 commit into from
Dec 6, 2024
Merged
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
6 changes: 6 additions & 0 deletions src/Temporalio/Client/TemporalClient.Workflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ private async IAsyncEnumerable<WorkflowExecution> ListWorkflowsInternalAsync(
// Need to combine cancellation token
var rpcOptsAndCancelSource = DefaultRetryOptions(input.Options?.Rpc).
WithAdditionalCancellationToken(cancellationToken);
var yielded = 0;
try
{
var req = new ListWorkflowExecutionsRequest()
Expand All @@ -472,6 +473,11 @@ private async IAsyncEnumerable<WorkflowExecution> ListWorkflowsInternalAsync(
req, rpcOptsAndCancelSource.Item1).ConfigureAwait(false);
foreach (var exec in resp.Executions)
{
if (input.Options != null && input.Options.Limit > 0 &&
yielded++ >= input.Options.Limit)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the biggest fan of side effects in conditionals like this since postfix increment operator precedence is not always easy for a code reader to understand, but not a blocker.

{
yield break;
}
yield return new(exec, Client.Options.DataConverter);
}
req.NextPageToken = resp.NextPageToken;
Expand Down
7 changes: 6 additions & 1 deletion src/Temporalio/Client/WorkflowListOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public class WorkflowListOptions : ICloneable
/// </summary>
public RpcOptions? Rpc { get; set; }

/// <summary>
/// Gets or sets the maximum number of workflows to return. A zero value means no limit.
/// </summary>
public int Limit { get; set; }

/// <summary>
/// Create a shallow copy of these options.
/// </summary>
Expand All @@ -28,4 +33,4 @@ public virtual object Clone()
}
}
}
#endif
#endif
9 changes: 9 additions & 0 deletions tests/Temporalio.Tests/Client/TemporalClientWorkflowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,15 @@ await Client.ExecuteWorkflowAsync(
actualResults.Add(result);
}
Assert.Equal(expectedResults, actualResults);

// Verify limit option works
var limitedResults = 0;
await foreach (var wf in Client.ListWorkflowsAsync(
$"WorkflowId = '{workflowId}'", new() { Limit = 3 }))
{
limitedResults++;
}
Assert.Equal(3, limitedResults);
}

[Workflow]
Expand Down
Loading