Skip to content

Commit b05c93d

Browse files
A bunch of fixes for the Workflow API (#222)
1 parent 8a74cae commit b05c93d

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

sdk-api-gen/src/main/resources/templates/ServiceAdapter.hbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
public class {{className}}ServiceAdapter implements dev.restate.sdk.common.ServiceAdapter<{{fqcn}}> {
44

5+
public static final String SERVICE_NAME = "{{fqcn}}";
6+
57
@java.lang.Override
68
public dev.restate.sdk.workflow.impl.WorkflowServicesBundle adapt({{fqcn}} service) {
79
return dev.restate.sdk.workflow.impl.WorkflowServicesBundle.named(
8-
"{{fqcn}}",
10+
SERVICE_NAME,
911
{{#methods}}{{#if isWorkflow}}
1012
dev.restate.sdk.workflow.impl.WorkflowServicesBundle.MethodSignature.of("{{name}}", {{{inputSerdeDecl}}}, {{{outputSerdeDecl}}}),
1113
(ctx, req) -> {

sdk-core/src/main/java/dev/restate/sdk/core/RestateEndpoint.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import dev.restate.generated.service.discovery.Discovery;
1212
import dev.restate.sdk.common.ServiceAdapter;
13-
import dev.restate.sdk.common.ServicesBundle;
1413
import io.grpc.*;
1514
import io.opentelemetry.api.OpenTelemetry;
1615
import io.opentelemetry.api.trace.Span;
@@ -221,11 +220,12 @@ public void setInvocationStatus(String invocationStatus) {
221220
}
222221

223222
/** Resolve the code generated {@link ServiceAdapter} */
224-
public static ServicesBundle adapt(Object entity) {
223+
public static ServiceAdapter<Object> discoverAdapter(Object entity) {
225224
Class<?> userClazz = entity.getClass();
226225

227226
// Find Service code-generated class
228227
// TODO This could be done with an SPI
228+
// TODO This should support interfaces
229229
Class<?> serviceAdapterClazz;
230230
try {
231231
serviceAdapterClazz = Class.forName(userClazz.getCanonicalName() + "ServiceAdapter");
@@ -251,6 +251,6 @@ public static ServicesBundle adapt(Object entity) {
251251
e);
252252
}
253253

254-
return serviceAdapter.adapt(entity);
254+
return serviceAdapter;
255255
}
256256
}

sdk-http-vertx/src/main/java/dev/restate/sdk/http/vertx/RestateHttpEndpointBuilder.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import dev.restate.generated.service.discovery.Discovery;
1212
import dev.restate.sdk.common.BlockingService;
1313
import dev.restate.sdk.common.NonBlockingService;
14+
import dev.restate.sdk.common.ServiceAdapter;
1415
import dev.restate.sdk.core.RestateEndpoint;
1516
import io.grpc.ServerInterceptor;
1617
import io.grpc.ServerInterceptors;
@@ -117,20 +118,42 @@ public RestateHttpEndpointBuilder withService(
117118
return this;
118119
}
119120

120-
/** Add a Restate entity to the endpoint. */
121+
/**
122+
* Add a Restate service to the endpoint. This will automatically discover the adapter based on
123+
* the class name. You can provide the adapter manually using {@link #with(Object,
124+
* ServiceAdapter)}
125+
*/
121126
public RestateHttpEndpointBuilder with(Object service) {
122127
return this.with(service, defaultExecutor);
123128
}
124129

125130
/**
126-
* Add a Restate entity to the endpoint, specifying the {@code executor} where to run the entity
127-
* code.
131+
* Add a Restate service to the endpoint, specifying the {@code executor} where to run the service
132+
* code. This will automatically discover the adapter based on the class name. You can provide the
133+
* adapter manually using {@link #with(Object, ServiceAdapter, Executor)}
128134
*
129135
* <p>You can run on virtual threads by using the executor {@code
130136
* Executors.newVirtualThreadPerTaskExecutor()}.
131137
*/
132138
public RestateHttpEndpointBuilder with(Object service, Executor executor) {
133-
List<BlockingService> services = RestateEndpoint.adapt(service).services();
139+
return this.with(service, RestateEndpoint.discoverAdapter(service), executor);
140+
}
141+
142+
/** Add a Restate service to the endpoint, specifying an adapter. */
143+
public <T> RestateHttpEndpointBuilder with(T service, ServiceAdapter<T> adapter) {
144+
return this.with(service, adapter, defaultExecutor);
145+
}
146+
147+
/**
148+
* Add a Restate service to the endpoint, specifying the {@code executor} where to run the service
149+
* code.
150+
*
151+
* <p>You can run on virtual threads by using the executor {@code
152+
* Executors.newVirtualThreadPerTaskExecutor()}.
153+
*/
154+
public <T> RestateHttpEndpointBuilder with(
155+
T service, ServiceAdapter<T> adapter, Executor executor) {
156+
List<BlockingService> services = adapter.adapt(service).services();
134157
for (BlockingService svc : services) {
135158
this.withService(svc, executor);
136159
}

sdk-lambda/src/main/java/dev/restate/sdk/lambda/RestateLambdaEndpointBuilder.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import dev.restate.generated.service.discovery.Discovery;
1212
import dev.restate.sdk.common.BlockingService;
1313
import dev.restate.sdk.common.Service;
14+
import dev.restate.sdk.common.ServiceAdapter;
1415
import dev.restate.sdk.core.RestateEndpoint;
1516
import io.grpc.ServerInterceptor;
1617
import io.grpc.ServerInterceptors;
@@ -44,8 +45,12 @@ public RestateLambdaEndpointBuilder withService(
4445
* code.
4546
*/
4647
public RestateLambdaEndpointBuilder with(Object service) {
47-
List<BlockingService> services = RestateEndpoint.adapt(service).services();
48-
for (BlockingService svc : services) {
48+
return this.with(service, RestateEndpoint.discoverAdapter(service));
49+
}
50+
51+
public <T> RestateLambdaEndpointBuilder with(T service, ServiceAdapter<T> adapter) {
52+
List<BlockingService> services = adapter.adapt(service).services();
53+
for (Service svc : services) {
4954
this.restateGrpcServerBuilder.withService(svc);
5055
}
5156

sdk-workflow-api/src/main/java/dev/restate/sdk/workflow/impl/WorkflowManagerImpl.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ public StartResponse tryStart(KeyedContext context, StartRequest request)
135135
return maybeResponse.get();
136136
}
137137

138-
StartResponse response =
139-
StartResponse.newBuilder().setState(WorkflowExecutionState.STARTED).build();
140-
context.set(WORKFLOW_EXECUTION_STATE_KEY, response);
141-
return response;
138+
context.set(
139+
WORKFLOW_EXECUTION_STATE_KEY,
140+
StartResponse.newBuilder().setState(WorkflowExecutionState.ALREADY_STARTED).build());
141+
return StartResponse.newBuilder().setState(WorkflowExecutionState.STARTED).build();
142142
}
143143

144144
@Override
@@ -159,6 +159,9 @@ public GetOutputResponse getOutput(KeyedContext context, OutputRequest request)
159159
@Override
160160
public void setOutput(KeyedContext context, SetOutputRequest request) throws TerminalException {
161161
context.set(OUTPUT_KEY, request.getOutput());
162+
context.set(
163+
WORKFLOW_EXECUTION_STATE_KEY,
164+
StartResponse.newBuilder().setState(WorkflowExecutionState.ALREADY_COMPLETED).build());
162165
}
163166

164167
@Override

0 commit comments

Comments
 (0)