60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package dtmcli
|
||
|
||
import (
|
||
"github.com/sirupsen/logrus"
|
||
"github.com/yedf/dtm/common"
|
||
)
|
||
|
||
// Saga struct of saga
|
||
type Saga struct {
|
||
SagaData
|
||
Server string
|
||
}
|
||
|
||
// SagaData sage data
|
||
type SagaData struct {
|
||
Gid string `json:"gid"`
|
||
TransType string `json:"trans_type"`
|
||
Steps []SagaStep `json:"steps"`
|
||
}
|
||
|
||
// SagaStep one step of saga
|
||
type SagaStep struct {
|
||
Action string `json:"action"`
|
||
Compensate string `json:"compensate"`
|
||
Data string `json:"data"`
|
||
}
|
||
|
||
// NewSaga create a saga
|
||
func NewSaga(server string, gid string) *Saga {
|
||
return &Saga{
|
||
SagaData: SagaData{
|
||
Gid: gid,
|
||
TransType: "saga",
|
||
},
|
||
Server: server,
|
||
}
|
||
}
|
||
|
||
// Add add a saga step
|
||
func (s *Saga) Add(action string, compensate string, postData interface{}) *Saga {
|
||
logrus.Printf("saga %s Add %s %s %v", s.Gid, action, compensate, postData)
|
||
step := SagaStep{
|
||
Action: action,
|
||
Compensate: compensate,
|
||
Data: common.MustMarshalString(postData),
|
||
}
|
||
s.Steps = append(s.Steps, step)
|
||
return s
|
||
}
|
||
|
||
// Submit submit the saga trans
|
||
func (s *Saga) Submit() error {
|
||
return s.SubmitExt(&TransOptions{})
|
||
}
|
||
|
||
// SubmitExt 高级submit,更多的选项和更详细的返回值
|
||
func (s *Saga) SubmitExt(opt *TransOptions) error {
|
||
return CallDtm(s.Server, &s.SagaData, "submit", opt)
|
||
}
|