Skip to content

Commit c8e749d

Browse files
add middleware implementation (#87)
* add middleware implementation * update code with new API * rename middleware chain * minor updates * refactor code * update method/variable names make them more clear * update ctx to taskOrchestrationContext make it more clear * Update build-validation.yml * Update build.gradle * Update build.gradle * update to latest worker 2.7.0 * update OrchestratorBlockedEvent to OrchestratorBlockedException Co-authored-by: Shreyas Gopalakrishna <[email protected]>
1 parent 6acaa32 commit c8e749d

File tree

8 files changed

+65
-17
lines changed

8 files changed

+65
-17
lines changed

.github/workflows/build-validation.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ jobs:
8383
continue-on-error: true
8484
- name: Download azure functions java library # TODO: Remove this step once gradle plugin is updated
8585
run: |
86-
wget -P samples-azure-functions/build/azure-functions/azure-functions-sample/lib/ "https://repo.maven.apache.org/maven2/com/microsoft/azure/functions/azure-functions-java-library/2.0.1/azure-functions-java-library-2.0.1.jar" --show-progress
86+
wget -P samples-azure-functions/build/azure-functions/azure-functions-sample/lib/ "https://repo.maven.apache.org/maven2/com/microsoft/azure/functions/azure-functions-java-library/2.1.0/azure-functions-java-library-2.1.0.jar" --show-progress
8787
- name: Run azure functions test
8888
run: samples-azure-functions/e2e-test.ps1 -DockerfilePath samples-azure-functions/Dockerfile -HttpStartPath api/StartOrchestration
89-
shell: pwsh
89+
shell: pwsh

azurefunctions/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ repositories {
1717

1818
dependencies {
1919
api project(':client')
20-
implementation group: 'com.microsoft.azure.functions', name: 'azure-functions-java-library', version: '2.0.1'
20+
implementation group: 'com.microsoft.azure.functions', name: 'azure-functions-java-library', version: '2.1.0'
2121
implementation "com.google.protobuf:protobuf-java:${protocVersion}"
2222
}
2323

@@ -59,4 +59,4 @@ publishing {
5959
java {
6060
withSourcesJar()
6161
withJavadocJar()
62-
}
62+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for
4+
* license information.
5+
*/
6+
7+
package com.microsoft.durabletask.azurefunctions.internal.middleware;
8+
9+
import com.microsoft.azure.functions.internal.spi.middleware.Middleware;
10+
import com.microsoft.azure.functions.internal.spi.middleware.MiddlewareChain;
11+
import com.microsoft.azure.functions.internal.spi.middleware.MiddlewareContext;
12+
import com.microsoft.durabletask.OrchestrationRunner;
13+
import com.microsoft.durabletask.OrchestratorBlockedException;
14+
15+
/**
16+
* Durable Function Orchestration Middleware
17+
*
18+
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
19+
* at any time.
20+
*/
21+
public class OrchestrationMiddleware implements Middleware {
22+
23+
private static final String ORCHESTRATION_TRIGGER = "DurableOrchestrationTrigger";
24+
25+
@Override
26+
public void invoke(MiddlewareContext context, MiddlewareChain chain) throws Exception {
27+
String parameterName = context.getParameterName(ORCHESTRATION_TRIGGER);
28+
if (parameterName == null){
29+
chain.doNext(context);
30+
return;
31+
}
32+
String orchestratorRequestEncodedProtoBytes = (String) context.getParameterValue(parameterName);
33+
String orchestratorOutputEncodedProtoBytes = OrchestrationRunner.loadAndRun(orchestratorRequestEncodedProtoBytes, taskOrchestrationContext -> {
34+
try {
35+
context.updateParameterValue(parameterName, taskOrchestrationContext);
36+
chain.doNext(context);
37+
return context.getReturnValue();
38+
} catch (Exception e) {
39+
// The OrchestratorBlockedEvent will be wrapped into InvocationTargetException by using reflection to
40+
// invoke method. Thus get the cause to check if it's OrchestratorBlockedEvent.
41+
Throwable cause = e.getCause();
42+
if (cause instanceof OrchestratorBlockedException){
43+
throw (OrchestratorBlockedException) cause;
44+
}
45+
throw new RuntimeException("Unexpected failure in the task execution", e);
46+
}
47+
});
48+
context.updateReturnValue(orchestratorOutputEncodedProtoBytes);
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.microsoft.durabletask.azurefunctions.internal.middleware.OrchestrationMiddleware

samples-azure-functions/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM mcr.microsoft.com/azure-functions/java:4-java11
22

33
RUN rm /azure-functions-host/workers/java/azure-functions-java-worker.jar
4-
COPY samples-azure-functions/azure-functions-java-worker-2.2.3.jar /azure-functions-host/workers/java/azure-functions-java-worker.jar
4+
COPY samples-azure-functions/azure-functions-java-worker-2.7.0.jar /azure-functions-host/workers/java/azure-functions-java-worker.jar
55
COPY samples-azure-functions/build/azure-functions/azure-functions-sample/ /home/site/wwwroot/
66
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
77
AzureFunctionsJobHost__Logging__Console__IsEnabled=true

samples-azure-functions/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ dependencies {
1919
implementation project(':client')
2020
implementation project(':azurefunctions')
2121

22-
implementation 'com.microsoft.azure.functions:azure-functions-java-library:2.0.1'
22+
implementation 'com.microsoft.azure.functions:azure-functions-java-library:2.1.0'
2323
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
2424
testImplementation 'org.mockito:mockito-core:3.3.3'
2525
runtimeOnly "io.grpc:grpc-netty-shaded:1.38.0"

samples-azure-functions/src/main/java/com/functions/AzureFunctions.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import com.microsoft.azure.functions.*;
55
import java.util.*;
66

7-
import com.microsoft.durabletask.DurableTaskClient;
8-
import com.microsoft.durabletask.OrchestrationRunner;
7+
import com.microsoft.durabletask.*;
98
import com.microsoft.durabletask.azurefunctions.DurableActivityTrigger;
109
import com.microsoft.durabletask.azurefunctions.DurableClientContext;
1110
import com.microsoft.durabletask.azurefunctions.DurableClientInput;
@@ -37,15 +36,13 @@ public HttpResponseMessage startOrchestration(
3736
*/
3837
@FunctionName("Cities")
3938
public String citiesOrchestrator(
40-
@DurableOrchestrationTrigger(name = "orchestratorRequestProtoBytes") String orchestratorRequestProtoBytes) {
41-
return OrchestrationRunner.loadAndRun(orchestratorRequestProtoBytes, ctx -> {
42-
String result = "";
43-
result += ctx.callActivity("Capitalize", "Tokyo", String.class).await() + ", ";
44-
result += ctx.callActivity("Capitalize", "London", String.class).await() + ", ";
45-
result += ctx.callActivity("Capitalize", "Seattle", String.class).await() + ", ";
46-
result += ctx.callActivity("Capitalize", "Austin", String.class).await();
47-
return result;
48-
});
39+
@DurableOrchestrationTrigger(name = "taskOrchestrationContext") TaskOrchestrationContext ctx) {
40+
String result = "";
41+
result += ctx.callActivity("Capitalize", "Tokyo", String.class).await() + ", ";
42+
result += ctx.callActivity("Capitalize", "London", String.class).await() + ", ";
43+
result += ctx.callActivity("Capitalize", "Seattle", String.class).await() + ", ";
44+
result += ctx.callActivity("Capitalize", "Austin", String.class).await();
45+
return result;
4946
}
5047

5148
/**

0 commit comments

Comments
 (0)