From f14b8641d3791764b451609145f21b75c42f18e6 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Thu, 2 Nov 2023 18:22:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9C=8D=E5=8A=A1=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=97=B6=E8=87=AA=E5=8A=A8=E5=90=8C=E6=AD=A5=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E6=97=B6=E9=97=B4=20(#2775)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs #2747 --- backend/cron/cron.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/backend/cron/cron.go b/backend/cron/cron.go index 448955f53..bbf4d712e 100644 --- a/backend/cron/cron.go +++ b/backend/cron/cron.go @@ -10,6 +10,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/cron/job" "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/utils/common" + "github.com/1Panel-dev/1Panel/backend/utils/ntp" "github.com/robfig/cron/v3" ) @@ -21,6 +22,7 @@ func Run() { interval model.Setting status model.Setting ) + syncBeforeStart() if err := global.DB.Where("key = ?", "MonitorStatus").Find(&status).Error; err != nil { global.LOG.Errorf("load monitor status from db failed, err: %v", err) } @@ -67,3 +69,23 @@ func Run() { } } } + +func syncBeforeStart() { + var ntpSite model.Setting + if err := global.DB.Where("key = ?", "NtpSite").Find(&ntpSite).Error; err != nil { + global.LOG.Errorf("load ntp serve from db failed, err: %v", err) + } + if len(ntpSite.Value) == 0 { + ntpSite.Value = "pool.ntp.org" + } + ntime, err := ntp.GetRemoteTime(ntpSite.Value) + if err != nil { + global.LOG.Errorf("load remote time with [%s] failed, err: %v", ntpSite.Value, err) + return + } + ts := ntime.Format("2006-01-02 15:04:05") + if err := ntp.UpdateSystemTime(ts); err != nil { + global.LOG.Errorf("failed to synchronize system time with [%s], err: %v", ntpSite.Value, err) + } + global.LOG.Debugf("synchronize system time with [%s] successful!", ntpSite.Value) +}