From ed8dc18caa1c11c23367dbd7fba9a056896a5711 Mon Sep 17 00:00:00 2001 From: naiba Date: Sat, 23 Jan 2021 15:32:04 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20[dashboard=20v0.3.7]=20cron:=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B=E8=AE=A1=E5=88=92=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=95=B0=E6=8D=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- cmd/dashboard/controller/member_api.go | 19 ++++++++++++------- service/dao/dao.go | 2 +- service/rpc/nezha.go | 22 ++++++++++++---------- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index df36fe5..1d4f223 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 哪吒面板 -![dashboard](https://img.shields.io/badge/管理面板-v0.3.6-brightgreen?style=for-the-badge&logo=github) ![Agent release](https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github) +![dashboard](https://img.shields.io/badge/管理面板-v0.3.7-brightgreen?style=for-the-badge&logo=github) ![Agent release](https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github) 系统状态监控报警、API(SSL证书变更、即将到期、到期)/TCP端口存活/PING 监控、计划任务(可以定时在Agent上执行命令,备份、重启、What ever you want)、极省资源,64M 服务器也能装 agent。 diff --git a/cmd/dashboard/controller/member_api.go b/cmd/dashboard/controller/member_api.go index 9edd26b..855d3c8 100644 --- a/cmd/dashboard/controller/member_api.go +++ b/cmd/dashboard/controller/member_api.go @@ -75,13 +75,13 @@ func (ma *memberAPI) delete(c *gin.Context) { err = dao.DB.Delete(&model.MonitorHistory{}, "monitor_id = ?", id).Error } case "cron": - err = dao.DB.Delete(&model.Cron{}, "id = ?", id).Error if err == nil { dao.CronLock.RLock() defer dao.CronLock.RUnlock() - if dao.Crons[id].CronID != 0 { - dao.Cron.Remove(dao.Crons[id].CronID) + cr := dao.Crons[id] + if cr != nil && cr.CronID != 0 { + dao.Cron.Remove(cr.CronID) } delete(dao.Crons, id) } @@ -264,13 +264,15 @@ func (ma *memberAPI) addOrEditCron(c *gin.Context) { return } - if cr.CronID != 0 { - dao.Cron.Remove(cr.CronID) + dao.ServerLock.Lock() + defer dao.ServerLock.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, func() { - dao.ServerLock.RLock() - defer dao.ServerLock.RUnlock() for j := 0; j < len(cr.Servers); j++ { if dao.ServerList[cr.Servers[j]].TaskStream != nil { dao.ServerList[cr.Servers[j]].TaskStream.Send(&pb.Task{ @@ -284,6 +286,9 @@ func (ma *memberAPI) addOrEditCron(c *gin.Context) { } }) + delete(dao.Crons, cr.ID) + dao.Crons[cr.ID] = &cr + c.JSON(http.StatusOK, model.Response{ Code: http.StatusOK, }) diff --git a/service/dao/dao.go b/service/dao/dao.go index 97546fa..947a852 100644 --- a/service/dao/dao.go +++ b/service/dao/dao.go @@ -34,7 +34,7 @@ var CronLock sync.RWMutex var Crons map[uint64]*model.Cron var Cron *cron.Cron -var Version = "v0.3.6" +var Version = "v0.3.7" func ReSortServer() { ServerLock.RLock() diff --git a/service/rpc/nezha.go b/service/rpc/nezha.go index 4fcc2ef..071310d 100644 --- a/service/rpc/nezha.go +++ b/service/rpc/nezha.go @@ -58,18 +58,20 @@ func (s *NezhaHandler) ReportTask(c context.Context, r *pb.TaskResult) (*pb.Rece if r.GetType() == model.TaskTypeCommand { // 处理上报的计划任务 dao.CronLock.RLock() + defer dao.CronLock.RUnlock() cr := dao.Crons[r.GetId()] - dao.CronLock.RUnlock() - if cr.PushSuccessful && r.GetSuccessful() { - alertmanager.SendNotification(fmt.Sprintf("成功计划任务:%s ,服务器:%d,日志:\n%s", cr.Name, clientID, r.GetData())) + if cr != nil { + if cr.PushSuccessful && r.GetSuccessful() { + alertmanager.SendNotification(fmt.Sprintf("成功计划任务:%s ,服务器:%d,日志:\n%s", cr.Name, clientID, r.GetData())) + } + if !r.GetSuccessful() { + alertmanager.SendNotification(fmt.Sprintf("失败计划任务:%s ,服务器:%d,日志:\n%s", cr.Name, clientID, r.GetData())) + } + dao.DB.Model(cr).Updates(model.Cron{ + LastExecutedAt: time.Now().Add(time.Second * -1 * time.Duration(r.GetDelay())), + LastResult: r.GetSuccessful(), + }) } - if !r.GetSuccessful() { - alertmanager.SendNotification(fmt.Sprintf("失败计划任务:%s ,服务器:%d,日志:\n%s", cr.Name, clientID, r.GetData())) - } - dao.DB.Model(cr).Updates(model.Cron{ - LastExecutedAt: time.Now().Add(time.Second * -1 * time.Duration(r.GetDelay())), - LastResult: r.GetSuccessful(), - }) } else { // 存入历史记录 mh := model.PB2MonitorHistory(r)