🐛 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

@ -4,7 +4,7 @@
<br>
<small><i>LOGO designed by <a href="https://xio.ng" target="_blank">熊大</a> .</i></small>
<br><br>
<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.9.33&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github">&nbsp;<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/badge/Installer-v0.7.0-brightgreen?style=for-the-badge&logo=linux">
<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.9.34&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github">&nbsp;<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/badge/Installer-v0.7.0-brightgreen?style=for-the-badge&logo=linux">
<br>
<br>
<p>:trollface: <b>哪吒监控</b> 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,命令批量执行和计划任务。</p>

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

View File

@ -13,7 +13,7 @@ import (
pb "github.com/naiba/nezha/proto"
)
var Version = "v0.9.33" // !!记得修改 README 中的 badge 版本!!
var Version = "v0.9.34" // !!记得修改 README 中的 badge 版本!!
var (
Conf *model.Config

View File

@ -154,12 +154,8 @@ func (ss *ServiceSentinel) loadMonitorHistory() {
panic(err)
}
ss.monitors[monitors[i].ID] = monitors[i]
if len(ss.serviceCurrentStatusData[monitors[i].ID]) == 0 {
ss.serviceCurrentStatusData[monitors[i].ID] = make([]model.MonitorHistory, _CurrentStatusSize)
}
if ss.serviceStatusToday[monitors[i].ID] == nil {
ss.serviceStatusToday[monitors[i].ID] = &_TodayStatsOfMonitor{}
}
ss.serviceCurrentStatusData[monitors[i].ID] = make([]model.MonitorHistory, _CurrentStatusSize)
ss.serviceStatusToday[monitors[i].ID] = &_TodayStatsOfMonitor{}
}
year, month, day := time.Now().Date()
@ -216,6 +212,10 @@ func (ss *ServiceSentinel) OnMonitorUpdate(m model.Monitor) error {
Up: &[30]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Down: &[30]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}
ss.serviceResponseDataStoreLock.Lock()
defer ss.serviceResponseDataStoreLock.Unlock()
ss.serviceCurrentStatusData[m.ID] = make([]model.MonitorHistory, _CurrentStatusSize)
ss.serviceStatusToday[m.ID] = &_TodayStatsOfMonitor{}
}
// 更新这个任务
ss.monitors[m.ID] = &m