-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_manager.go
181 lines (159 loc) · 4.87 KB
/
task_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package cronjob
import (
"context"
"cronjob/logger"
"cronjob/mysql"
"database/sql"
"encoding/json"
"fmt"
"github.com/go-redis/redis/v8"
"strconv"
"strings"
"sync"
"time"
)
type TaskConfInfo struct {
Schedue string //format: * * * * *,represent the M, H, D, M, w, with Unix crontab definition
Repeatable bool //True Said whether or not the last time scheduling execution is completed, all new tasks,
//False Said before the last task, no longer the new task
Closed bool //whether the task is shut down,exposed to external can close the cron job
Params map[string]interface{} //task execution parameter
CallBack func(context.Context, map[string]interface{}) error //callback function
}
type CronManager struct {
ctx context.Context
redisCli *redis.Client
mysqlCli *sql.DB
tasks sync.Map
}
func NewCronManager(ctx context.Context, tasks map[string]*TaskConfInfo, redisCli *redis.Client, mysqlCli *sql.DB) *CronManager {
cm := &CronManager{
ctx: ctx,
redisCli: redisCli,
mysqlCli: mysqlCli,
}
if tasks == nil {
return cm
}
for name, info := range tasks {
cm.tasks.Store(name, info)
}
return cm
}
func (cm *CronManager) SetCronTask(taskname string, taskInfo *TaskConfInfo) {
cm.tasks.Store(taskname, taskInfo)
}
func (cm *CronManager) GetCronTask(taskname string) (*TaskConfInfo, bool) {
defer GoRecover(cm.ctx, "cron manager get cron task", true)
info, exist := cm.tasks.Load(taskname)
if !exist {
return nil, false
}
task := info.(*TaskConfInfo)
return task, exist
}
func (cm *CronManager) Range(f func(key, value any) bool) {
cm.tasks.Range(f)
}
const (
MaxErrorGetPageCount = 5
GetCronConfigLimit = 100
)
func (cm *CronManager) LoadConfig(ctx context.Context) {
defer GoRecover(cm.ctx, "cron manager load config panic", true)
page, errorGetCount := 0, 0
for {
offset := page * GetCronConfigLimit
taskConfList, err := mysql.GetAllOnlineCronConfigInfo(ctx, offset, GetCronConfigLimit)
if err != nil {
logger.Warn(fmt.Sprintf("get all online cron task config faild err:[%v]", err))
page++
errorGetCount++
if errorGetCount >= MaxErrorGetPageCount {
break
}
continue
}
if len(taskConfList) == 0 {
break
}
for _, taskConf := range taskConfList {
task, exist := cm.GetCronTask(taskConf.TaskName)
if exist {
if taskConf.CronExpr == "" {
logger.Warn(fmt.Sprintf("%s task execution cycle not configured, please check cron_expr filed", taskConf.TaskName))
continue
}
param := make(map[string]interface{})
if taskConf.Params != "" {
if err := json.Unmarshal([]byte(taskConf.Params), ¶m); err != nil {
logger.Warn(fmt.Sprintf("json unmarshal task params error,when load cron task config[%v]", err))
continue
}
}
newTask := &TaskConfInfo{
Schedue: taskConf.CronExpr,
Repeatable: taskConf.Repeatable == 1,
Closed: taskConf.Closed == 1,
CallBack: task.CallBack,
Params: param,
}
cm.SetCronTask(taskConf.TaskName, newTask)
}
}
page++
}
}
func (cm *CronManager) Run() {
defer GoRecover(cm.ctx, "cron manager run panic", true)
cm.LoadConfig(cm.ctx)
// To perform a task scheduling interval for a second
ticker := time.NewTicker(time.Second * 1)
defer ticker.Stop()
for {
select {
case <-ticker.C:
cm.tasks.Range(cm.dispatch)
case <-cm.ctx.Done():
return
}
}
}
var CronLockKey = "cron_task_lock_%s_%s-%s_%s:%s"
func (cm *CronManager) dispatch(key, value interface{}) bool {
defer GoRecover(cm.ctx, "cron manager dispatch panic", true)
curTime := time.Now()
taskName := key.(string)
taskConf := value.(*TaskConfInfo)
// Check whether the current time to execute
if taskConf.Closed || taskConf.CallBack == nil {
return true
}
if !IsCronTime(curTime, strings.TrimSpace(taskConf.Schedue)) {
return true
}
// Check the redis locks, judge whether there is a task has been executed
lockKey := fmt.Sprintf(CronLockKey, taskName, strconv.Itoa(int(curTime.Month())),
strconv.Itoa(curTime.Day()), strconv.Itoa(curTime.Hour()), strconv.Itoa(curTime.Minute()))
locker, _ := cm.redisCli.Incr(cm.ctx, lockKey).Result()
// Lock failed or has been locked
if locker != 1 {
return true
}
if _, err := cm.redisCli.Expire(cm.ctx, lockKey, time.Minute).Result(); err != nil {
logger.Warn(fmt.Sprintf("cron task set redis expire error:[%v]", err))
}
// do task
go func(taskName string, task *TaskConfInfo) {
defer GoRecover(cm.ctx, "do task panic", true)
taskCtx, cancel := context.WithCancel(context.Background())
defer cancel()
logger.Info(fmt.Sprintf("Cron_Task_Start:[%s]", taskName))
errCb := task.CallBack(taskCtx, task.Params)
if errCb != nil {
logger.Warn(fmt.Sprintf("cron task set redis expire error:[%v]", errCb))
}
logger.Info(fmt.Sprintf("Cron_Task_End:[%s]", taskName))
}(taskName, taskConf)
return true
}