2022-09-08 03:39:14 +00:00
|
|
|
package cron
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2023-03-14 02:19:29 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/service"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/cron/job"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-05-04 04:00:37 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
2023-11-02 10:22:41 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/ntp"
|
2022-09-08 03:39:14 +00:00
|
|
|
"github.com/robfig/cron/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Run() {
|
2023-05-04 04:00:37 +00:00
|
|
|
nyc, _ := time.LoadLocation(common.LoadTimeZone())
|
2023-05-25 10:02:17 +00:00
|
|
|
global.Cron = cron.New(cron.WithLocation(nyc), cron.WithChain(cron.Recover(cron.DefaultLogger)), cron.WithChain(cron.DelayIfStillRunning(cron.DefaultLogger)))
|
|
|
|
|
|
|
|
var (
|
|
|
|
interval model.Setting
|
|
|
|
status model.Setting
|
|
|
|
)
|
2023-11-02 10:22:41 +00:00
|
|
|
syncBeforeStart()
|
2023-05-25 10:02:17 +00:00
|
|
|
if err := global.DB.Where("key = ?", "MonitorStatus").Find(&status).Error; err != nil {
|
|
|
|
global.LOG.Errorf("load monitor status from db failed, err: %v", err)
|
|
|
|
}
|
|
|
|
if status.Value == "enable" {
|
|
|
|
if err := global.DB.Where("key = ?", "MonitorInterval").Find(&interval).Error; err != nil {
|
|
|
|
global.LOG.Errorf("load monitor interval from db failed, err: %v", err)
|
|
|
|
}
|
|
|
|
if err := service.StartMonitor(false, interval.Value); err != nil {
|
|
|
|
global.LOG.Errorf("can not add monitor corn job: %s", err.Error())
|
|
|
|
}
|
2022-12-30 07:23:29 +00:00
|
|
|
}
|
2023-05-25 10:02:17 +00:00
|
|
|
|
|
|
|
if _, err := global.Cron.AddJob("@daily", job.NewWebsiteJob()); err != nil {
|
2022-12-30 07:23:29 +00:00
|
|
|
global.LOG.Errorf("can not add website corn job: %s", err.Error())
|
2022-09-08 03:39:14 +00:00
|
|
|
}
|
2023-05-25 10:02:17 +00:00
|
|
|
if _, err := global.Cron.AddJob("@daily", job.NewSSLJob()); err != nil {
|
2023-01-04 03:44:43 +00:00
|
|
|
global.LOG.Errorf("can not add ssl corn job: %s", err.Error())
|
|
|
|
}
|
2023-06-02 06:23:24 +00:00
|
|
|
if _, err := global.Cron.AddJob("@daily", job.NewAppStoreJob()); err != nil {
|
|
|
|
global.LOG.Errorf("can not add appstore corn job: %s", err.Error())
|
|
|
|
}
|
2023-05-25 10:02:17 +00:00
|
|
|
global.Cron.Start()
|
2022-09-27 09:26:01 +00:00
|
|
|
|
2022-12-30 07:23:29 +00:00
|
|
|
var cronJobs []model.Cronjob
|
|
|
|
if err := global.DB.Where("status = ?", constant.StatusEnable).Find(&cronJobs).Error; err != nil {
|
2022-09-27 09:26:01 +00:00
|
|
|
global.LOG.Errorf("start my cronjob failed, err: %v", err)
|
|
|
|
}
|
2022-09-28 10:11:36 +00:00
|
|
|
if err := global.DB.Model(&model.JobRecords{}).
|
|
|
|
Where("status = ?", constant.StatusRunning).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"status": constant.StatusFailed,
|
|
|
|
"message": "Task Cancel",
|
|
|
|
"records": "errHandle",
|
|
|
|
}).Error; err != nil {
|
|
|
|
global.LOG.Errorf("start my cronjob failed, err: %v", err)
|
|
|
|
}
|
2023-03-31 04:34:14 +00:00
|
|
|
for i := 0; i < len(cronJobs); i++ {
|
2023-03-28 10:00:06 +00:00
|
|
|
entryID, err := service.NewICronjobService().StartJob(&cronJobs[i])
|
2023-03-14 02:19:29 +00:00
|
|
|
if err != nil {
|
2023-04-10 09:30:23 +00:00
|
|
|
global.LOG.Errorf("start %s job %s failed, err: %v", cronJobs[i].Type, cronJobs[i].Name, err)
|
2022-09-27 09:26:01 +00:00
|
|
|
}
|
2023-03-31 04:34:14 +00:00
|
|
|
if err := repo.NewICronjobRepo().Update(cronJobs[i].ID, map[string]interface{}{"entry_id": entryID}); err != nil {
|
|
|
|
global.LOG.Errorf("update cronjob %s %s failed, err: %v", cronJobs[i].Type, cronJobs[i].Name, err)
|
2023-03-14 02:19:29 +00:00
|
|
|
}
|
2022-09-27 09:26:01 +00:00
|
|
|
}
|
2022-09-08 03:39:14 +00:00
|
|
|
}
|
2023-11-02 10:22:41 +00:00
|
|
|
|
|
|
|
func syncBeforeStart() {
|
|
|
|
var ntpSite model.Setting
|
|
|
|
if err := global.DB.Where("key = ?", "NtpSite").Find(&ntpSite).Error; err != nil {
|
|
|
|
global.LOG.Errorf("load ntp serve from db failed, err: %v", err)
|
|
|
|
}
|
|
|
|
if len(ntpSite.Value) == 0 {
|
|
|
|
ntpSite.Value = "pool.ntp.org"
|
|
|
|
}
|
|
|
|
ntime, err := ntp.GetRemoteTime(ntpSite.Value)
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("load remote time with [%s] failed, err: %v", ntpSite.Value, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ts := ntime.Format("2006-01-02 15:04:05")
|
|
|
|
if err := ntp.UpdateSystemTime(ts); err != nil {
|
|
|
|
global.LOG.Errorf("failed to synchronize system time with [%s], err: %v", ntpSite.Value, err)
|
|
|
|
}
|
|
|
|
global.LOG.Debugf("synchronize system time with [%s] successful!", ntpSite.Value)
|
|
|
|
}
|