package main import ( "errors" "fmt" "git.sxidc.com/yjp/dapr_client" "git.sxidc.com/yjp/dapr_client/saga" "time" ) func main() { dapr_client.InitDAPR(10) defer dapr_client.DestroyDAPR() saga.Init("statestore", "test-saga") defer saga.Destroy() ok() rollback() for { } } func ok() { orchestrator := saga.GetInstance().BuildOrchestrator("test-ok", 5*time.Second). AddStep(&saga.Step{ StepFunc: func() error { fmt.Println("one") return nil }, StepRollbackFunc: func() error { fmt.Println("one rollback") return nil }, }). AddStep(&saga.Step{ StepFunc: func() error { fmt.Println("two") return nil }, StepRollbackFunc: func() error { fmt.Println("two rollback") return nil }, }). AddStep(&saga.Step{ StepFunc: func() error { fmt.Println("three") return nil }, StepRollbackFunc: nil, }). Output(func() (interface{}, error) { return "ok result", nil }) result, err := orchestrator.Run() if err != nil { fmt.Println(err) return } fmt.Println("Result:", result) } func rollback() { orchestrator := saga.GetInstance().BuildOrchestrator("test-rollback", 5*time.Second). AddStep(&saga.Step{ StepFunc: func() error { fmt.Println("one") return nil }, StepRollbackFunc: func() error { fmt.Println("one rollback") return nil }, }). AddStep(&saga.Step{ StepFunc: func() error { fmt.Println("two") return nil }, StepRollbackFunc: func() error { fmt.Println("two rollback") return nil }, }). AddStep(&saga.Step{ StepFunc: func() error { fmt.Println("three") return nil }, StepRollbackFunc: nil, }). AddStep(&saga.Step{ StepFunc: func() error { return errors.New("four error") }, StepRollbackFunc: nil, }). Output(func() (interface{}, error) { return "should not come here", nil }) result, err := orchestrator.Run() if err != nil { fmt.Println(err) return } fmt.Println("Result:", result) }