forked from dtm-labs/dtm
/
trans_saga.go
54 lines (45 loc) · 1.5 KB
/
trans_saga.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* Copyright (c) 2021 yedf. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
package dtmcli
import (
"github.com/10antz-inc/pf-dtm/client/dtmcli/dtmimp"
)
// Saga struct of saga
type Saga struct {
dtmimp.TransBase
orders map[int][]int
}
// NewSaga create a saga
func NewSaga(server string, gid string) *Saga {
return &Saga{TransBase: *dtmimp.NewTransBase(gid, "saga", server, ""), orders: map[int][]int{}}
}
// Add add a saga step
func (s *Saga) Add(action string, compensate string, postData interface{}) *Saga {
s.Steps = append(s.Steps, map[string]string{"action": action, "compensate": compensate})
s.Payloads = append(s.Payloads, dtmimp.MustMarshalString(postData))
return s
}
// AddBranchOrder specify that branch should be after preBranches. branch should is larger than all the element in preBranches
func (s *Saga) AddBranchOrder(branch int, preBranches []int) *Saga {
s.orders[branch] = preBranches
return s
}
// SetConcurrent enable the concurrent exec of sub trans
func (s *Saga) SetConcurrent() *Saga {
s.Concurrent = true
return s
}
// Submit submit the saga trans
func (s *Saga) Submit() error {
s.BuildCustomOptions()
return dtmimp.TransCallDtm(&s.TransBase, "submit")
}
// BuildCustomOptions add custom options to the request context
func (s *Saga) BuildCustomOptions() {
if s.Concurrent {
s.CustomData = dtmimp.MustMarshalString(map[string]interface{}{"orders": s.orders, "concurrent": s.Concurrent})
}
}