add
wangpengfei
2023-08-25 9f98932726cb41697fabccbbbd876205e7255c95
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
package timer
 
import (
    "sync"
 
    "github.com/robfig/cron/v3"
)
 
type Timer interface {
    AddTaskByFunc(taskName string, spec string, task func(), option ...cron.Option) (cron.EntryID, error)
    AddTaskByJob(taskName string, spec string, job interface{ Run() }, option ...cron.Option) (cron.EntryID, error)
    FindCron(taskName string) (*cron.Cron, bool)
    StartTask(taskName string)
    StopTask(taskName string)
    Remove(taskName string, id int)
    Clear(taskName string)
    Close()
}
 
// timer 定时任务管理
type timer struct {
    taskList map[string]*cron.Cron
    sync.Mutex
}
 
// AddTaskByFunc 通过函数的方法添加任务
func (t *timer) AddTaskByFunc(taskName string, spec string, task func(), option ...cron.Option) (cron.EntryID, error) {
    t.Lock()
    defer t.Unlock()
    if _, ok := t.taskList[taskName]; !ok {
        t.taskList[taskName] = cron.New(option...)
    }
    id, err := t.taskList[taskName].AddFunc(spec, task)
    t.taskList[taskName].Start()
    return id, err
}
 
// AddTaskByFuncWithSeconds 通过函数的方法使用WithSeconds添加任务
func (t *timer) AddTaskByFuncWhithSecond(taskName string, spec string, task func(), option ...cron.Option) (cron.EntryID, error) {
    t.Lock()
    defer t.Unlock()
    option = append(option, cron.WithSeconds())
    if _, ok := t.taskList[taskName]; !ok {
        t.taskList[taskName] = cron.New(option...)
    }
    id, err := t.taskList[taskName].AddFunc(spec, task)
    t.taskList[taskName].Start()
    return id, err
}
 
// AddTaskByJob 通过接口的方法添加任务
func (t *timer) AddTaskByJob(taskName string, spec string, job interface{ Run() }, option ...cron.Option) (cron.EntryID, error) {
    t.Lock()
    defer t.Unlock()
    if _, ok := t.taskList[taskName]; !ok {
        t.taskList[taskName] = cron.New(option...)
    }
    id, err := t.taskList[taskName].AddJob(spec, job)
    t.taskList[taskName].Start()
    return id, err
}
 
// AddTaskByJobWithSeconds 通过接口的方法添加任务
func (t *timer) AddTaskByJobWithSeconds(taskName string, spec string, job interface{ Run() }, option ...cron.Option) (cron.EntryID, error) {
    t.Lock()
    defer t.Unlock()
    option = append(option, cron.WithSeconds())
    if _, ok := t.taskList[taskName]; !ok {
        t.taskList[taskName] = cron.New(option...)
    }
    id, err := t.taskList[taskName].AddJob(spec, job)
    t.taskList[taskName].Start()
    return id, err
}
 
// FindCron 获取对应taskName的cron 可能会为空
func (t *timer) FindCron(taskName string) (*cron.Cron, bool) {
    t.Lock()
    defer t.Unlock()
    v, ok := t.taskList[taskName]
    return v, ok
}
 
// StartTask 开始任务
func (t *timer) StartTask(taskName string) {
    t.Lock()
    defer t.Unlock()
    if v, ok := t.taskList[taskName]; ok {
        v.Start()
    }
}
 
// StopTask 停止任务
func (t *timer) StopTask(taskName string) {
    t.Lock()
    defer t.Unlock()
    if v, ok := t.taskList[taskName]; ok {
        v.Stop()
    }
}
 
// Remove 从taskName 删除指定任务
func (t *timer) Remove(taskName string, id int) {
    t.Lock()
    defer t.Unlock()
    if v, ok := t.taskList[taskName]; ok {
        v.Remove(cron.EntryID(id))
    }
}
 
// Clear 清除任务
func (t *timer) Clear(taskName string) {
    t.Lock()
    defer t.Unlock()
    if v, ok := t.taskList[taskName]; ok {
        v.Stop()
        delete(t.taskList, taskName)
    }
}
 
// Close 释放资源
func (t *timer) Close() {
    t.Lock()
    defer t.Unlock()
    for _, v := range t.taskList {
        v.Stop()
    }
}
 
func NewTimerTask() Timer {
    return &timer{taskList: make(map[string]*cron.Cron)}
}