From c679631440543d93c76d03bcbe6c7122e07946d4 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Thu, 4 May 2023 12:00:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=97=B6=E5=8C=BA=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=20(#855)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/cronjob.go | 6 ++++-- backend/app/api/v1/monitor.go | 6 ++++-- backend/cron/cron.go | 12 +++++------- backend/middleware/password_expired.go | 3 ++- backend/utils/common/common.go | 9 +++++++++ 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/backend/app/api/v1/cronjob.go b/backend/app/api/v1/cronjob.go index 433654d50..4ef0081cf 100644 --- a/backend/app/api/v1/cronjob.go +++ b/backend/app/api/v1/cronjob.go @@ -7,6 +7,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/global" + "github.com/1Panel-dev/1Panel/backend/utils/common" "github.com/gin-gonic/gin" ) @@ -77,8 +78,9 @@ func (b *BaseApi) SearchJobRecords(c *gin.Context) { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return } - req.StartTime = req.StartTime.Add(8 * time.Hour) - req.EndTime = req.EndTime.Add(8 * time.Hour) + loc, _ := time.LoadLocation(common.LoadTimeZone()) + req.StartTime = req.StartTime.In(loc) + req.EndTime = req.EndTime.In(loc) total, list, err := cronjobService.SearchRecords(req) if err != nil { diff --git a/backend/app/api/v1/monitor.go b/backend/app/api/v1/monitor.go index b1d12e11c..5173f121d 100644 --- a/backend/app/api/v1/monitor.go +++ b/backend/app/api/v1/monitor.go @@ -9,6 +9,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/global" + "github.com/1Panel-dev/1Panel/backend/utils/common" "github.com/gin-gonic/gin" "github.com/shirou/gopsutil/v3/disk" "github.com/shirou/gopsutil/v3/net" @@ -24,8 +25,9 @@ func (b *BaseApi) LoadMonitor(c *gin.Context) { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return } - req.StartTime = req.StartTime.Add(8 * time.Hour) - req.EndTime = req.EndTime.Add(8 * time.Hour) + loc, _ := time.LoadLocation(common.LoadTimeZone()) + req.StartTime = req.StartTime.In(loc) + req.EndTime = req.EndTime.In(loc) var backdatas []dto.MonitorData if req.Param == "all" || req.Param == "cpu" || req.Param == "memory" || req.Param == "load" { diff --git a/backend/cron/cron.go b/backend/cron/cron.go index 0fe21b1f2..19c6c7ed9 100644 --- a/backend/cron/cron.go +++ b/backend/cron/cron.go @@ -9,22 +9,20 @@ import ( "github.com/1Panel-dev/1Panel/backend/constant" "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/robfig/cron/v3" ) func Run() { - nyc, _ := time.LoadLocation("Asia/Shanghai") + nyc, _ := time.LoadLocation(common.LoadTimeZone()) Cron := cron.New(cron.WithLocation(nyc), cron.WithChain(cron.Recover(cron.DefaultLogger)), cron.WithChain(cron.DelayIfStillRunning(cron.DefaultLogger))) - _, err := Cron.AddJob("@every 5m", job.NewMonitorJob()) - if err != nil { + if _, err := Cron.AddJob("@every 5m", job.NewMonitorJob()); err != nil { global.LOG.Errorf("can not add monitor corn job: %s", err.Error()) } - _, err = Cron.AddJob("@daily", job.NewWebsiteJob()) - if err != nil { + if _, err := Cron.AddJob("@daily", job.NewWebsiteJob()); err != nil { global.LOG.Errorf("can not add website corn job: %s", err.Error()) } - _, err = Cron.AddJob("@daily", job.NewSSLJob()) - if err != nil { + if _, err := Cron.AddJob("@daily", job.NewSSLJob()); err != nil { global.LOG.Errorf("can not add ssl corn job: %s", err.Error()) } Cron.Start() diff --git a/backend/middleware/password_expired.go b/backend/middleware/password_expired.go index 07c61bf48..e1ed9e9fa 100644 --- a/backend/middleware/password_expired.go +++ b/backend/middleware/password_expired.go @@ -7,6 +7,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" "github.com/1Panel-dev/1Panel/backend/app/repo" "github.com/1Panel-dev/1Panel/backend/constant" + "github.com/1Panel-dev/1Panel/backend/utils/common" "github.com/gin-gonic/gin" ) @@ -29,7 +30,7 @@ func PasswordExpired() gin.HandlerFunc { helper.ErrorWithDetail(c, constant.CodePasswordExpired, constant.ErrTypePasswordExpired, err) return } - loc, _ := time.LoadLocation("Asia/Shanghai") + loc, _ := time.LoadLocation(common.LoadTimeZone()) expiredTime, err := time.ParseInLocation("2006-01-02 15:04:05", extime.Value, loc) if err != nil { helper.ErrorWithDetail(c, constant.CodePasswordExpired, constant.ErrTypePasswordExpired, err) diff --git a/backend/utils/common/common.go b/backend/utils/common/common.go index 312fd6c10..c32c7ce2c 100644 --- a/backend/utils/common/common.go +++ b/backend/utils/common/common.go @@ -11,6 +11,7 @@ import ( "sort" "strconv" "strings" + "time" ) func CompareVersion(version1 string, version2 string) bool { @@ -134,3 +135,11 @@ func LoadSizeUnit(value float64) string { } return fmt.Sprintf("%v", value) } + +func LoadTimeZone() string { + loc := time.Now().Location() + if _, err := time.LoadLocation(loc.String()); err != nil { + return "Asia/Shanghai" + } + return loc.String() +}