|
| 1 | +package io.temporal.internal.nexus; |
| 2 | + |
| 3 | +import static io.temporal.internal.common.LinkConverter.workflowEventToNexusLink; |
| 4 | +import static io.temporal.internal.common.NexusUtil.nexusProtoLinkToLink; |
| 5 | + |
| 6 | +import io.nexusrpc.handler.HandlerException; |
| 7 | +import io.nexusrpc.handler.OperationContext; |
| 8 | +import io.nexusrpc.handler.OperationStartDetails; |
| 9 | +import io.temporal.api.common.v1.Link; |
| 10 | +import io.temporal.api.common.v1.WorkflowExecution; |
| 11 | +import io.temporal.api.enums.v1.EventType; |
| 12 | +import io.temporal.internal.client.NexusStartWorkflowRequest; |
| 13 | +import io.temporal.internal.client.NexusStartWorkflowResponse; |
| 14 | +import java.net.URISyntaxException; |
| 15 | +import java.util.function.Function; |
| 16 | + |
| 17 | +/** |
| 18 | + * Shared helper for starting a workflow from a Nexus operation and attaching workflow links to the |
| 19 | + * operation context. Used by both {@code WorkflowRunOperationImpl} and {@code |
| 20 | + * TemporalNexusClientImpl}. |
| 21 | + */ |
| 22 | +public class NexusStartWorkflowHelper { |
| 23 | + |
| 24 | + /** |
| 25 | + * Starts a workflow via the provided invoker function, attaches workflow links to the operation |
| 26 | + * context, and returns the response. |
| 27 | + * |
| 28 | + * @param ctx the operation context (links will be attached as a side-effect) |
| 29 | + * @param details the operation start details containing requestId, callback, links |
| 30 | + * @param invoker function that starts the workflow given a {@link NexusStartWorkflowRequest} |
| 31 | + * @return the {@link NexusStartWorkflowResponse} containing the operation token and workflow |
| 32 | + * execution |
| 33 | + */ |
| 34 | + public static NexusStartWorkflowResponse startWorkflowAndAttachLinks( |
| 35 | + OperationContext ctx, |
| 36 | + OperationStartDetails details, |
| 37 | + Function<NexusStartWorkflowRequest, NexusStartWorkflowResponse> invoker) { |
| 38 | + InternalNexusOperationContext nexusCtx = CurrentNexusOperationContext.get(); |
| 39 | + |
| 40 | + NexusStartWorkflowRequest nexusRequest = |
| 41 | + new NexusStartWorkflowRequest( |
| 42 | + details.getRequestId(), |
| 43 | + details.getCallbackUrl(), |
| 44 | + details.getCallbackHeaders(), |
| 45 | + nexusCtx.getTaskQueue(), |
| 46 | + details.getLinks()); |
| 47 | + |
| 48 | + NexusStartWorkflowResponse response = invoker.apply(nexusRequest); |
| 49 | + WorkflowExecution workflowExec = response.getWorkflowExecution(); |
| 50 | + |
| 51 | + // If the start workflow response returned a link use it, otherwise |
| 52 | + // create the link information about the new workflow and return to the caller. |
| 53 | + Link.WorkflowEvent workflowEventLink = |
| 54 | + nexusCtx.getStartWorkflowResponseLink().hasWorkflowEvent() |
| 55 | + ? nexusCtx.getStartWorkflowResponseLink().getWorkflowEvent() |
| 56 | + : null; |
| 57 | + if (workflowEventLink == null) { |
| 58 | + workflowEventLink = |
| 59 | + Link.WorkflowEvent.newBuilder() |
| 60 | + .setNamespace(nexusCtx.getNamespace()) |
| 61 | + .setWorkflowId(workflowExec.getWorkflowId()) |
| 62 | + .setRunId(workflowExec.getRunId()) |
| 63 | + .setEventRef( |
| 64 | + Link.WorkflowEvent.EventReference.newBuilder() |
| 65 | + .setEventType(EventType.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED)) |
| 66 | + .build(); |
| 67 | + } |
| 68 | + io.temporal.api.nexus.v1.Link nexusLink = workflowEventToNexusLink(workflowEventLink); |
| 69 | + if (nexusLink != null) { |
| 70 | + try { |
| 71 | + ctx.addLinks(nexusProtoLinkToLink(nexusLink)); |
| 72 | + } catch (URISyntaxException e) { |
| 73 | + throw new HandlerException(HandlerException.ErrorType.INTERNAL, "failed to parse URI", e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return response; |
| 78 | + } |
| 79 | + |
| 80 | + private NexusStartWorkflowHelper() {} |
| 81 | +} |
0 commit comments