feat: 增加同步任务 停止过期网站

This commit is contained in:
zhengkunwang223 2022-12-30 15:23:29 +08:00 committed by zhengkunwang223
parent 272eab867f
commit f27b173f63
4 changed files with 73 additions and 4 deletions

View File

@ -14,6 +14,7 @@ type IWebsiteRepo interface {
WithAlias(alias string) DBOption
WithWebsiteSSLID(sslId uint) DBOption
Page(page, size int, opts ...DBOption) (int64, []model.Website, error)
List(opts ...DBOption) ([]model.Website, error)
GetFirst(opts ...DBOption) (model.Website, error)
GetBy(opts ...DBOption) ([]model.Website, error)
Save(ctx context.Context, app *model.Website) error
@ -61,6 +62,12 @@ func (w *WebsiteRepo) Page(page, size int, opts ...DBOption) (int64, []model.Web
return count, websites, err
}
func (w *WebsiteRepo) List(opts ...DBOption) ([]model.Website, error) {
var websites []model.Website
err := getDb(opts...).Model(&model.Website{}).Preload("WebsiteSSL").Find(&websites).Error
return websites, err
}
func (w *WebsiteRepo) GetFirst(opts ...DBOption) (model.Website, error) {
var website model.Website
db := getDb(opts...).Model(&model.Website{})

View File

@ -10,6 +10,7 @@ import (
"path"
"strconv"
"strings"
"time"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/app/model"
@ -627,6 +628,11 @@ func opWebsite(website *model.Website, operate string) error {
server.UpdateRootProxy([]string{website.Proxy})
}
website.Status = constant.WebRunning
now := time.Now()
if website.ExpireDate.Before(now) {
defaultDate, _ := time.Parse(constant.DateLayout, constant.DefaultDate)
website.ExpireDate = defaultDate
}
}
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {

View File

@ -16,14 +16,18 @@ func Run() {
Cron := cron.New(cron.WithLocation(nyc), cron.WithChain(cron.Recover(cron.DefaultLogger)), cron.WithChain(cron.DelayIfStillRunning(cron.DefaultLogger)))
_, err := Cron.AddJob("@every 1m", job.NewMonitorJob())
if err != nil {
global.LOG.Errorf("can not add corn job: %s", err.Error())
global.LOG.Errorf("can not add monitor corn job: %s", err.Error())
}
_, err = Cron.AddJob("@daily", job.NewWebsiteJob())
if err != nil {
global.LOG.Errorf("can not add website corn job: %s", err.Error())
}
Cron.Start()
global.Cron = Cron
var Cronjobs []model.Cronjob
if err := global.DB.Where("status = ?", constant.StatusEnable).Find(&Cronjobs).Error; err != nil {
var cronJobs []model.Cronjob
if err := global.DB.Where("status = ?", constant.StatusEnable).Find(&cronJobs).Error; err != nil {
global.LOG.Errorf("start my cronjob failed, err: %v", err)
}
if err := global.DB.Model(&model.JobRecords{}).
@ -35,7 +39,7 @@ func Run() {
}).Error; err != nil {
global.LOG.Errorf("start my cronjob failed, err: %v", err)
}
for _, cronjob := range Cronjobs {
for _, cronjob := range cronJobs {
if err := service.ServiceGroupApp.StartJob(&cronjob); err != nil {
global.LOG.Errorf("start %s job %s failed, err: %v", cronjob.Type, cronjob.Name, err)
}

View File

@ -0,0 +1,52 @@
package job
import (
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
"github.com/1Panel-dev/1Panel/backend/app/repo"
"github.com/1Panel-dev/1Panel/backend/app/service"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"sync"
"time"
)
type website struct{}
func NewWebsiteJob() *website {
return &website{}
}
func (w *website) Run() {
websites, _ := repo.NewIWebsiteRepo().List()
global.LOG.Info("website cron job start....")
now := time.Now()
if len(websites) > 0 {
neverExpireDate, _ := time.Parse(constant.DateLayout, constant.DefaultDate)
var wg sync.WaitGroup
for _, site := range websites {
if site.Status != constant.WebRunning || neverExpireDate.Equal(site.ExpireDate) {
continue
}
if site.ExpireDate.Before(now) {
wg.Add(1)
go func() {
stopWebsite(site.ID, &wg)
}()
}
}
wg.Wait()
}
global.LOG.Info("website cron job end")
}
func stopWebsite(websiteId uint, wg *sync.WaitGroup) {
websiteService := service.NewWebsiteService()
req := request.WebsiteOp{
ID: websiteId,
Operate: constant.StopWeb,
}
if err := websiteService.OpWebsite(req); err != nil {
global.LOG.Errorf("stop website err: %s", err.Error())
}
wg.Done()
}