2022-09-08 11:39:14 +08:00
|
|
|
package cron
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"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"
|
2022-09-08 11:39:14 +08:00
|
|
|
"github.com/robfig/cron/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Run() {
|
|
|
|
nyc, _ := time.LoadLocation("Asia/Shanghai")
|
2022-09-27 17:26:01 +08:00
|
|
|
Cron := cron.New(cron.WithLocation(nyc), cron.WithChain(cron.Recover(cron.DefaultLogger)), cron.WithChain(cron.DelayIfStillRunning(cron.DefaultLogger)))
|
2022-09-08 11:39:14 +08:00
|
|
|
_, err := Cron.AddJob("@every 1m", job.NewMonitorJob())
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("can not add corn job: %s", err.Error())
|
|
|
|
}
|
|
|
|
Cron.Start()
|
|
|
|
|
2022-09-20 19:12:48 +08:00
|
|
|
global.Cron = Cron
|
2022-09-27 17:26:01 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2022-09-28 18:11:36 +08: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)
|
|
|
|
}
|
2022-09-27 17:26:01 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 11:39:14 +08:00
|
|
|
}
|