Commit 19be3a76 authored by szh's avatar szh

rollback change to chan

parent edd89b0c
......@@ -27,6 +27,7 @@ func init() {
var tlog = log.New("main", "main.go")
var serial string
// @title Golang Gin API
// @version 1.0
// @description An example of gin
......@@ -42,7 +43,7 @@ func main() {
//树莓派内置程序操作
go app.PaiJob(time.NewTicker(time.Second * 50))
//go app.ClearLog(time.NewTicker(time.Hour*1))
go app.RollbackJob()
//上传统计树莓派信息
go app.UploadInfo(time.NewTicker(time.Minute * 10))
......
......@@ -892,3 +892,4 @@ func GetNodeUpdateStatus() bool {
}
return false
}
package app
import (
"time"
"chain33-pai/pkg/setting"
"bytes"
"os/exec"
"fmt"
"chain33-pai/service/pai_service"
)
var RollChan chan bool
func RollbackJob() {
for {
if value,err := <- RollChan;err != nil {
if !value {
continue
}
err := rollback()
if err == nil {
tlog.Info("rollback success","err",nil)
} else {
tlog.Error("rollback fail ","err",err)
}
}
time.Sleep(time.Second * 1)
}
}
func rollback() error {
BityuanFlag = true
_,err := SafeCloseNode()
if err != nil {
BityuanFlag = false
return err
}
var pai pai_service.Pai
err = pai.SetPai()
if err != nil {
tlog.Error("SetPai","err",err)
BityuanFlag = false
return err
}
if pai.LocalLastHeight == 0 {
tlog.Error("LocalLastHeight","height",pai.LocalLastHeight)
BityuanFlag = false
return err
}
rollheight := pai.LocalLastHeight - 1000
if (pai.LocalLastHeight - 1000) <= 0 {
rollheight = 1
}
time.Sleep(time.Second * 2)
tlog.Info("rollback start","height",pai.LocalLastHeight,"rollbackheight",rollheight)
var buf bytes.Buffer
path := GetAbsPath(setting.BityuanSetting.Name)
if path == "" {
tlog.Error("GetAbsPath","err","path not exists")
BityuanFlag = false
return err
}
cmd := exec.Command(path+"/"+setting.BityuanSetting.Name, "-rollback",fmt.Sprintf("%d",rollheight))
cmd.Stdout = &buf
err = cmd.Run()
if err != nil {
BityuanFlag = false
return err
}
tlog.Info("rollback end","height",pai.LocalLastHeight,"rollbackheight",rollheight)
BityuanFlag = false
return nil
}
\ No newline at end of file
......@@ -554,49 +554,6 @@ func Test(c *gin.Context) {
func Rollback(c *gin.Context) {
appG:=app.Gin{C:c}
app.BityuanFlag = true
_,err := app.SafeCloseNode()
if err != nil {
appG.Response(http.StatusOK,e.NODE_ERROR,err)
app.BityuanFlag = false
return
}
var pai pai_service.Pai
err = pai.SetPai()
if err != nil {
tlog.Error("SetPai","err",err)
appG.Response(http.StatusOK,e.NODE_ERROR,err)
app.BityuanFlag = false
return
}
if pai.LocalLastHeight == 0 {
tlog.Error("LocalLastHeight","height",pai.LocalLastHeight)
appG.Response(http.StatusOK,e.NODE_ERROR,err)
app.BityuanFlag = false
return
}
rollheight := pai.LocalLastHeight - 1000
if (pai.LocalLastHeight - 1000) <= 0 {
rollheight = 1
}
time.Sleep(time.Second * 2)
tlog.Info("rollback start","height",pai.LocalLastHeight,"rollbackheight",rollheight)
var buf bytes.Buffer
path := app.GetAbsPath(setting.BityuanSetting.Name)
if path == "" {
tlog.Error("GetAbsPath","err","path not exists")
appG.Response(http.StatusOK,e.NODE_ERROR,err)
return
}
cmd := exec.Command(path+"/"+setting.BityuanSetting.Name, "-rollback",fmt.Sprintf("%d",rollheight))
cmd.Stdout = &buf
err = cmd.Run()
if err != nil {
appG.Response(http.StatusOK,e.NODE_ERROR,err)
app.BityuanFlag = false
return
}
tlog.Info("rollback end","height",pai.LocalLastHeight,"rollbackheight",rollheight)
app.BityuanFlag = false
app.RollChan <- true
appG.Response(http.StatusOK,e.SUCCESS,"bityuan rollback")
}
\ No newline at end of file
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