feat: 添加时间同步服务和北京时间支持

- 在主程序中集成时间同步服务,每30分钟同步一次时间。
- 在心跳报告中加载并使用北京时间,确保心跳在每分钟的第1秒发送。
- 增强了错误处理,确保在加载时区失败时使用默认时区。
This commit is contained in:
2025-12-24 02:34:05 +08:00
parent bb73e0f384
commit e0d97c4486
3 changed files with 236 additions and 10 deletions

View File

@@ -13,6 +13,7 @@ import (
"linkmaster-node/internal/heartbeat"
"linkmaster-node/internal/recovery"
"linkmaster-node/internal/server"
"linkmaster-node/internal/timesync"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
@@ -54,6 +55,17 @@ func main() {
}
}
// 启动时间同步服务每30分钟同步一次
var timeSync *timesync.TimeSync
timeSync, err = timesync.NewTimeSync(logger)
if err != nil {
logger.Warn("创建时间同步器失败", zap.Error(err))
timeSync = nil
} else {
go timeSync.Start(context.Background(), 30*time.Minute)
logger.Info("时间同步服务已启动")
}
// 启动心跳上报
heartbeatReporter := heartbeat.NewReporter(cfg)
go heartbeatReporter.Start(context.Background())
@@ -79,6 +91,9 @@ func main() {
httpServer.Shutdown(ctx)
heartbeatReporter.Stop()
if timeSync != nil {
timeSync.Stop()
}
logger.Info("服务已关闭")
}