-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
274 lines (241 loc) · 6.32 KB
/
task.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package zcron
import (
"sync"
"sync/atomic"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/zlyuancn/zutils"
)
type ITask interface {
// 返回任务名
Name() string
// 获取handler
Handler() Handler
// 返回启用状态
IsEnable() bool
// 任务信息
TaskInfo() *TaskInfo
// 输出
String() string
// 获取触发时间
TriggerTime() time.Time
// 生成下次触发时间, 如果返回了 false 表示没有下一次了, 返回的时间一定>t
MakeNextTriggerTime(t time.Time) (time.Time, bool)
// 立即触发执行, 阻塞等待执行结束
Trigger(callback ErrCallback) *ExecuteInfo
// 修改执行器
ChangeExecutor(executor IExecutor)
// 重置定时, 发生在被定时器添加任务时和重新设为启用时
resetClock()
// 设置启用
setEnable(enable bool)
// 设置堆索引
setHeapIndex(index int)
// 获取堆索引
getHeapIndex() int
}
// 任务信息
type TaskInfo struct {
// 任务名
Name string
// 是否启用
Enable bool
// 执行成功数
ExecutedSuccessNum uint64
// 执行失败数
ExecuteFailureNum uint64
// 最后一次执行信息
LastExecuteInfo *ExecuteInfo
// 触发器信息
TriggerInfo *TriggerInfo
// 执行器信息
ExecutorInfo *ExecutorInfo
}
// 执行信息
type ExecuteInfo struct {
// 执行开始时间
ExecuteStartTime time.Time
// 执行结束时间
ExecuteEndTime time.Time
// 是否执行成功
ExecuteSuccess bool
// 执行的错误, 如果最后一次执行没有错误那么值为nil
ExecuteErr error
}
type Task struct {
name string
successNum uint64
failureNum uint64
handler Handler
lastExecuteInfo *ExecuteInfo
triggerTime time.Time
trigger ITrigger
executor IExecutor
enable int32
mx sync.Mutex // 用于锁 triggerTime, lastExecuteInfo, trigger, executor
heapIndex int // 堆索引
}
type TaskConfig struct {
Trigger ITrigger
Executor IExecutor
Handler Handler
Enable bool
}
// 创建一个任务
func NewTask(name string, expression string, enable bool, handler Handler) ITask {
trigger := NewCronTrigger(expression)
executor := NewExecutor(0, 0, 1)
return NewTaskOfConfig(name, TaskConfig{
Trigger: trigger,
Executor: executor,
Handler: handler,
Enable: enable,
})
}
// 根据任务配置创建一个任务
func NewTaskOfConfig(name string, config TaskConfig) ITask {
t := &Task{
name: name,
trigger: config.Trigger,
executor: config.Executor,
handler: config.Handler,
}
t.setEnable(config.Enable)
return t
}
func (t *Task) Name() string {
return t.name
}
func (t *Task) Handler() Handler {
return t.handler
}
func (t *Task) IsEnable() bool {
return atomic.LoadInt32(&t.enable) == 1
}
func (t *Task) TaskInfo() *TaskInfo {
t.mx.Lock()
lastInfo := t.lastExecuteInfo
trigger := t.trigger
executor := t.executor
t.mx.Unlock()
info := &TaskInfo{
Name: t.name,
Enable: t.IsEnable(),
ExecutedSuccessNum: atomic.LoadUint64(&t.successNum),
ExecuteFailureNum: atomic.LoadUint64(&t.failureNum),
LastExecuteInfo: nil,
TriggerInfo: trigger.TriggerInfo(),
ExecutorInfo: executor.ExecutorInfo(),
}
if lastInfo != nil {
v := *lastInfo
info.LastExecuteInfo = &v
}
return info
}
func (t *Task) String() string {
taskInfo := t.TaskInfo()
triggerInfo := taskInfo.TriggerInfo
executorInfo := taskInfo.ExecutorInfo
info := map[string]interface{}{
"name": taskInfo.Name,
"enable": taskInfo.Enable,
"execute_success_num": taskInfo.ExecutedSuccessNum,
"execute_failure_num": taskInfo.ExecuteFailureNum,
"last_execute_info": nil,
"trigger_info": map[string]interface{}{
"trigger_type": triggerInfo.TriggerType,
"expression": triggerInfo.Expression,
"next_trigger_time": zutils.Time.TimeToText(triggerInfo.NextTriggerTime),
"next_trigger_time_stamp": triggerInfo.NextTriggerTime.Unix(),
},
"executor_info": map[string]interface{}{
"max_concurrent_execute_count": executorInfo.MaxConcurrentExecuteCount,
"concurrent_execute_count": executorInfo.ConcurrentExecuteCount,
"max_retry_count": executorInfo.MaxRetryCount,
"retry_interval": executorInfo.RetryInterval.String(),
},
}
if taskInfo.LastExecuteInfo != nil {
var errMsg string
if taskInfo.LastExecuteInfo.ExecuteErr != nil {
errMsg = taskInfo.LastExecuteInfo.ExecuteErr.Error()
}
info["last_execute_info"] = map[string]interface{}{
"execute_start_time": zutils.Time.TimeToText(taskInfo.LastExecuteInfo.ExecuteStartTime),
"execute_start_time_stamp": taskInfo.LastExecuteInfo.ExecuteStartTime.Unix(),
"execute_end_time": zutils.Time.TimeToText(taskInfo.LastExecuteInfo.ExecuteEndTime),
"execute_end_time_stamp": taskInfo.LastExecuteInfo.ExecuteEndTime.Unix(),
"execute_success": taskInfo.LastExecuteInfo.ExecuteSuccess,
"err_message": errMsg,
}
}
text, _ := jsoniter.ConfigCompatibleWithStandardLibrary.MarshalToString(info)
return text
}
func (t *Task) TriggerTime() time.Time {
t.mx.Lock()
tt := t.triggerTime
t.mx.Unlock()
return tt
}
func (t *Task) MakeNextTriggerTime(tt time.Time) (time.Time, bool) {
if !t.IsEnable() {
return tt, false
}
tt, ok := t.trigger.MakeNextTriggerTime(tt)
t.mx.Lock()
t.triggerTime = tt
t.mx.Unlock()
return tt, ok
}
func (t *Task) Trigger(callback ErrCallback) *ExecuteInfo {
ctx := newContext(t)
info := &ExecuteInfo{
ExecuteStartTime: time.Now(),
}
err := t.execute(ctx, callback)
info.ExecuteEndTime = time.Now()
if err != nil {
info.ExecuteErr = err
atomic.AddUint64(&t.failureNum, 1)
} else {
info.ExecuteSuccess = true
atomic.AddUint64(&t.successNum, 1)
}
t.mx.Lock()
t.lastExecuteInfo = info
t.mx.Unlock()
return info
}
// 执行
func (t *Task) execute(ctx IContext, errCallback ErrCallback) error {
t.mx.Lock()
executor := t.executor
t.mx.Unlock()
return executor.Do(ctx, errCallback)
}
func (t *Task) ChangeExecutor(executor IExecutor) {
t.mx.Lock()
t.executor = executor
t.mx.Unlock()
}
func (t *Task) resetClock() {
t.mx.Lock()
trigger := t.trigger
t.mx.Unlock()
trigger.ResetClock()
}
func (t *Task) setEnable(enable bool) {
if enable {
atomic.StoreInt32(&t.enable, 1)
} else {
atomic.StoreInt32(&t.enable, 0)
}
}
func (t *Task) setHeapIndex(index int) {
t.heapIndex = index
}
func (t *Task) getHeapIndex() int {
return t.heapIndex
}