dtm/dtmsvr/trans_tcc.go
2021-07-29 19:15:08 +08:00

49 lines
1.5 KiB
Go

package dtmsvr
import (
"fmt"
"strings"
"github.com/yedf/dtm/common"
)
type transTccProcessor struct {
*TransGlobal
}
func init() {
registorProcessorCreator("tcc", func(trans *TransGlobal) transProcessor { return &transTccProcessor{TransGlobal: trans} })
}
func (t *transTccProcessor) GenBranches() []TransBranch {
return []TransBranch{}
}
func (t *transTccProcessor) ExecBranch(db *common.DB, branch *TransBranch) {
resp, err := common.RestyClient.R().SetBody(branch.Data).SetHeader("Content-type", "application/json").SetQueryParams(t.getBranchParams(branch)).Post(branch.URL)
e2p(err)
body := resp.String()
if strings.Contains(body, "SUCCESS") {
t.touch(db, config.TransCronInterval)
branch.changeStatus(db, "succeed")
} else if branch.BranchType == "try" && strings.Contains(body, "FAILURE") {
t.touch(db, config.TransCronInterval)
branch.changeStatus(db, "failed")
} else {
panic(fmt.Errorf("unknown response: %s, will be retried", body))
}
}
func (t *transTccProcessor) ProcessOnce(db *common.DB, branches []TransBranch) {
if t.Status == "succeed" || t.Status == "failed" {
return
}
branchType := common.If(t.Status == "submitted", "confirm", "cancel").(string)
for current := len(branches) - 1; current >= 0; current-- {
if branches[current].BranchType == branchType && branches[current].Status == "prepared" {
t.ExecBranch(db, &branches[current])
}
}
t.changeStatus(db, common.If(t.Status == "submitted", "succeed", "failed").(string))
}