Commit 492bf342 authored by szh's avatar szh

autossh 定期清理链接

parent 31b938c8
...@@ -37,10 +37,10 @@ func main() { ...@@ -37,10 +37,10 @@ func main() {
gin.SetMode(setting.ServerSetting.RunMode) gin.SetMode(setting.ServerSetting.RunMode)
//区域网广播设备 //区域网广播设备
go broadcast() go broadcast()
//定时监控节点 //节点操作
go app.CornProcessJob(time.NewTicker(time.Second*30)) go app.BityuanJob(time.NewTicker(time.Second*30))
//定时自动更新树莓派内置程序 //树莓派内置程序操作
go app.AutoUpdate(time.NewTicker(time.Second * 50)) go app.PaiJob(time.NewTicker(time.Second * 50))
//go app.ClearLog(time.NewTicker(time.Hour*1)) //go app.ClearLog(time.NewTicker(time.Hour*1))
//上传统计树莓派信息 //上传统计树莓派信息
......
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
"encoding/json" "encoding/json"
"chain33-pai/pkg/chain33" "chain33-pai/pkg/chain33"
types2 "github.com/33cn/plugin/plugin/dapp/ticket/types" types2 "github.com/33cn/plugin/plugin/dapp/ticket/types"
"strconv"
) )
var tlog = log.New("pkg","app") var tlog = log.New("pkg","app")
...@@ -138,7 +139,7 @@ func StrFilter(str []string,filter string)(res []string){ ...@@ -138,7 +139,7 @@ func StrFilter(str []string,filter string)(res []string){
} }
//corn job for collecting chain33 process info //corn job for collecting chain33 process info
func CornProcessJob(ticker *time.Ticker){ func BityuanJob(ticker *time.Ticker){
NodeError = getWalletInfo() NodeError = getWalletInfo()
for { for {
...@@ -547,35 +548,99 @@ func GetWalletStatus() (*types.WalletStatus,error) { ...@@ -547,35 +548,99 @@ func GetWalletStatus() (*types.WalletStatus,error) {
return p.GetWalletStatus() return p.GetWalletStatus()
} }
func AutoUpdate(tick *time.Ticker) { func PaiJob(tick *time.Ticker) {
for { for {
select { select {
case <-tick.C: case <-tick.C:
if RebootP {//重启pai AutoUpdate()
err := KillPai() AutoSSHClean()
if err != nil {
tlog.Error("kill pai","err",err) }
} else { }
err := StartProcess(setting.Chain33Pai.Start) }
if err != nil {
tlog.Error("restart pai fail","err",err) type PaiProcessInfo struct {
} else { Pid int32
tlog.Info("kill restart pai success ") Stime int64
} Cmd string
} }
func AutoSSHClean() error {
pai,err := getPaiProcessInfo()
tlog.Info("getPaiProcessInfo","pai",*pai)
if err != nil {
tlog.Error("getPaiProcessInfo","err",err)
return err
}
if pai.Stime > 0 && (time.Now().Unix()-pai.Stime) > 72 * 3600 && pai.Pid > 0 {
err := KillAutoSsh(pai.Pid)
if err != nil {
tlog.Error("KillAutoSsh","err",err)
}
tlog.Info("KillAutoSsh Success","pid",pai.Pid)
}
return nil
}
func KillAutoSsh(pid int32) error {
ps := fmt.Sprintf("%d",pid)
cmd := exec.Command("kill","-9",ps)
return cmd.Run()
}
func getPaiProcessInfo() (*PaiProcessInfo,error) {
var pai PaiProcessInfo
var buf bytes.Buffer
cmd := exec.Command("bash","-c","ps -eo pid,lstart,cmd |grep autossh | grep -v grep")
cmd.Stdout = &buf
err := cmd.Run()
if err != nil {
tlog.Error("getPaiProcessInfo","err",err)
return nil,err
}
list := strings.Split(buf.String()," ")
if len(list) < 7 {
return nil, errors.New("ps return exception")
}
lstart := list[1]+" "+list[2]+" "+ list[3]+" "+ list[4]+" "+list[5]
location , _ := time.LoadLocation("Local")
tt,err := time.ParseInLocation(time.ANSIC,lstart,location)
if err != nil {
tlog.Error("getPaiProcessInfo","err",err)
return nil,err
}
start := tt.Unix()
p ,_ := strconv.Atoi(list[0])
pai.Pid = int32(p)
pai.Stime = start
return &pai, nil
}
func AutoUpdate() {
if RebootP {//重启pai
err := KillPai()
if err != nil {
tlog.Error("kill pai","err",err)
} else {
err := StartProcess(setting.Chain33Pai.Start)
if err != nil {
tlog.Error("restart pai fail","err",err)
} else { } else {
if !DPercent.Flag {//没有其他任务占用 tlog.Info("kill restart pai success ")
DPercent.Flag = true
err := AutoUpdatePai()
if err != nil {
tlog.Error("autoupdatepai","err",err)
}
tlog.Info("autoupdatepai","err",GetVersion())
DPercent.Flag = false
}
} }
}
} else {
if !DPercent.Flag {//没有其他任务占用
DPercent.Flag = true
err := AutoUpdatePai()
if err != nil {
tlog.Error("autoupdatepai","err",err)
}
tlog.Info("autoupdatepai","err",GetVersion())
DPercent.Flag = false
} }
} }
} }
......
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