-
Notifications
You must be signed in to change notification settings - Fork 63
/
cron_manager.go
142 lines (127 loc) · 3.08 KB
/
cron_manager.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package cron_task
import (
"os"
"os/exec"
"path"
"strconv"
"sync"
"time"
"github.com/hootrhino/rulex/component/interdb"
"github.com/hootrhino/rulex/component/rulex_api_server/model"
"github.com/hootrhino/rulex/glogger"
"github.com/robfig/cron/v3"
)
var cronManager *CronManager
type CronManager struct {
cronEngine *cron.Cron
mtx sync.Mutex
crontab map[string]cron.EntryID
runningTask sync.Map
processManager *ProcessManager
}
func GetCronManager() *CronManager {
if cronManager == nil {
cronManager = NewCronManager()
}
return cronManager
}
func NewCronManager() *CronManager {
engine := cron.New(
cron.WithChain(
cron.SkipIfStillRunning(cron.DefaultLogger),
cron.Recover(cron.DefaultLogger),
),
cron.WithSeconds(),
)
manager := CronManager{
cronEngine: engine,
crontab: make(map[string]cron.EntryID),
processManager: NewProcessManager(),
mtx: sync.Mutex{},
}
engine.Start()
return &manager
}
func (m *CronManager) AddTask(task model.MCronTask) error {
cronExpr := task.CronExpr
id := task.UUID
m.mtx.Lock()
defer m.mtx.Unlock()
if _, ok := m.crontab[id]; ok {
return nil
}
dir, _ := os.Getwd()
task.WorkDir = path.Join(".")
err := os.MkdirAll(dir, PERM_0777)
if err != nil {
return err
}
entryId, err := m.cronEngine.AddFunc(cronExpr, func() {
// create a task execute log record
result := model.MCronResult{
TaskUuid: task.UUID,
Status: CRON_RESULT_STATUS_RUNNING,
StartTime: time.Now(),
}
saveResults(&result)
taskLogger := glogger.GLogger.WithField("task_uuid", task.UUID)
taskLogger.Info("---------------Start task---------------")
m.runningTask.Store(id, task)
defer m.runningTask.Delete(id)
// 调用process manager启动任务并等待其完成
err = m.processManager.RunProcess(taskLogger.Logger.Out, task)
exitCode := 0
if err != nil {
taskLogger.Error("Task Return Error, err=", err)
if exitError, ok := err.(*exec.ExitError); ok {
// 进程退出时返回非零状态码
exitCode = exitError.ExitCode()
} else {
exitCode = -1
}
}
taskLogger.Info("---------------End task---------------")
result.EndTime = time.Now()
result.Status = CRON_RESULT_STATUS_END
result.ExitCode = strconv.Itoa(exitCode)
saveResults(&result)
})
if err != nil {
return err
}
m.crontab[id] = entryId
return nil
}
func saveResults(m *model.MCronResult) {
db := interdb.DB()
if m.ID == 0 {
db.Create(m)
} else {
db.Updates(m)
}
}
func (m *CronManager) DeleteTask(id string) {
entryID, ok := m.crontab[id]
if !ok {
return
}
err := m.processManager.KillProcess(id)
if err != nil {
glogger.GLogger.Error("kill process failed, err=%+v", err)
return
}
m.cronEngine.Remove(entryID)
delete(m.crontab, id)
}
func (m *CronManager) KillTask(uuid string) error {
return m.processManager.KillProcess(uuid)
}
func (m *CronManager) ListRunningTask() []model.MCronTask {
tasks := make([]model.MCronTask, 0)
m.runningTask.Range(func(key, value any) bool {
task := value.(model.MCronTask)
tasks = append(tasks, task)
return true
})
return tasks
}