Skip to content

Commit

Permalink
Add support for query in listSchedules (#2163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinn-With-Two-Ns authored Jul 31, 2024
1 parent 1acafa3 commit 27a1fc2
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
public final class ListScheduleListDescriptionIterator
extends EagerPaginator<ListSchedulesResponse, ScheduleListEntry> {
private final @Nonnull String namespace;
private final @Nullable String query;
private final @Nullable Integer pageSize;
private final @Nonnull GenericWorkflowClient genericClient;

Expand All @@ -42,6 +43,18 @@ public ListScheduleListDescriptionIterator(
@Nullable Integer pageSize,
@Nonnull GenericWorkflowClient genericClient) {
this.namespace = namespace;
this.query = null;
this.pageSize = pageSize;
this.genericClient = genericClient;
}

public ListScheduleListDescriptionIterator(
@Nonnull String namespace,
@Nullable String query,
@Nullable Integer pageSize,
@Nonnull GenericWorkflowClient genericClient) {
this.namespace = namespace;
this.query = query;
this.pageSize = pageSize;
this.genericClient = genericClient;
}
Expand All @@ -51,10 +64,12 @@ CompletableFuture<ListSchedulesResponse> performRequest(@Nonnull ByteString next
ListSchedulesRequest.Builder request =
ListSchedulesRequest.newBuilder().setNamespace(namespace).setNextPageToken(nextPageToken);

if (query != null) {
request.setQuery(query);
}
if (pageSize != null) {
request.setMaximumPageSize(pageSize);
}

return genericClient.listSchedulesAsync(request.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,14 @@ static ScheduleClient newInstance(WorkflowServiceStubs service, ScheduleClientOp
* @return sequential stream that performs remote pagination under the hood
*/
Stream<ScheduleListDescription> listSchedules(@Nullable Integer pageSize);

/**
* List schedules.
*
* @param query Temporal Visibility Query, for syntax see <a
* href="https://docs.temporal.io/visibility#list-filter">Visibility docs</a>
* @param pageSize how many results to fetch from the Server at a time. Default is 100.
* @return sequential stream that performs remote pagination under the hood
*/
Stream<ScheduleListDescription> listSchedules(@Nullable String query, @Nullable Integer pageSize);
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,21 @@ public ScheduleHandle getHandle(String scheduleID) {

@Override
public Stream<ScheduleListDescription> listSchedules() {
return this.listSchedules(null);
return this.listSchedules(null, null);
}

@Override
public Stream<ScheduleListDescription> listSchedules(@Nullable Integer pageSize) {
return this.listSchedules(null, pageSize);
}

@Override
public Stream<ScheduleListDescription> listSchedules(
@Nullable String query, @Nullable Integer pageSize) {
return scheduleClientCallsInvoker
.listSchedules(
new ScheduleClientCallsInterceptor.ListSchedulesInput(
pageSize == null ? 100 : pageSize))
query, pageSize == null ? 100 : pageSize))
.getStream();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,21 @@ public ScheduleOptions getOptions() {
}

class ListSchedulesInput {
private final String query;
private final int pageSize;

public ListSchedulesInput(int pageSize) {
public ListSchedulesInput(String query, int pageSize) {
this.query = query;
this.pageSize = pageSize;
}

public int getPageSize() {
return pageSize;
}

public String getQuery() {
return query;
}
}

class ListScheduleOutput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void createSchedule(CreateScheduleInput input) {
public ListScheduleOutput listSchedules(ListSchedulesInput input) {
ListScheduleListDescriptionIterator iterator =
new ListScheduleListDescriptionIterator(
clientOptions.getNamespace(), input.getPageSize(), genericClient);
clientOptions.getNamespace(), input.getQuery(), input.getPageSize(), genericClient);
iterator.init();
Iterator<ScheduleListDescription> wrappedIterator =
Iterators.transform(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import io.temporal.api.enums.v1.ScheduleOverlapPolicy;
import io.temporal.client.WorkflowOptions;
import io.temporal.common.RetryOptions;
import io.temporal.common.SearchAttributeKey;
import io.temporal.common.SearchAttributes;
import io.temporal.common.converter.EncodedValues;
import io.temporal.common.interceptors.ScheduleClientInterceptor;
import io.temporal.testing.internal.SDKTestWorkflowRule;
Expand All @@ -43,6 +45,9 @@
import org.junit.Test;

public class ScheduleTest {
static final SearchAttributeKey<String> CUSTOM_KEYWORD_SA =
SearchAttributeKey.forKeyword("CustomKeywordField");

@Rule
public SDKTestWorkflowRule testWorkflowRule =
SDKTestWorkflowRule.newBuilder()
Expand Down Expand Up @@ -484,10 +489,12 @@ public void updateSchedules() {
public void listSchedules() {
ScheduleClient client = createScheduleClient();
// Create the schedule
ScheduleOptions options =
ScheduleOptions.Builder optionsBuilder =
ScheduleOptions.newBuilder()
.setMemo(Collections.singletonMap("memokey2", "memoval2"))
.build();
.setTypedSearchAttributes(
SearchAttributes.newBuilder().set(CUSTOM_KEYWORD_SA, "keyword").build());
ScheduleOptions options = optionsBuilder.build();
Schedule schedule =
createTestSchedule()
.setState(ScheduleState.newBuilder().setPaused(true).setNote("schedule list").build())
Expand Down Expand Up @@ -532,17 +539,28 @@ public void listSchedules() {
(ScheduleListActionStartWorkflow) listDescription.getSchedule().getAction();
Assert.assertEquals("TestWorkflow1", action.getWorkflow());
// Create two additional schedules
client.createSchedule(scheduleIdPrefix + UUID.randomUUID(), schedule, options);
client.createSchedule(scheduleIdPrefix + UUID.randomUUID(), schedule, options);
optionsBuilder = optionsBuilder.setTypedSearchAttributes(null);
client.createSchedule(scheduleIdPrefix + UUID.randomUUID(), schedule, optionsBuilder.build());
client.createSchedule(scheduleIdPrefix + UUID.randomUUID(), schedule, optionsBuilder.build());
// Add delay for schedules to appear
testWorkflowRule.sleep(Duration.ofSeconds(2));
// List all schedules and filter
scheduleStream = client.listSchedules(10);
long listedSchedulesCount =
scheduleStream.filter(s -> s.getScheduleId().startsWith(scheduleIdPrefix)).count();
Assert.assertEquals(3, listedSchedulesCount);
// List all schedules with a null filter
scheduleStream = client.listSchedules(null, 10);
listedSchedulesCount =
scheduleStream.filter(s -> s.getScheduleId().startsWith(scheduleIdPrefix)).count();
Assert.assertEquals(3, listedSchedulesCount);
// List schedules with a query
scheduleStream = client.listSchedules("CustomKeywordField = 'keyword'", null);
listedSchedulesCount =
scheduleStream.filter(s -> s.getScheduleId().startsWith(scheduleIdPrefix)).count();
Assert.assertEquals(1, listedSchedulesCount);
// Cleanup all schedules
scheduleStream = client.listSchedules(null);
scheduleStream = client.listSchedules(null, null);
scheduleStream
.filter(s -> s.getScheduleId().startsWith(scheduleIdPrefix))
.forEach(
Expand Down

0 comments on commit 27a1fc2

Please sign in to comment.