9
9
import com .microsoft .azure .functions .HttpStatus ;
10
10
import com .microsoft .durabletask .DurableTaskClient ;
11
11
import com .microsoft .durabletask .DurableTaskGrpcClientBuilder ;
12
+ import com .microsoft .durabletask .OrchestrationMetadata ;
13
+ import com .microsoft .durabletask .OrchestrationRuntimeStatus ;
12
14
13
15
import java .io .UnsupportedEncodingException ;
14
16
import java .net .MalformedURLException ;
15
17
import java .net .URL ;
16
18
import java .net .URLEncoder ;
17
19
import java .nio .charset .StandardCharsets ;
20
+ import java .time .Duration ;
21
+ import java .util .concurrent .TimeoutException ;
18
22
19
23
/**
20
24
* The binding value type for the {@literal @}DurableClientInput parameter.
@@ -24,6 +28,7 @@ public class DurableClientContext {
24
28
private String rpcBaseUrl ;
25
29
private String taskHubName ;
26
30
private String requiredQueryStringParameters ;
31
+ private DurableTaskClient client ;
27
32
28
33
/**
29
34
* Gets the name of the client binding's task hub.
@@ -51,9 +56,55 @@ public DurableTaskClient getClient() {
51
56
throw new IllegalStateException ("The client context RPC base URL was invalid!" , ex );
52
57
}
53
58
54
- return new DurableTaskGrpcClientBuilder ().port (rpcURL .getPort ()).build ();
59
+ this .client = new DurableTaskGrpcClientBuilder ().port (rpcURL .getPort ()).build ();
60
+ return this .client ;
55
61
}
56
62
63
+ /**
64
+ * Creates an HTTP response which either contains a payload of management URLs for a non-completed instance
65
+ * or contains the payload containing the output of the completed orchestration.
66
+ * <p>
67
+ * If the orchestration instance completes within the specified timeout, then the HTTP response payload will
68
+ * contains the output of the orchestration instance formatted as JSON. However, if the orchestration does not
69
+ * complete within the specified timeout, then the HTTP response will be identical to that of the
70
+ * {@link #createCheckStatusResponse(HttpRequestMessage, String)} API.
71
+ * </p>
72
+ * @param request the HTTP request that triggered the current function
73
+ * @param instanceId the unique ID of the instance to check
74
+ * @param timeout total allowed timeout for output from the durable function
75
+ * @return an HTTP response which may include a 202 and location header or a 200 with the durable function output in the response body
76
+ */
77
+ public HttpResponseMessage waitForCompletionOrCreateCheckStatusResponse (
78
+ HttpRequestMessage <?> request ,
79
+ String instanceId ,
80
+ Duration timeout ) {
81
+ if (this .client == null ) {
82
+ this .client = getClient ();
83
+ }
84
+ OrchestrationMetadata orchestration ;
85
+ try {
86
+ orchestration = this .client .waitForInstanceCompletion (instanceId , timeout , true );
87
+ return request .createResponseBuilder (HttpStatus .ACCEPTED )
88
+ .header ("Content-Type" , "application/json" )
89
+ .body (orchestration .getSerializedOutput ())
90
+ .build ();
91
+ } catch (TimeoutException e ) {
92
+ return createCheckStatusResponse (request , instanceId );
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Creates an HTTP response that is useful for checking the status of the specified instance.
98
+ * <p>
99
+ * The payload of the returned
100
+ * @see <a href="https://learn.microsoft.com/java/api/com.microsoft.azure.functions.httpresponsemessage">HttpResponseMessage</a>
101
+ * contains HTTP API URLs that can be used to query the status of the orchestration, raise events to the orchestration, or
102
+ * terminate the orchestration.
103
+ * </p>
104
+ * @param request the HTTP request that triggered the current orchestration instance
105
+ * @param instanceId the ID of the orchestration instance to check
106
+ * @return an HTTP 202 response with a Location header and a payload containing instance control URLs
107
+ */
57
108
public HttpResponseMessage createCheckStatusResponse (HttpRequestMessage <?> request , String instanceId ) {
58
109
// TODO: To better support scenarios involving proxies or application gateways, this
59
110
// code should take the X-Forwarded-Host, X-Forwarded-Proto, and Forwarded HTTP
0 commit comments