up(agent): 不间断上报信息为报警作准备

This commit is contained in:
naiba 2020-12-19 23:27:59 +08:00
parent fcbd564bff
commit ad4fedaa9b
7 changed files with 20 additions and 65 deletions

View File

@ -45,7 +45,7 @@
### 通知 ### 通知
正在开发,进度 0%
## 常见问题 ## 常见问题

View File

@ -40,7 +40,6 @@ var (
) )
var ( var (
endReport time.Time
reporting bool reporting bool
client pb.NezhaServiceClient client pb.NezhaServiceClient
ctx = context.Background() ctx = context.Background()
@ -97,13 +96,14 @@ func run(cmd *cobra.Command, args []string) {
// 上报服务器信息 // 上报服务器信息
go reportState() go reportState()
go func() { if version != "" {
for range updateCh { go func() {
go doSelfUpdate() for range updateCh {
} go doSelfUpdate()
}() }
}()
updateCh <- struct{}{} updateCh <- struct{}{}
}
var err error var err error
var conn *grpc.ClientConn var conn *grpc.ClientConn
@ -161,8 +161,6 @@ func receiveCommand(hc pb.NezhaService_HeartbeatClient) error {
return err return err
} }
switch action.GetType() { switch action.GetType() {
case model.MTReportState:
endReport = time.Now().Add(time.Minute * 10)
default: default:
log.Printf("Unknown action: %v", action) log.Printf("Unknown action: %v", action)
} }
@ -171,17 +169,15 @@ func receiveCommand(hc pb.NezhaService_HeartbeatClient) error {
func reportState() { func reportState() {
var err error var err error
defer log.Printf("reportState exit %v %v => %v", endReport, time.Now(), err) defer log.Printf("reportState exit %v => %v", time.Now(), err)
for { for {
if endReport.After(time.Now()) { if client != nil {
monitor.TrackNetworkSpeed() monitor.TrackNetworkSpeed()
_, err = client.ReportState(ctx, monitor.GetState(2).PB()) _, err = client.ReportState(ctx, monitor.GetState(2).PB())
if err != nil { if err != nil {
log.Printf("reportState error %v", err) log.Printf("reportState error %v", err)
time.Sleep(delayWhenError) time.Sleep(delayWhenError)
} }
} else {
time.Sleep(time.Second * 1)
} }
} }
} }

View File

@ -2,7 +2,6 @@ package controller
import ( import (
"net/http" "net/http"
"sync"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -10,7 +9,6 @@ import (
"github.com/naiba/nezha/model" "github.com/naiba/nezha/model"
"github.com/naiba/nezha/pkg/mygin" "github.com/naiba/nezha/pkg/mygin"
pb "github.com/naiba/nezha/proto"
"github.com/naiba/nezha/service/dao" "github.com/naiba/nezha/service/dao"
) )
@ -54,35 +52,13 @@ func (cp *commonPage) ws(c *gin.Context) {
return return
} }
defer conn.Close() defer conn.Close()
var wg sync.WaitGroup for {
wg.Add(2) dao.ServerLock.RLock()
go func() { err = conn.WriteJSON(dao.ServerList)
var mt int dao.ServerLock.RUnlock()
var message []byte if err != nil {
for { break
mt, message, err = conn.ReadMessage()
if err != nil {
wg.Done()
break
}
if mt == websocket.TextMessage && string(message) == "track" {
dao.SendCommand(&pb.Command{
Type: model.MTReportState,
})
}
} }
}() time.Sleep(time.Second * 2)
go func() { }
for {
dao.ServerLock.RLock()
err = conn.WriteJSON(dao.ServerList)
dao.ServerLock.RUnlock()
if err != nil {
wg.Done()
break
}
time.Sleep(time.Second * 2)
}
}()
wg.Wait()
} }

View File

@ -9,7 +9,7 @@
<title>{{.Title}}</title> <title>{{.Title}}</title>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.1/dist/semantic.min.css"> <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.1/dist/semantic.min.css">
<link rel="stylesheet" type="text/css" href="/static/semantic-ui-alerts.min.css"> <link rel="stylesheet" type="text/css" href="/static/semantic-ui-alerts.min.css">
<link rel="stylesheet" type="text/css" href="/static/main.css?v202012091024"> <link rel="stylesheet" type="text/css" href="/static/main.css?v202012192326">
<link rel="shortcut icon" type="image/png" href="/static/logo.png" /> <link rel="shortcut icon" type="image/png" href="/static/logo.png" />
</head> </head>

View File

@ -164,11 +164,6 @@
time: '2', time: '2',
position: 'top-center', position: 'top-center',
}); });
ws.send('track');
setInterval(() => {
ws.send('track');
console.log('追加监控时间')
}, 1000 * 60 * 6);
} }
ws.onmessage = function (evt) { ws.onmessage = function (evt) {
const oldServers = statusCards.servers const oldServers = statusCards.servers

View File

@ -227,11 +227,6 @@
time: '2', time: '2',
position: 'top-center', position: 'top-center',
}); });
ws.send('track');
setInterval(() => {
ws.send('track');
console.log('追加监控时间')
}, 1000 * 60 * 6);
} }
ws.onmessage = function (evt) { ws.onmessage = function (evt) {
statusCards.servers = JSON.parse(evt.data) statusCards.servers = JSON.parse(evt.data)

View File

@ -38,13 +38,6 @@ func (s *NezhaHandler) Heartbeat(r *pb.Beat, stream pb.NezhaService_HeartbeatSer
if clientID, err = s.Auth.Check(stream.Context()); err != nil { if clientID, err = s.Auth.Check(stream.Context()); err != nil {
return err return err
} }
// 默认连接后先上报10分钟
err = stream.Send(&pb.Command{
Type: model.MTReportState,
})
if err != nil {
return err
}
// 放入在线服务器列表 // 放入在线服务器列表
dao.ServerLock.RLock() dao.ServerLock.RLock()
closeCh := make(chan error) closeCh := make(chan error)