123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- 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)
- }
|