feat: 服务启动时自动同步系统时间 (#2775)

Refs #2747
This commit is contained in:
ssongliu 2023-11-02 18:22:41 +08:00 committed by GitHub
parent 47b75673f9
commit f14b8641d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)
}