🐛 dashboard: 修复服务监控任务调度

This commit is contained in:
naiba
2021-09-03 08:52:45 +08:00
parent b54d055c37
commit 20ea31140b
5 changed files with 13 additions and 16 deletions

View File

@@ -17,11 +17,7 @@ import (
"github.com/naiba/nezha/service/dao"
)
var serviceSentinelDispatchBus chan model.Monitor
func init() {
serviceSentinelDispatchBus = make(chan model.Monitor)
shanghai, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
panic(err)
@@ -59,7 +55,6 @@ func initSystem() {
dao.DB.AutoMigrate(model.Server{}, model.User{},
model.Notification{}, model.AlertRule{}, model.Monitor{},
model.MonitorHistory{}, model.Cron{}, model.Transfer{})
dao.NewServiceSentinel(serviceSentinelDispatchBus)
loadServers() //加载服务器列表
loadCrons() //加载计划任务
@@ -178,8 +173,10 @@ func loadCrons() {
func main() {
cleanMonitorHistory()
go rpc.ServeRPC(dao.Conf.GRPCPort)
serviceSentinelDispatchBus := make(chan model.Monitor)
go rpc.DispatchTask(serviceSentinelDispatchBus)
go dao.AlertSentinelStart()
dao.NewServiceSentinel(serviceSentinelDispatchBus)
srv := controller.ServeWeb(dao.Conf.HTTPPort)
graceful.Graceful(func() error {
return srv.ListenAndServe()

View File

@@ -28,10 +28,10 @@ func DispatchTask(serviceSentinelDispatchBus <-chan model.Monitor) {
workedServerIndex := 0
for task := range serviceSentinelDispatchBus {
round := 0
prevIndex := workedServerIndex
endIndex := workedServerIndex
dao.SortedServerLock.RLock()
// 如果已经轮了一整圈没有合适机器去请求,跳出循环
for round == 0 && prevIndex != workedServerIndex {
// 如果已经轮了一整圈又轮到自己,没有合适机器去请求,跳出循环
for round < 1 || workedServerIndex < endIndex {
// 如果到了圈尾,再回到圈头,圈数加一,游标重置
if workedServerIndex == len(dao.SortedServerList) {
workedServerIndex = 0