Skip to content

Commit

Permalink
Support toString on workflow proxy types (#2315)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinn-With-Two-Ns authored Nov 22, 2024
1 parent 16b0bb9 commit 1d86a57
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ protected Function<Object[], Object> getActivityFunc(
(a) -> stub.execute(activityName, method.getReturnType(), method.getGenericReturnType(), a);
return function;
}

@Override
protected String proxyToString() {
return "ActivityProxy{" + "options=" + options + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public static <T> T newProxy(Class<T> activityInterface, InvocationHandler invoc

@Override
public Object invoke(Object proxy, Method method, Object[] args) {
// Proxy the toString method so the stub can be inspected when debugging.
try {
if (method.equals(Object.class.getMethod("toString"))) {
return proxyToString();
}
} catch (NoSuchMethodException e) {
throw new Error("unexpected", e);
}
POJOActivityMethodMetadata methodMetadata = activityMetadata.getMethodMetadata(method);
MethodRetry methodRetry = methodMetadata.getMethod().getAnnotation(MethodRetry.class);
String activityType = methodMetadata.getActivityTypeName();
Expand All @@ -62,4 +70,6 @@ public Object invoke(Object proxy, Method method, Object[] args) {

protected abstract Function<Object[], Object> getActivityFunc(
Method method, MethodRetry methodRetry, String activityName);

protected abstract String proxyToString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ class ChildWorkflowInvocationHandler implements InvocationHandler {

@Override
public Object invoke(Object proxy, Method method, Object[] args) {
// Proxy the toString method so the stub can be inspected when debugging.
try {
if (method.equals(Object.class.getMethod("toString"))) {
return proxyToString();
}
} catch (NoSuchMethodException e) {
throw new Error("unexpected", e);
}
// Implement StubMarker
if (method.getName().equals(StubMarker.GET_UNTYPED_STUB_METHOD)) {
return stub;
Expand Down Expand Up @@ -99,4 +107,14 @@ public Object invoke(Object proxy, Method method, Object[] args) {
}
throw new IllegalArgumentException("unreachable");
}

private String proxyToString() {
return "ChildWorkflowProxy{"
+ "workflowType='"
+ stub.getWorkflowType()
+ '\''
+ ", options="
+ stub.getOptions()
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public ExternalWorkflowInvocationHandler(

@Override
public Object invoke(Object proxy, Method method, Object[] args) {
// Proxy the toString method so the stub can be inspected when debugging.
try {
if (method.equals(Object.class.getMethod("toString"))) {
return proxyToString();
}
} catch (NoSuchMethodException e) {
throw new Error("unexpected", e);
}
// Implement StubMarker
if (method.getName().equals(StubMarker.GET_UNTYPED_STUB_METHOD)) {
return stub;
Expand All @@ -73,4 +81,14 @@ public Object invoke(Object proxy, Method method, Object[] args) {
}
return null;
}

private String proxyToString() {
return "ExternalWorkflowProxy{"
+ "workflowType='"
+ workflowMetadata.getWorkflowType().orElse("")
+ '\''
+ ", execution="
+ stub.getExecution()
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,9 @@ public Function<Object[], Object> getActivityFunc(
(a) -> stub.execute(activityName, method.getReturnType(), method.getGenericReturnType(), a);
return function;
}

@Override
protected String proxyToString() {
return "LocalActivityProxy{" + "options='" + options + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public class NexusServiceInvocationHandler implements InvocationHandler {

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Proxy the toString method so the stub can be inspected when debugging.
try {
if (method.equals(Object.class.getMethod("toString"))) {
return proxyToString();
}
} catch (NoSuchMethodException e) {
throw new Error("unexpected", e);
}

if (method.getName().equals(StubMarker.GET_UNTYPED_STUB_METHOD)) {
return stub;
}
Expand All @@ -69,4 +78,14 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
this.stub.execute(opName, method.getReturnType(), method.getGenericReturnType(), arg),
method.getReturnType());
}

private String proxyToString() {
return "NexusServiceProxy{"
+ "serviceName="
+ serviceDef.getName()
+ '\''
+ ", options="
+ stub.getOptions()
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
import java.util.Collections;

public class NexusServiceStubImpl implements NexusServiceStub {
final String name;
final NexusServiceOptions options;
final WorkflowOutboundCallsInterceptor outboundCallsInterceptor;
final Functions.Proc1<String> assertReadOnly;
private final String name;
private final NexusServiceOptions options;
private final WorkflowOutboundCallsInterceptor outboundCallsInterceptor;
private final Functions.Proc1<String> assertReadOnly;

public NexusServiceStubImpl(
String name,
Expand Down Expand Up @@ -118,6 +118,11 @@ public <R> NexusOperationHandle<R> start(
arg,
mergedOptions,
Collections.emptyMap()));
return new NexusOperationHandleImpl(result.getOperationExecution(), result.getResult());
return new NexusOperationHandleImpl<>(result.getOperationExecution(), result.getResult());
}

@Override
public NexusServiceOptions getOptions() {
return options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,9 @@ <R> Promise<R> executeAsync(
*/
<R> NexusOperationHandle<R> start(
String operationName, Class<R> resultClass, Type resultType, Object arg);

/**
* @return Options used to create this stub.
*/
NexusServiceOptions getOptions();
}

0 comments on commit 1d86a57

Please sign in to comment.