diff --git a/README.md b/README.md index b962e8e..3e6502d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@
:trollface: 哪吒监控 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,命令批量执行和计划任务。
diff --git a/cmd/dashboard/controller/member_api.go b/cmd/dashboard/controller/member_api.go index e0be4f9..c8d7fe7 100644 --- a/cmd/dashboard/controller/member_api.go +++ b/cmd/dashboard/controller/member_api.go @@ -277,16 +277,20 @@ func (ma *memberAPI) addOrEditCron(c *gin.Context) { return } + cr.CronID, err = dao.Cron.AddFunc(cr.Scheduler, dao.CronTrigger(cr)) + if err != nil { + c.JSON(http.StatusOK, model.Response{ + Code: http.StatusBadRequest, + Message: err.Error(), + }) + return + } + dao.CronLock.Lock() defer dao.CronLock.Unlock() crOld := dao.Crons[cr.ID] if crOld != nil && crOld.CronID != 0 { - dao.Cron.Remove(crOld.CronID) - } - - cr.CronID, err = dao.Cron.AddFunc(cr.Scheduler, dao.CronTrigger(cr)) - if err != nil { - panic(err) + dao.Cron.Remove(cron.EntryID(crOld.ID)) } delete(dao.Crons, cr.ID) diff --git a/cmd/dashboard/main.go b/cmd/dashboard/main.go index 7328fa8..12e33c6 100644 --- a/cmd/dashboard/main.go +++ b/cmd/dashboard/main.go @@ -61,9 +61,15 @@ func initSystem() { loadCrons() //加载计划任务 // 清理 服务请求记录 和 流量记录 的旧数据 - dao.Cron.AddFunc("0 20 3 * * *", cleanMonitorHistory) + _, err := dao.Cron.AddFunc("30 3 * * *", cleanMonitorHistory) + if err != nil { + panic(err) + } // 流量记录打点 - dao.Cron.AddFunc("0 0 * * * *", recordTransferHourlyUsage) + _, err = dao.Cron.AddFunc("0 * * * *", recordTransferHourlyUsage) + if err != nil { + panic(err) + } } func recordTransferHourlyUsage() { diff --git a/service/dao/dao.go b/service/dao/dao.go index bf29ea7..fffd151 100644 --- a/service/dao/dao.go +++ b/service/dao/dao.go @@ -13,7 +13,7 @@ import ( pb "github.com/naiba/nezha/proto" ) -var Version = "v0.9.5" // !!记得修改 README 中的 badge 版本!! +var Version = "v0.9.6" // !!记得修改 README 中的 badge 版本!! var ( Conf *model.Config diff --git a/service/dao/servicesentinel.go b/service/dao/servicesentinel.go index 6f09a47..e782e77 100644 --- a/service/dao/servicesentinel.go +++ b/service/dao/servicesentinel.go @@ -71,7 +71,10 @@ func NewServiceSentinel() { go ServiceSentinelShared.worker() // 每日将游标往后推一天 - Cron.AddFunc("0 0 * * * *", ServiceSentinelShared.refreshMonthlyServiceStatus) + _, err := Cron.AddFunc("* * * * *", ServiceSentinelShared.refreshMonthlyServiceStatus) + if err != nil { + panic(err) + } } /* @@ -92,13 +95,13 @@ type ServiceSentinel struct { monitors map[uint64]model.Monitor sslCertCache map[uint64]string // 30天数据缓存 - monthlyStatusLock sync.RWMutex + monthlyStatusLock sync.Mutex monthlyStatus map[uint64]*model.ServiceItemResponse } func (ss *ServiceSentinel) refreshMonthlyServiceStatus() { ss.monthlyStatusLock.Lock() - defer ss.monitorsLock.Unlock() + defer ss.monthlyStatusLock.Unlock() // 将数据往前搦 for _, v := range ss.monthlyStatus { for i := 0; i < len(v.Up)-2; i++ { @@ -195,16 +198,9 @@ func (ss *ServiceSentinel) LoadStats() map[uint64]*model.ServiceItemResponse { // 刷新最新一天的数据 ss.serviceResponseDataStoreLock.RLock() defer ss.serviceResponseDataStoreLock.RUnlock() - ss.monthlyStatusLock.RLock() - defer ss.monthlyStatusLock.RUnlock() + ss.monthlyStatusLock.Lock() + defer ss.monthlyStatusLock.Unlock() for k := range ss.monitors { - if ss.monthlyStatus[k] == nil { - ss.monthlyStatus[k] = &model.ServiceItemResponse{ - Up: new([30]int), - Down: new([30]int), - Delay: new([30]float32), - } - } ss.monthlyStatus[k].Monitor = ss.monitors[k] v := ss.serviceStatusToday[k] ss.monthlyStatus[k].Up[29] = v.Up