Commit 06407ece authored by 贾浩@五瓣科技's avatar 贾浩@五瓣科技

rm task.go

parent b818ad31
package server
import (
. "contract_backend/constant"
"contract_backend/util"
"crypto/ecdsa"
"io"
"math/big"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
func taskChange(c *gin.Context) {
// 获取原始json数据
rawJson, err := io.ReadAll(c.Request.Body)
if err != nil {
log.WithError(err).Error("read body error")
c.JSON(200, withError(InvalidParam))
return
}
// 判断是插入还是update
table := gjson.Get(string(rawJson), "table").String()
if table != "exec_task" {
log.WithField("table", table).Error("wrong table")
c.JSON(200, withError(InvalidParam))
return
}
var userId string
var taskId string
var appId string
var modelIds []string
var key *ecdsa.PrivateKey
operation := gjson.Get(string(rawJson), "operation").String()
switch operation {
case "UPDATE":
userId = gjson.Get(string(rawJson), "new_row.user_id").String()
taskId = gjson.Get(string(rawJson), "new_row.id").String()
appId = gjson.Get(string(rawJson), "new_row.app_key").String()
tmp := gjson.Get(string(rawJson), "new_row.model_ids").Array()
for _, v := range tmp {
modelIds = append(modelIds, v.String())
}
key, err = util.GetPrivKeyByPriv(conf.Chain.XPrivateKey, userId)
if err != nil {
log.WithError(err).Error("get private key error")
c.JSON(200, withError(InternalError))
return
}
default:
log.WithField("operation", operation).Error("wrong operation")
c.JSON(200, withError(InvalidParam))
return
}
oldStatus := gjson.Get(string(rawJson), "old_row.status").Int()
newStatus := gjson.Get(string(rawJson), "new_row.status").Int()
if !(oldStatus == 0 && newStatus == 1) {
c.JSON(200, withSuccess(""))
return
}
dbTxHash := gjson.Get(string(rawJson), "new_row.tx_hash").String()
txStatus := gjson.Get(string(rawJson), "new_row.tx_status").Int()
if dbTxHash != "" || txStatus != 0 {
log.WithField("txHash", dbTxHash).WithField("txStatus", txStatus).Error("already processed")
c.JSON(200, withError(InternalError))
return
}
val10, _ := big.NewInt(10).SetString("10000000000000000000", 10)
txHash, err := d.ExecuteTask(key, taskId, userId, appId, modelIds, val10)
if err != nil {
log.WithError(err).Error("execute task error")
c.JSON(200, withError(InternalError))
return
}
err = d.UpdateTask(taskId, txHash.String(), 0)
if err != nil {
log.WithError(err).Error("update task error")
c.JSON(200, withError(InternalError))
return
}
c.JSON(200, withSuccess(txHash.String()))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment