-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathcron.go
More file actions
96 lines (89 loc) · 3.6 KB
/
cron.go
File metadata and controls
96 lines (89 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cron
import (
"fmt"
mathRand "math/rand"
"time"
"github.com/1Panel-dev/1Panel/agent/app/model"
"github.com/1Panel-dev/1Panel/agent/app/repo"
"github.com/1Panel-dev/1Panel/agent/app/service"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/cron/job"
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/utils/common"
"github.com/1Panel-dev/1Panel/agent/utils/ntp"
"github.com/robfig/cron/v3"
)
func Run() {
nyc, _ := time.LoadLocation(common.LoadTimeZoneByCmd())
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
)
go syncBeforeStart()
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())
}
}
if _, err := global.Cron.AddJob("@daily", job.NewWebsiteJob()); err != nil {
global.LOG.Errorf("can not add website corn job: %s", err.Error())
}
if _, err := global.Cron.AddJob("@daily", job.NewSSLJob()); err != nil {
global.LOG.Errorf("can not add ssl corn job: %s", err.Error())
}
if _, err := global.Cron.AddJob(fmt.Sprintf("%v %v * * *", mathRand.Intn(60), mathRand.Intn(3)), job.NewAppStoreJob()); err != nil {
global.LOG.Errorf("can not add appstore corn job: %s", err.Error())
}
if _, err := global.Cron.AddJob("0 3 */31 * *", job.NewBackupJob()); err != nil {
global.LOG.Errorf("can not add backup token refresh corn job: %s", err.Error())
}
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{}).
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)
}
for i := 0; i < len(cronJobs); i++ {
entryIDs, err := service.NewICronjobService().StartJob(&cronJobs[i], false)
if err != nil {
global.LOG.Errorf("start %s job %s failed, err: %v", cronJobs[i].Type, cronJobs[i].Name, err)
}
if err := repo.NewICronjobRepo().Update(cronJobs[i].ID, map[string]interface{}{"entry_ids": entryIDs}); err != nil {
global.LOG.Errorf("update cronjob %s %s failed, err: %v", cronJobs[i].Type, cronJobs[i].Name, err)
}
}
global.Cron.Start()
}
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(constant.DateTimeLayout)
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)
}