feat: 解决时区获取不到导致的网站自动停止的问题 (#921)

This commit is contained in:
zhengkunwang223 2023-05-08 12:01:39 +07:00 committed by GitHub
parent c4d2593a42
commit ca1eca476c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/repo"
"github.com/1Panel-dev/1Panel/backend/app/service"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"time"
)
@ -18,13 +19,14 @@ func (ssl *ssl) Run() {
sslRepo := repo.NewISSLRepo()
sslService := service.NewIWebsiteSSLService()
sslList, _ := sslRepo.List()
nyc, _ := time.LoadLocation(common.LoadTimeZone())
global.LOG.Info("The scheduled certificate update task is currently in progress ...")
now := time.Now().Add(10 * time.Second)
for _, s := range sslList {
if !s.AutoRenew || s.Provider == "manual" || s.Provider == "dnsManual" {
continue
}
expireDate := s.ExpireDate.In(time.Now().Location())
expireDate := s.ExpireDate.In(nyc)
sub := expireDate.Sub(now)
if sub.Hours() < 720 {
global.LOG.Errorf("Update the SSL certificate for the [%s] domain", s.PrimaryDomain)

View File

@ -7,6 +7,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/service"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"sync"
"time"
)
@ -18,8 +19,9 @@ func NewWebsiteJob() *website {
}
func (w *website) Run() {
nyc, _ := time.LoadLocation(common.LoadTimeZone())
websites, _ := repo.NewIWebsiteRepo().List()
global.LOG.Info("website cron job start...")
global.LOG.Info("Website scheduled task in progress ...")
now := time.Now().Add(10 * time.Second)
if len(websites) > 0 {
neverExpireDate, _ := time.Parse(constant.DateLayout, constant.DefaultDate)
@ -28,27 +30,29 @@ func (w *website) Run() {
if site.Status != constant.WebRunning || neverExpireDate.Equal(site.ExpireDate) {
continue
}
expireDate, _ := time.ParseInLocation(constant.DateTimeLayout, site.ExpireDate.String(), time.Now().Location())
expireDate, _ := time.ParseInLocation(constant.DateTimeLayout, site.ExpireDate.String(), nyc)
if expireDate.Before(now) {
wg.Add(1)
go func(ws model.Website) {
stopWebsite(ws.ID, &wg)
stopWebsite(ws.ID, ws.PrimaryDomain, &wg)
}(site)
}
}
wg.Wait()
}
global.LOG.Info("website cron job end...")
global.LOG.Info("Website scheduled task has completed")
}
func stopWebsite(websiteId uint, wg *sync.WaitGroup) {
func stopWebsite(websiteId uint, websiteName string, wg *sync.WaitGroup) {
websiteService := service.NewIWebsiteService()
req := request.WebsiteOp{
ID: websiteId,
Operate: constant.StopWeb,
}
if err := websiteService.OpWebsite(req); err != nil {
global.LOG.Errorf("stop website err: %s", err.Error())
global.LOG.Errorf("Website [%s] seop failed err %v", websiteName, err)
} else {
global.LOG.Infof("Website [%s] stopped successfully", websiteName)
}
wg.Done()
}