During this exercise, you will:
- Orchestrate Activities using a Saga pattern to implement compensating transactions
- Handle failures with rollback logic
Make your changes to the code in the practice
subdirectory (look for TODO
comments that will guide you to where you should make changes to the code). If you need a hint or want to verify your changes, look at the complete version in the solution
subdirectory.
This Exercise uses the same structure as in the previous Exercises — meaning that it will fail at the very end on the ValidateCreditCard
Activity if you provide it with a bad credit card number.
Three new Activities have been created to demonstrate rollback actions.
UpdateInventory
reduces the stock from the pizza inventory once the pizza order comes through.RevertInventory
has also been added as a compensating action forUpdateInventory
. It add the ingredients back into the pizza inventory.RefundCustomer
has been added as a compensating action forSendBill
.
- Review these new Activities in
Activities.cs
in theWorkflow
directory. None of them make actual inventory or billing changes, because the intent of this Activity is to show Temporal features, but you should be able to see where you could add functionality here. - Close the files.
Now you will implement a compensating action using Activities in your Temporal Workflow.
Also, note that we register compensating actions before executing their corresponding Activities. Consider this example of why we use this pattern.
- An Activity (like
UpdateInventory
) executes successfully and updates the inventory system. - However, right after the update succeeds, a network failure occurs.
- The Activity fails to report its success back to the Workflow From the Workflow's perspective, the Activity failed.
- If we had registered the compensation after the activity, the compensation would never be registered
- Open
PizzaWorkflow.cs
from yourWorkflow
directory. - Note that a List,
compensations
, has been added at the top to keep track of each Activity's compensating action. - Note that after the bill is created in the
PizzaWorkflow
file, theUpdateInventory
Activity is executed, before theSendBill
Activity is called. The compensating action was added to the compensations list in the list above. Study this and use it for the next step. - Locate the invocation for the
SendBill
Activity. Add the appropriate compensating Activity to the compensations list, containing the compensating input. Use the previous step as a reference.
In this part of the exercise, you will create a function which will loop through the each one of the items in your compensations
list in a synchronous order. In the case of an error, we will invoke this function to roll back on any Activities we want to undo.
- In the
PizzaWorkflow.cs
file, locate theCompensate
Task after thePizzaWorkflow
. - Call the
Reverse
method before you loop through thecompensations
list. This ensures that compensating actions are called in reverse order, aligning with the correct sequence to roll back local transactions that have already completed.
In this part of the exercise, you will call the Compensate
function that you defined in Part C.
- In the
PizzaWorkflow.cs
file, notice you have atry/catch
block. You call your Activities in thetry
block. In thecatch
block, if an error occurs, we want to roll back all of the Activities that have so far executed by calling the compensating actions. - In the
catch
block of thePizzaWorkflow
, callawait CompensateAsync()
. Now ifValidateCreditCard
fails, first we roll back onSendBill
by callingRefundCustomer
. Next, we will roll back onUpdateInventory
by callingRevertInventory
. - Save the file.
To run the Workflow:
- In one terminal, start the Worker by running
dotnet run --project Worker
. - In another terminal, start the Workflow by running
dotnet run --project Client
. - You should see the Workflow Execution failed. There is now a
WorkflowExecutionFailed
Event in the Web UI. - Over in the Web UI (or the terminal window where your Worker ran), you can see that after the
ValidateCreditCard
Activity failed, we then called the Activities:RefundCustomer
andRevertInventory
.