4.Saga事务
原创
©著作权归作者所有:来自51CTO博客作者步_步_为营的原创作品,请联系作者获取转载授权,否则将追究法律责任
Saga事务
Saga可以封装一系列步骤,并可以为每个步骤进行补偿操作。
//Task2会抛出异常,则会触发undotask2和undotask1
builder
.StartWith(context => Console.WriteLine("Begin"))
.Saga(saga => saga
.StartWith<Task1>()
.CompensateWith<UndoTask1>()//补偿操作
.Then<Task2>()
.CompensateWith<UndoTask2>()
.Then<Task3>()
.CompensateWith<UndoTask3>()
)
.Then(context => Console.WriteLine("End"));
saga事务的重试
//该案例会5s重试一次
builder
.StartWith(context => Console.WriteLine("Begin"))
.Saga(saga => saga
.StartWith<Task1>()
.CompensateWith<UndoTask1>()
.Then<Task2>()
.CompensateWith<UndoTask2>()
.Then<Task3>()
.CompensateWith<UndoTask3>()
)
.OnError(Models.WorkflowErrorHandling.Retry, TimeSpan.FromSeconds(5))
.Then(context => Console.WriteLine("End"));
补偿整个saga事务
builder
.StartWith(context => Console.WriteLine("Begin"))
.Saga(saga => saga
.StartWith<Task1>()
.Then<Task2>()
.Then<Task3>()
)
.CompensateWith<UndoEverything>()//补充整个saga事务
.Then(context => Console.WriteLine("End"));
给补偿步骤传参
builder
.StartWith<SayHello>()
.CompensateWith<PrintMessage>(compensate =>
{
compensate.Input(step => step.Message, data => "undoing...");
})