From 94e0c80d476e10c1fbe625abeeae8e06f3445697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=98=AD?= <81747598+lan-yonghui@users.noreply.github.com> Date: Thu, 5 Sep 2024 16:41:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B0=83=E6=95=B4=E4=B8=BB?= =?UTF-8?q?=E6=9C=BA=E7=9B=91=E6=8E=A7=E4=BB=BB=E5=8A=A1=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E4=B8=BA=E6=95=B4=E5=88=86=E9=92=9F=E6=89=A7=E8=A1=8C=20(#6379?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/monitor.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/backend/app/service/monitor.go b/backend/app/service/monitor.go index 8f907a71f..0d73e48ef 100644 --- a/backend/app/service/monitor.go +++ b/backend/app/service/monitor.go @@ -201,16 +201,19 @@ func StartMonitor(removeBefore bool, interval string) error { service := NewIMonitorService() ctx, cancel := context.WithCancel(context.Background()) monitorCancel = cancel - monitorID, err := global.Cron.AddJob(fmt.Sprintf("@every %sm", interval), service) - if err != nil { - return err - } - + now := time.Now() + nextMinute := now.Truncate(time.Minute).Add(time.Minute) + time.AfterFunc(time.Until(nextMinute), func() { + monitorID, err := global.Cron.AddJob(fmt.Sprintf("@every %sm", interval), service) + if err != nil { + return + } + global.MonitorCronID = monitorID + }) service.Run() go service.saveIODataToDB(ctx, float64(intervalItem)) go service.saveNetDataToDB(ctx, float64(intervalItem)) - global.MonitorCronID = monitorID return nil }