|
2 | 2 | title: Custom orchestration status in Durable Functions - Azure
|
3 | 3 | description: Learn how to configure and use custom orchestration status for Durable Functions.
|
4 | 4 | ms.topic: conceptual
|
5 |
| -ms.date: 05/10/2021 |
| 5 | +ms.date: 12/07/2022 |
6 | 6 | ms.author: azfuncdf
|
7 | 7 | ms.devlang: csharp, javascript, python
|
8 | 8 | ---
|
@@ -137,17 +137,15 @@ param($name)
|
137 | 137 | ```java
|
138 | 138 | @FunctionName("HelloCities")
|
139 | 139 | public String helloCitiesOrchestrator(
|
140 |
| - @DurableOrchestrationTrigger(name = "runtimeState") String runtimeState) { |
141 |
| - return OrchestrationRunner.loadAndRun(runtimeState, ctx -> { |
142 |
| - String result = ""; |
143 |
| - result += ctx.callActivity("SayHello", "Tokyo", String.class).await() + ", "; |
144 |
| - ctx.setCustomStatus("Tokyo"); |
145 |
| - result += ctx.callActivity("SayHello", "London", String.class).await() + ", "; |
146 |
| - ctx.setCustomStatus("London"); |
147 |
| - result += ctx.callActivity("SayHello", "Seattle", String.class).await(); |
148 |
| - ctx.setCustomStatus("Seattle"); |
149 |
| - return result; |
150 |
| - }); |
| 140 | + @DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) { |
| 141 | + String result = ""; |
| 142 | + result += ctx.callActivity("SayHello", "Tokyo", String.class).await() + ", "; |
| 143 | + ctx.setCustomStatus("Tokyo"); |
| 144 | + result += ctx.callActivity("SayHello", "London", String.class).await() + ", "; |
| 145 | + ctx.setCustomStatus("London"); |
| 146 | + result += ctx.callActivity("SayHello", "Seattle", String.class).await(); |
| 147 | + ctx.setCustomStatus("Seattle"); |
| 148 | + return result; |
151 | 149 | }
|
152 | 150 |
|
153 | 151 | @FunctionName("SayHello")
|
@@ -266,7 +264,13 @@ public HttpResponseMessage startHelloCities(
|
266 | 264 | String instanceId = client.scheduleNewOrchestrationInstance("HelloCities");
|
267 | 265 | context.getLogger().info("Created new Java orchestration with instance ID = " + instanceId);
|
268 | 266 |
|
269 |
| - OrchestrationMetadata metadata = client.waitForInstanceStart(instanceId, Duration.ofMinutes(5), true); |
| 267 | + OrchestrationMetadata metadata; |
| 268 | + try { |
| 269 | + metadata = client.waitForInstanceStart(instanceId, Duration.ofMinutes(5), true); |
| 270 | + } catch (TimeoutException ex) { |
| 271 | + return req.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).build(); |
| 272 | + } |
| 273 | + |
270 | 274 | while (metadata.readCustomStatusAs(String.class) != "London") {
|
271 | 275 | Thread.sleep(200);
|
272 | 276 | metadata = client.getInstanceMetadata(instanceId, true);
|
@@ -423,30 +427,28 @@ if ($userChoice -eq 3) {
|
423 | 427 |
|
424 | 428 | ```java
|
425 | 429 | @FunctionName("CityRecommender")
|
426 |
| -public String cityRecommender( |
427 |
| - @DurableOrchestrationTrigger(name = "runtimeState") String runtimeState) { |
428 |
| - return OrchestrationRunner.loadAndRun(runtimeState, ctx -> { |
429 |
| - int userChoice = ctx.getInput(int.class); |
430 |
| - switch (userChoice) { |
431 |
| - case 1: |
432 |
| - ctx.setCustomStatus(new Recommendation( |
433 |
| - new String[]{ "Tokyo", "Seattle" }, |
434 |
| - new String[]{ "Spring", "Summer" })); |
435 |
| - break; |
436 |
| - case 2: |
437 |
| - ctx.setCustomStatus(new Recommendation( |
438 |
| - new String[]{ "Seattle", "London" }, |
439 |
| - new String[]{ "Summer" })); |
440 |
| - break; |
441 |
| - case 3: |
442 |
| - ctx.setCustomStatus(new Recommendation( |
443 |
| - new String[]{ "Tokyo", "London" }, |
444 |
| - new String[]{ "Spring", "Summer" })); |
445 |
| - break; |
446 |
| - } |
| 430 | +public void cityRecommender( |
| 431 | + @DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) { |
| 432 | + int userChoice = ctx.getInput(int.class); |
| 433 | + switch (userChoice) { |
| 434 | + case 1: |
| 435 | + ctx.setCustomStatus(new Recommendation( |
| 436 | + new String[]{ "Tokyo", "Seattle" }, |
| 437 | + new String[]{ "Spring", "Summer" })); |
| 438 | + break; |
| 439 | + case 2: |
| 440 | + ctx.setCustomStatus(new Recommendation( |
| 441 | + new String[]{ "Seattle", "London" }, |
| 442 | + new String[]{ "Summer" })); |
| 443 | + break; |
| 444 | + case 3: |
| 445 | + ctx.setCustomStatus(new Recommendation( |
| 446 | + new String[]{ "Tokyo", "London" }, |
| 447 | + new String[]{ "Spring", "Summer" })); |
| 448 | + break; |
| 449 | + } |
447 | 450 |
|
448 |
| - // Wait for user selection with an external event handler |
449 |
| - }); |
| 451 | + // Wait for user selection with an external event |
450 | 452 | }
|
451 | 453 |
|
452 | 454 | class Recommendation {
|
@@ -578,22 +580,20 @@ return $isBookingConfirmed
|
578 | 580 |
|
579 | 581 | ```java
|
580 | 582 | @FunctionName("ReserveTicket")
|
581 |
| -public String reserveTicket( |
582 |
| - @DurableOrchestrationTrigger(name = "runtimeState") String runtimeState) { |
583 |
| - return OrchestrationRunner.loadAndRun(runtimeState, ctx -> { |
584 |
| - String userID = ctx.getInput(String.class); |
585 |
| - int discount = ctx.callActivity("CalculateDiscount", userID, int.class).await(); |
586 |
| - ctx.setCustomStatus(new DiscountInfo(discount, 60, "https://www.myawesomebookingweb.com")); |
587 |
| - |
588 |
| - boolean isConfirmed = ctx.waitForExternalEvent("BookingConfirmed", boolean.class).await(); |
589 |
| - if (isConfirmed) { |
590 |
| - ctx.setCustomStatus("Thank you for confirming your booking."); |
591 |
| - } else { |
592 |
| - ctx.setCustomStatus("There was a problem confirming your booking. Please try again."); |
593 |
| - } |
| 583 | +public boolean reserveTicket( |
| 584 | + @DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) { |
| 585 | + String userID = ctx.getInput(String.class); |
| 586 | + int discount = ctx.callActivity("CalculateDiscount", userID, int.class).await(); |
| 587 | + ctx.setCustomStatus(new DiscountInfo(discount, 60, "https://www.myawesomebookingweb.com")); |
| 588 | + |
| 589 | + boolean isConfirmed = ctx.waitForExternalEvent("BookingConfirmed", boolean.class).await(); |
| 590 | + if (isConfirmed) { |
| 591 | + ctx.setCustomStatus("Thank you for confirming your booking."); |
| 592 | + } else { |
| 593 | + ctx.setCustomStatus("There was a problem confirming your booking. Please try again."); |
| 594 | + } |
594 | 595 |
|
595 |
| - return isConfirmed; |
596 |
| - }); |
| 596 | + return isConfirmed; |
597 | 597 | }
|
598 | 598 |
|
599 | 599 | class DiscountInfo {
|
@@ -681,19 +681,17 @@ Set-DurableCustomStatus -CustomStatus @{ nextActions = @('A', 'B', 'C');
|
681 | 681 |
|
682 | 682 | ```java
|
683 | 683 | @FunctionName("MyCustomStatusOrchestrator")
|
684 |
| -public String myCustomStatusOrchestrator( |
685 |
| - @DurableOrchestrationTrigger(name = "runtimeState") String runtimeState) { |
686 |
| - return OrchestrationRunner.loadAndRun(runtimeState, ctx -> { |
687 |
| - // ... do work ... |
688 |
| - |
689 |
| - // update the status of the orchestration with some arbitrary data |
690 |
| - CustomStatusPayload payload = new CustomStatusPayload(); |
691 |
| - payload.nextActions = new String[] { "A", "B", "C" }; |
692 |
| - payload.foo = 2; |
693 |
| - ctx.setCustomStatus(payload); |
694 |
| - |
695 |
| - // ... do more work ... |
696 |
| - }); |
| 684 | +public void myCustomStatusOrchestrator( |
| 685 | + @DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) { |
| 686 | + // ... do work ... |
| 687 | + |
| 688 | + // update the status of the orchestration with some arbitrary data |
| 689 | + CustomStatusPayload payload = new CustomStatusPayload(); |
| 690 | + payload.nextActions = new String[] { "A", "B", "C" }; |
| 691 | + payload.foo = 2; |
| 692 | + ctx.setCustomStatus(payload); |
| 693 | + |
| 694 | + // ... do more work ... |
697 | 695 | }
|
698 | 696 |
|
699 | 697 | class CustomStatusPayload {
|
|
0 commit comments