Commit 492bf342 authored by szh's avatar szh

autossh 定期清理链接

parent 31b938c8
......@@ -37,10 +37,10 @@ func main() {
gin.SetMode(setting.ServerSetting.RunMode)
//区域网广播设备
go broadcast()
//定时监控节点
go app.CornProcessJob(time.NewTicker(time.Second*30))
//定时自动更新树莓派内置程序
go app.AutoUpdate(time.NewTicker(time.Second * 50))
//节点操作
go app.BityuanJob(time.NewTicker(time.Second*30))
//树莓派内置程序操作
go app.PaiJob(time.NewTicker(time.Second * 50))
//go app.ClearLog(time.NewTicker(time.Hour*1))
//上传统计树莓派信息
......
......@@ -23,6 +23,7 @@ import (
"encoding/json"
"chain33-pai/pkg/chain33"
types2 "github.com/33cn/plugin/plugin/dapp/ticket/types"
"strconv"
)
var tlog = log.New("pkg","app")
......@@ -138,7 +139,7 @@ func StrFilter(str []string,filter string)(res []string){
}
//corn job for collecting chain33 process info
func CornProcessJob(ticker *time.Ticker){
func BityuanJob(ticker *time.Ticker){
NodeError = getWalletInfo()
for {
......@@ -547,35 +548,99 @@ func GetWalletStatus() (*types.WalletStatus,error) {
return p.GetWalletStatus()
}
func AutoUpdate(tick *time.Ticker) {
func PaiJob(tick *time.Ticker) {
for {
select {
case <-tick.C:
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 {
tlog.Info("kill restart pai success ")
}
}
AutoUpdate()
AutoSSHClean()
}
}
}
type PaiProcessInfo struct {
Pid int32
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 {
if !DPercent.Flag {//没有其他任务占用
DPercent.Flag = true
err := AutoUpdatePai()
if err != nil {
tlog.Error("autoupdatepai","err",err)
}
tlog.Info("autoupdatepai","err",GetVersion())
DPercent.Flag = false
}
tlog.Info("kill restart pai success ")
}
}
} 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