|
| 1 | +# Saga transaction with compensation sample |
| 2 | + |
| 3 | +Illustrates how to encapsulate a sequence of steps within a saga transaction and specify compensation steps for each. |
| 4 | + |
| 5 | +In the sample, `Task2` will throw an exception, then `UndoTask2` and `UndoTask1` will be triggered. |
| 6 | + |
| 7 | +```c# |
| 8 | +builder |
| 9 | + .StartWith(context => Console.WriteLine("Begin")) |
| 10 | + .Saga(saga => saga |
| 11 | + .StartWith<Task1>() |
| 12 | + .CompensateWith<UndoTask1>() |
| 13 | + .Then<Task2>() |
| 14 | + .CompensateWith<UndoTask2>() |
| 15 | + .Then<Task3>() |
| 16 | + .CompensateWith<UndoTask3>() |
| 17 | + ) |
| 18 | + .OnError(Models.WorkflowErrorHandling.Retry, TimeSpan.FromSeconds(5)) |
| 19 | + .Then(context => Console.WriteLine("End")); |
| 20 | +``` |
| 21 | + |
| 22 | +## Retry policy for failed saga transaction |
| 23 | + |
| 24 | +This particular example will retry the saga every 5 seconds, but you could also simply fail completely, and process a master compensation task for the whole saga. |
| 25 | + |
| 26 | +```c# |
| 27 | +builder |
| 28 | + .StartWith(context => Console.WriteLine("Begin")) |
| 29 | + .Saga(saga => saga |
| 30 | + .StartWith<Task1>() |
| 31 | + .CompensateWith<UndoTask1>() |
| 32 | + .Then<Task2>() |
| 33 | + .CompensateWith<UndoTask2>() |
| 34 | + .Then<Task3>() |
| 35 | + .CompensateWith<UndoTask3>() |
| 36 | + ) |
| 37 | + .CompensateWith<CleanUp>() |
| 38 | + .Then(context => Console.WriteLine("End")); |
| 39 | +``` |
| 40 | + |
| 41 | +## Compensate entire saga transaction |
| 42 | + |
| 43 | +You could also only specify a master compensation step, as follows |
| 44 | + |
| 45 | +```c# |
| 46 | +builder |
| 47 | + .StartWith(context => Console.WriteLine("Begin")) |
| 48 | + .Saga(saga => saga |
| 49 | + .StartWith<Task1>() |
| 50 | + .Then<Task2>() |
| 51 | + .Then<Task3>() |
| 52 | + ) |
| 53 | + .CompensateWith<UndoEverything>() |
| 54 | + .Then(context => Console.WriteLine("End")); |
| 55 | +``` |
| 56 | + |
| 57 | +## Passing parameters to compensation steps |
| 58 | + |
| 59 | +Parameters can be passed to a compensation step as follows |
| 60 | + |
| 61 | +```c# |
| 62 | +builder |
| 63 | + .StartWith<SayHello>() |
| 64 | + .CompensateWith<PrintMessage>(compensate => |
| 65 | + { |
| 66 | + compensate.Input(step => step.Message, data => "undoing..."); |
| 67 | + }) |
| 68 | +``` |
| 69 | + |
| 70 | +## Expressing a saga in JSON |
| 71 | + |
| 72 | +A saga transaction can be expressed in JSON, by using the `WorkflowCore.Primitives.Sequence` step and setting the `Saga` parameter to `true`. |
| 73 | + |
| 74 | +The compensation steps can be defined by specifying the `CompensateWith` parameter. |
| 75 | + |
| 76 | +```json |
| 77 | +{ |
| 78 | + "Id": "Saga-Sample", |
| 79 | + "Version": 1, |
| 80 | + "DataType": "MyApp.MyDataClass, MyApp", |
| 81 | + "Steps": [ |
| 82 | + { |
| 83 | + "Id": "Hello", |
| 84 | + "StepType": "MyApp.HelloWorld, MyApp", |
| 85 | + "NextStepId": "MySaga" |
| 86 | + }, |
| 87 | + { |
| 88 | + "Id": "MySaga", |
| 89 | + "StepType": "WorkflowCore.Primitives.Sequence, WorkflowCore", |
| 90 | + "NextStepId": "Bye", |
| 91 | + "Saga": true, |
| 92 | + "Do": [ |
| 93 | + [ |
| 94 | + { |
| 95 | + "Id": "do1", |
| 96 | + "StepType": "MyApp.Task1, MyApp", |
| 97 | + "NextStepId": "do2", |
| 98 | + "CompensateWith": [ |
| 99 | + { |
| 100 | + "Id": "undo1", |
| 101 | + "StepType": "MyApp.UndoTask1, MyApp" |
| 102 | + } |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "Id": "do2", |
| 107 | + "StepType": "MyApp.Task2, MyApp", |
| 108 | + "CompensateWith": [ |
| 109 | + { |
| 110 | + "Id": "undo2-1", |
| 111 | + "NextStepId": "undo2-2", |
| 112 | + "StepType": "MyApp.UndoTask2, MyApp" |
| 113 | + }, |
| 114 | + { |
| 115 | + "Id": "undo2-2", |
| 116 | + "StepType": "MyApp.DoSomethingElse, MyApp" |
| 117 | + } |
| 118 | + ] |
| 119 | + } |
| 120 | + ] |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "Id": "Bye", |
| 125 | + "StepType": "MyApp.GoodbyeWorld, MyApp" |
| 126 | + } |
| 127 | + ] |
| 128 | +} |
| 129 | +``` |
0 commit comments