From 5f31d07ff92d87dd1016e9279df84d8f3641feb5 Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期日, 25 八月 2024 22:26:29 +0800
Subject: [PATCH] 添加系统校时功能

---
 service/systime.go |   22 ++++++++++++++++++++++
 config/config.go   |   22 ++++++++++++++++++----
 cron/cron.go       |    1 +
 3 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/config/config.go b/config/config.go
index 95a0312..f4609ee 100644
--- a/config/config.go
+++ b/config/config.go
@@ -36,7 +36,7 @@
 	Enable             bool   `mapstructure:"enable"`
 }
 
-type logConfig struct {
+type log struct {
 	LogWay string `mapstructure:"log-way"` // 鏃ュ織杈撳嚭
 	Path   string `mapstructure:"path"`    // 鏃ュ織瀛樺偍璺緞
 	Level  string `mapstructure:"level"`   // 鏃ュ織绛夌骇
@@ -67,18 +67,24 @@
 	WaitRunTime int    `mapstructure:"wait-run-time"`
 }
 
-type rateLimitConfig struct {
+type rateLimit struct {
 	FillInterval int64 `mapstructure:"fill-interval" json:"fillInterval"`
 	Capacity     int64 `mapstructure:"capacity" json:"capacity"`
 }
 
+type sysTime struct {
+	NTPServer    string `mapstructure:"ntp-server" json:"ntp-server"`
+	SyncInterval int    `mapstructure:"sync-interval" json:"sync-interval"`
+}
+
 var ServeConf = &serve{}
-var LogConf = &logConfig{}
+var LogConf = &log{}
 var ForwardConf = &forward{}
-var RateLimitConf = &rateLimitConfig{}
+var RateLimitConf = &rateLimit{}
 var ClientConf = &client{}
 var NVCSConf = &nvcs{}
 var ImageConf = &image{}
+var SysTimeConf = &sysTime{}
 
 // Init is an exported method that takes the environment starts the viper
 // (external lib) and returns the configuration struct.
@@ -139,5 +145,13 @@
 		NVCSConf.WaitRunTime = 60
 	}
 
+	if SysTimeConf.SyncInterval == 0 {
+		SysTimeConf.SyncInterval = 10
+	}
+
+	if SysTimeConf.NTPServer == "" {
+		SysTimeConf.NTPServer = "ntp.aliyun.com"
+	}
+
 	logger.SetLogLevel(LogConf.Level)
 }
diff --git a/cron/cron.go b/cron/cron.go
index e71e4e4..c38eebd 100644
--- a/cron/cron.go
+++ b/cron/cron.go
@@ -17,6 +17,7 @@
 	s.Every(config.ForwardConf.RetryInterval).Minute().StartImmediately().Do(service.ResendImageData)
 	s.Every(config.ForwardConf.ReportInterval).Second().StartImmediately().Do(service.DeviceInfoReportTask)
 	s.Every(1).Hour().StartImmediately().Do(service.CleanExpireData)
+	s.Every(config.SysTimeConf.SyncInterval).Minute().StartImmediately().Do(service.SyncSystemTime)
 
 	s.StartAsync()
 }
diff --git a/service/systime.go b/service/systime.go
new file mode 100644
index 0000000..326d531
--- /dev/null
+++ b/service/systime.go
@@ -0,0 +1,22 @@
+package service
+
+import (
+	"os/exec"
+
+	"gat1400Exchange/config"
+	"gat1400Exchange/pkg/logger"
+)
+
+func SyncSystemTime() {
+	if config.SysTimeConf.NTPServer == "" {
+		return
+	}
+
+	cmd := exec.Command("/bin/sh", "sudo ntpdate "+config.SysTimeConf.NTPServer)
+	stdout, err := cmd.Output()
+	if err != nil {
+		logger.Error("Fail to sync system time: %s", err.Error())
+	} else {
+		logger.Info("Sync system time %s", string(stdout))
+	}
+}

--
Gitblit v1.8.0