orchestrator.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package saga
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. type StepFunc func() error
  7. type StepRollbackFunc func() error
  8. type OutputFunc func() (interface{}, error)
  9. type Step struct {
  10. StepFunc StepFunc
  11. StepRollbackFunc StepRollbackFunc
  12. RollbackContextData string
  13. rollbackDone chan interface{}
  14. }
  15. type Orchestrator struct {
  16. stateStoreName string
  17. sagaName string
  18. name string
  19. rollbackRetryPeriodSec time.Duration
  20. steps []*Step
  21. OutputFunc OutputFunc
  22. }
  23. func (orchestrator *Orchestrator) AddStep(step *Step) *Orchestrator {
  24. orchestrator.steps = append(orchestrator.steps, step)
  25. return orchestrator
  26. }
  27. func (orchestrator *Orchestrator) Output(outputFunc OutputFunc) *Orchestrator {
  28. orchestrator.OutputFunc = outputFunc
  29. return orchestrator
  30. }
  31. func (orchestrator *Orchestrator) Run() (interface{}, error) {
  32. for index, step := range orchestrator.steps {
  33. if step.StepFunc == nil {
  34. continue
  35. }
  36. err := step.StepFunc()
  37. if err == nil {
  38. continue
  39. }
  40. if err != nil {
  41. err = saveOrchestratorState(orchestrator.stateStoreName, orchestrator.sagaName,
  42. orchestrator.name, step.RollbackContextData, index)
  43. if err != nil {
  44. return nil, err
  45. }
  46. go orchestrator.Rollback(index)
  47. return nil, err
  48. }
  49. }
  50. if orchestrator.OutputFunc != nil {
  51. return orchestrator.OutputFunc()
  52. }
  53. return nil, nil
  54. }
  55. func (orchestrator *Orchestrator) Rollback(startIndex int) {
  56. for i := startIndex; i >= 0; i-- {
  57. rollbackStep := orchestrator.steps[i]
  58. err := orchestrator.rollbackStep(rollbackStep)
  59. if err != nil {
  60. fmt.Println("Rollback", "orchestrator.rollbackStep", err)
  61. }
  62. err = saveOrchestratorState(orchestrator.stateStoreName, orchestrator.sagaName, orchestrator.name,
  63. rollbackStep.RollbackContextData, i-1)
  64. if err != nil {
  65. fmt.Println("Rollback", "saveOrchestratorState", err)
  66. return
  67. }
  68. }
  69. err := deleteOrchestratorState(orchestrator.stateStoreName, orchestrator.sagaName, orchestrator.name)
  70. if err != nil {
  71. fmt.Println("Rollback", "deleteOrchestratorState", err)
  72. return
  73. }
  74. }
  75. func (orchestrator *Orchestrator) rollbackStep(rollbackStep *Step) error {
  76. if rollbackStep.StepRollbackFunc == nil {
  77. return nil
  78. }
  79. err := rollbackStep.StepRollbackFunc()
  80. if err == nil {
  81. return nil
  82. }
  83. orchestrator.rollbackRetry(rollbackStep)
  84. return nil
  85. }
  86. func (orchestrator *Orchestrator) rollbackRetry(step *Step) {
  87. ticker := time.NewTicker(orchestrator.rollbackRetryPeriodSec)
  88. for {
  89. select {
  90. case <-step.rollbackDone:
  91. ticker.Stop()
  92. ticker = nil
  93. case <-ticker.C:
  94. err := step.StepRollbackFunc()
  95. if err == nil {
  96. return
  97. }
  98. }
  99. }
  100. }
  101. func (orchestrator *Orchestrator) stop() {
  102. for _, step := range orchestrator.steps {
  103. if step.rollbackDone != nil {
  104. step.rollbackDone <- true
  105. close(step.rollbackDone)
  106. step.rollbackDone = nil
  107. }
  108. }
  109. }