wait_result may ok

This commit is contained in:
yedf2 2021-08-03 18:10:43 +08:00
parent 0df21c7c70
commit 3a9aaa3166
3 changed files with 30 additions and 10 deletions

View File

@ -41,8 +41,7 @@ func submit(c *gin.Context) (interface{}, error) {
} }
t.Status = "submitted" t.Status = "submitted"
t.saveNew(db) t.saveNew(db)
go t.Process(db) return t.Process(db, c.Query("wait_result") == "true" || c.Query("wait_result") == "1"), nil
return dtmcli.ResultSuccess, nil
} }
func abort(c *gin.Context) (interface{}, error) { func abort(c *gin.Context) (interface{}, error) {
@ -52,8 +51,7 @@ func abort(c *gin.Context) (interface{}, error) {
if t.TransType != "xa" && t.TransType != "tcc" || dbt.Status != "prepared" && dbt.Status != "aborting" { if t.TransType != "xa" && t.TransType != "tcc" || dbt.Status != "prepared" && dbt.Status != "aborting" {
return M{"dtm_result": "FAILURE", "message": fmt.Sprintf("trans type: %s current status %s, cannot abort", dbt.TransType, dbt.Status)}, nil return M{"dtm_result": "FAILURE", "message": fmt.Sprintf("trans type: %s current status %s, cannot abort", dbt.TransType, dbt.Status)}, nil
} }
go dbt.Process(db) return dbt.Process(db, c.Query("wait_result") == "true" || c.Query("wait_result") == "1"), nil
return dtmcli.ResultSuccess, nil
} }
func registerXaBranch(c *gin.Context) (interface{}, error) { func registerXaBranch(c *gin.Context) (interface{}, error) {

View File

@ -1,6 +1,7 @@
package dtmsvr package dtmsvr
import ( import (
"fmt"
"math" "math"
"math/rand" "math/rand"
"runtime/debug" "runtime/debug"
@ -12,7 +13,7 @@ import (
// CronTransOnce cron expired trans. use expireIn as expire time // CronTransOnce cron expired trans. use expireIn as expire time
func CronTransOnce(expireIn time.Duration) bool { func CronTransOnce(expireIn time.Duration) bool {
defer handlePanic() defer handlePanic(nil)
trans := lockOneTrans(expireIn) trans := lockOneTrans(expireIn)
if trans == nil { if trans == nil {
return false return false
@ -20,7 +21,7 @@ func CronTransOnce(expireIn time.Duration) bool {
if TransProcessedTestChan != nil { if TransProcessedTestChan != nil {
defer WaitTransProcessed(trans.Gid) defer WaitTransProcessed(trans.Gid)
} }
trans.Process(dbGet()) trans.Process(dbGet(), true)
return true return true
} }
@ -52,9 +53,12 @@ func lockOneTrans(expireIn time.Duration) *TransGlobal {
return &trans return &trans
} }
func handlePanic() { func handlePanic(perr *error) {
if err := recover(); err != nil { if err := recover(); err != nil {
common.RedLogf("----panic %s handlered\n%s", err.(error).Error(), string(debug.Stack())) common.RedLogf("----panic %v handlered\n%s", err, string(debug.Stack()))
if perr != nil {
*perr = fmt.Errorf("dtm panic: %v", err)
}
} }
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/yedf/dtm/common" "github.com/yedf/dtm/common"
"github.com/yedf/dtm/dtmcli"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
) )
@ -110,8 +111,24 @@ func (t *TransGlobal) getProcessor() transProcessor {
} }
// Process process global transaction once // Process process global transaction once
func (t *TransGlobal) Process(db *common.DB) { func (t *TransGlobal) Process(db *common.DB, waitResult bool) common.M {
defer handlePanic() if !waitResult {
go t.processInner(db)
return dtmcli.ResultSuccess
}
submitting := t.Status == "submitted"
err := t.processInner(db)
if err != nil {
return common.M{"dtm_result": "FAILURE", "message": err.Error()}
}
if submitting && t.Status != "succeed" {
return common.M{"dtm_result": "FAILURE", "message": "trans failed by user"}
}
return dtmcli.ResultSuccess
}
func (t *TransGlobal) processInner(db *common.DB) (rerr error) {
defer handlePanic(&rerr)
defer func() { defer func() {
if TransProcessedTestChan != nil { if TransProcessedTestChan != nil {
logrus.Printf("processed: %s", t.Gid) logrus.Printf("processed: %s", t.Gid)
@ -126,6 +143,7 @@ func (t *TransGlobal) Process(db *common.DB) {
branches := []TransBranch{} branches := []TransBranch{}
db.Must().Where("gid=?", t.Gid).Order("id asc").Find(&branches) db.Must().Where("gid=?", t.Gid).Order("id asc").Find(&branches)
t.getProcessor().ProcessOnce(db, branches) t.getProcessor().ProcessOnce(db, branches)
return
} }
func (t *TransGlobal) getBranchParams(branch *TransBranch) common.MS { func (t *TransGlobal) getBranchParams(branch *TransBranch) common.MS {