Skip to content

Commit 0985041

Browse files
Add schedules API (#1776)
Add schedules API
1 parent ee2f5d0 commit 0985041

39 files changed

+5048
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3+
*
4+
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this material except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
package io.temporal.client;
22+
23+
import com.google.protobuf.ByteString;
24+
import io.temporal.api.schedule.v1.ScheduleListEntry;
25+
import io.temporal.api.workflowservice.v1.ListSchedulesRequest;
26+
import io.temporal.api.workflowservice.v1.ListSchedulesResponse;
27+
import io.temporal.internal.client.external.GenericWorkflowClient;
28+
import java.util.List;
29+
import java.util.concurrent.CompletableFuture;
30+
import javax.annotation.Nonnull;
31+
import javax.annotation.Nullable;
32+
33+
/** Eager iterator for listing schedules. */
34+
public final class ListScheduleListDescriptionIterator
35+
extends EagerPaginator<ListSchedulesResponse, ScheduleListEntry> {
36+
private final @Nonnull String namespace;
37+
private final @Nullable Integer pageSize;
38+
private final @Nonnull GenericWorkflowClient genericClient;
39+
40+
public ListScheduleListDescriptionIterator(
41+
@Nonnull String namespace,
42+
@Nullable Integer pageSize,
43+
@Nonnull GenericWorkflowClient genericClient) {
44+
this.namespace = namespace;
45+
this.pageSize = pageSize;
46+
this.genericClient = genericClient;
47+
}
48+
49+
@Override
50+
CompletableFuture<ListSchedulesResponse> performRequest(@Nonnull ByteString nextPageToken) {
51+
ListSchedulesRequest.Builder request =
52+
ListSchedulesRequest.newBuilder().setNamespace(namespace).setNextPageToken(nextPageToken);
53+
54+
if (pageSize != null) {
55+
request.setMaximumPageSize(pageSize);
56+
}
57+
58+
return genericClient.listSchedulesAsync(request.build());
59+
}
60+
61+
@Override
62+
ByteString getNextPageToken(ListSchedulesResponse response) {
63+
return response.getNextPageToken();
64+
}
65+
66+
@Override
67+
List<ScheduleListEntry> toElements(ListSchedulesResponse response) {
68+
return response.getSchedulesList();
69+
}
70+
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3+
*
4+
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this material except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
package io.temporal.client.schedules;
22+
23+
import java.util.Objects;
24+
import javax.annotation.Nonnull;
25+
import javax.annotation.Nullable;
26+
27+
/** A schedule for periodically running an action. */
28+
public final class Schedule {
29+
public static Schedule.Builder newBuilder() {
30+
return new Schedule.Builder();
31+
}
32+
33+
public static Schedule.Builder newBuilder(Schedule options) {
34+
return new Schedule.Builder(options);
35+
}
36+
37+
public static final class Builder {
38+
private ScheduleAction action;
39+
private ScheduleSpec spec;
40+
private SchedulePolicy policy;
41+
private ScheduleState state;
42+
43+
private Builder() {}
44+
45+
private Builder(Schedule options) {
46+
if (options == null) {
47+
return;
48+
}
49+
action = options.action;
50+
policy = options.policy;
51+
state = options.state;
52+
spec = options.spec;
53+
}
54+
55+
/**
56+
* Set the action for this schedule. Required to build.
57+
*
58+
* @see ScheduleAction
59+
*/
60+
public Builder setAction(ScheduleAction action) {
61+
this.action = action;
62+
return this;
63+
}
64+
65+
/**
66+
* Set the spec for this schedule. Required to build.
67+
*
68+
* @see ScheduleSpec
69+
*/
70+
public Builder setSpec(ScheduleSpec spec) {
71+
this.spec = spec;
72+
return this;
73+
}
74+
75+
/**
76+
* Set the spec for this schedule
77+
*
78+
* @see ScheduleSpec
79+
*/
80+
public Builder setPolicy(SchedulePolicy policy) {
81+
this.policy = policy;
82+
return this;
83+
}
84+
85+
/**
86+
* Set the state for this schedule
87+
*
88+
* @see ScheduleState
89+
*/
90+
public Builder setState(ScheduleState state) {
91+
this.state = state;
92+
return this;
93+
}
94+
95+
public Schedule build() {
96+
return new Schedule(
97+
Objects.requireNonNull(action), Objects.requireNonNull(spec), policy, state);
98+
}
99+
}
100+
101+
private final ScheduleAction action;
102+
private final SchedulePolicy policy;
103+
private final ScheduleState state;
104+
private final ScheduleSpec spec;
105+
106+
private Schedule(
107+
ScheduleAction action, ScheduleSpec spec, SchedulePolicy policy, ScheduleState state) {
108+
this.action = action;
109+
this.spec = spec;
110+
this.policy = policy;
111+
this.state = state;
112+
}
113+
114+
/**
115+
* Gets the action for the schedule.
116+
*
117+
* @return action of the schedule
118+
*/
119+
@Nonnull
120+
public ScheduleAction getAction() {
121+
return action;
122+
}
123+
124+
/**
125+
* Gets the spec for the schedule.
126+
*
127+
* @return spec of the schedule
128+
*/
129+
@Nonnull
130+
public ScheduleSpec getSpec() {
131+
return spec;
132+
}
133+
134+
/**
135+
* Gets the policy for the schedule.
136+
*
137+
* @return policy of the schedule
138+
*/
139+
@Nullable
140+
public SchedulePolicy getPolicy() {
141+
return policy;
142+
}
143+
144+
/**
145+
* Gets the state of the schedule.
146+
*
147+
* @return state of the schedule
148+
*/
149+
@Nullable
150+
public ScheduleState getState() {
151+
return state;
152+
}
153+
154+
@Override
155+
public boolean equals(Object o) {
156+
if (this == o) return true;
157+
if (o == null || getClass() != o.getClass()) return false;
158+
Schedule schedule = (Schedule) o;
159+
return Objects.equals(action, schedule.action)
160+
&& Objects.equals(policy, schedule.policy)
161+
&& Objects.equals(state, schedule.state)
162+
&& Objects.equals(spec, schedule.spec);
163+
}
164+
165+
@Override
166+
public int hashCode() {
167+
return Objects.hash(action, policy, state, spec);
168+
}
169+
170+
@Override
171+
public String toString() {
172+
return "Schedule{"
173+
+ "action="
174+
+ action
175+
+ ", policy="
176+
+ policy
177+
+ ", state="
178+
+ state
179+
+ ", spec="
180+
+ spec
181+
+ '}';
182+
}
183+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3+
*
4+
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this material except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
package io.temporal.client.schedules;
22+
23+
/**
24+
* Base class for an action a schedule can take. See ScheduleActionStartWorkflow for the most
25+
* commonly used implementation.
26+
*
27+
* @see ScheduleActionStartWorkflow
28+
*/
29+
public abstract class ScheduleAction {
30+
ScheduleAction() {}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3+
*
4+
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this material except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
package io.temporal.client.schedules;
22+
23+
/**
24+
* Base class for an action execution.
25+
*
26+
* @see ScheduleActionExecutionStartWorkflow
27+
*/
28+
public abstract class ScheduleActionExecution {
29+
ScheduleActionExecution() {}
30+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3+
*
4+
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this material except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
package io.temporal.client.schedules;
22+
23+
import java.util.Objects;
24+
25+
/** Action execution representing a scheduled workflow start. */
26+
public final class ScheduleActionExecutionStartWorkflow extends ScheduleActionExecution {
27+
private final String workflowId;
28+
private final String firstExecutionRunId;
29+
30+
public ScheduleActionExecutionStartWorkflow(String workflowId, String firstExecutionRunId) {
31+
this.workflowId = workflowId;
32+
this.firstExecutionRunId = firstExecutionRunId;
33+
}
34+
35+
/**
36+
* Get the workflow ID of the scheduled workflow.
37+
*
38+
* @return workflow ID
39+
*/
40+
public String getWorkflowId() {
41+
return workflowId;
42+
}
43+
44+
/**
45+
* Get the workflow run ID of the scheduled workflow.
46+
*
47+
* @return run ID
48+
*/
49+
public String getFirstExecutionRunId() {
50+
return firstExecutionRunId;
51+
}
52+
53+
@Override
54+
public boolean equals(Object o) {
55+
if (this == o) return true;
56+
if (o == null || getClass() != o.getClass()) return false;
57+
ScheduleActionExecutionStartWorkflow that = (ScheduleActionExecutionStartWorkflow) o;
58+
return Objects.equals(workflowId, that.workflowId)
59+
&& Objects.equals(firstExecutionRunId, that.firstExecutionRunId);
60+
}
61+
62+
@Override
63+
public int hashCode() {
64+
return Objects.hash(workflowId, firstExecutionRunId);
65+
}
66+
67+
@Override
68+
public String toString() {
69+
return "ScheduleActionExecutionStartWorkflow{"
70+
+ "workflowId='"
71+
+ workflowId
72+
+ '\''
73+
+ ", firstExecutionRunId='"
74+
+ firstExecutionRunId
75+
+ '\''
76+
+ '}';
77+
}
78+
}

0 commit comments

Comments
 (0)