mirror of https://github.com/dtm-labs/dtm.git
csharpjavadistributed-transactionsdtmgogolangmicroservicenodejsphpdatabasesagaseatatcctransactiontransactionsxapythondistributed
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
854 B
38 lines
854 B
package dtmgrpc
|
|
|
|
import (
|
|
context "context"
|
|
|
|
"github.com/yedf/dtm/dtmcli"
|
|
)
|
|
|
|
// SagaGrpc struct of saga
|
|
type SagaGrpc struct {
|
|
dtmcli.TransBase
|
|
Steps []dtmcli.SagaStep `json:"steps"`
|
|
}
|
|
|
|
// NewSaga create a saga
|
|
func NewSaga(server string, gid string) *SagaGrpc {
|
|
return &SagaGrpc{TransBase: *dtmcli.NewTransBase(gid, "saga", server, "")}
|
|
}
|
|
|
|
// Add add a saga step
|
|
func (s *SagaGrpc) Add(action string, compensate string, busiData []byte) *SagaGrpc {
|
|
s.Steps = append(s.Steps, dtmcli.SagaStep{
|
|
Action: action,
|
|
Compensate: compensate,
|
|
Data: string(busiData),
|
|
})
|
|
return s
|
|
}
|
|
|
|
// Submit submit the saga trans
|
|
func (s *SagaGrpc) Submit() error {
|
|
_, err := MustGetDtmClient(s.Dtm).Submit(context.Background(), &DtmRequest{
|
|
Gid: s.Gid,
|
|
TransType: s.TransType,
|
|
Data: dtmcli.MustMarshalString(&s.Steps),
|
|
})
|
|
return err
|
|
}
|
|
|