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

View File

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

View File

@ -9,7 +9,7 @@
<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="/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" />
</head>

View File

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

View File

@ -227,11 +227,6 @@
time: '2',
position: 'top-center',
});
ws.send('track');
setInterval(() => {
ws.send('track');
console.log('追加监控时间')
}, 1000 * 60 * 6);
}
ws.onmessage = function (evt) {
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 {
return err
}
// 默认连接后先上报10分钟
err = stream.Send(&pb.Command{
Type: model.MTReportState,
})
if err != nil {
return err
}
// 放入在线服务器列表
dao.ServerLock.RLock()
closeCh := make(chan error)