71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
|
|
package nodes
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/reporterconfigs"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
teaconst "github.com/TeaOSLab/EdgeReporter/internal/const"
|
|
"github.com/TeaOSLab/EdgeReporter/internal/remotelogs"
|
|
"github.com/TeaOSLab/EdgeReporter/internal/rpc"
|
|
"github.com/TeaOSLab/EdgeReporter/internal/utils"
|
|
"os/user"
|
|
"runtime"
|
|
"time"
|
|
)
|
|
|
|
var sharedNodeStatus = &reporterconfigs.Status{}
|
|
|
|
type StatusManager struct {
|
|
}
|
|
|
|
func NewStatusManager() *StatusManager {
|
|
return &StatusManager{}
|
|
}
|
|
|
|
func (this *StatusManager) Start() {
|
|
var ticker = time.NewTicker(30 * time.Second)
|
|
|
|
// 先执行一次
|
|
err := this.Loop()
|
|
if err != nil {
|
|
remotelogs.Error("STATUS_MANAGER", err.Error())
|
|
}
|
|
|
|
for range ticker.C {
|
|
err := this.Loop()
|
|
if err != nil {
|
|
remotelogs.Error("STATUS_MANAGER", err.Error())
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *StatusManager) Loop() error {
|
|
sharedNodeStatus.BuildVersion = teaconst.Version
|
|
sharedNodeStatus.BuildVersionCode = utils.VersionToLong(teaconst.Version)
|
|
sharedNodeStatus.OS = runtime.GOOS
|
|
sharedNodeStatus.OSName = runtime.GOOS // TODO 需要实现
|
|
|
|
u, err := user.Current()
|
|
if err == nil && u != nil {
|
|
sharedNodeStatus.Username = u.Name
|
|
}
|
|
|
|
rpcClient, err := rpc.SharedRPC()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
statusJSON, err := json.Marshal(sharedNodeStatus)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = rpcClient.ReportNodeRPC().UpdateReportNodeStatus(rpcClient.Context(), &pb.UpdateReportNodeStatusRequest{StatusJSON: statusJSON})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|