Skip to content

Commit 6f2c785

Browse files
author
piexlMax
committed
调整为新的timer模式 增加每个task内具体方法的描述方便对接后续的页面控制
1 parent 05a1ce5 commit 6f2c785

File tree

3 files changed

+99
-33
lines changed

3 files changed

+99
-33
lines changed

server/initialize/timer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func Timer() {
1919
if err != nil {
2020
fmt.Println("timer error:", err)
2121
}
22-
}, option...)
22+
}, "定时清理数据库【日志,黑名单】内容", option...)
2323
if err != nil {
2424
fmt.Println("add timer error:", err)
2525
}

server/utils/timer/timed_task.go

Lines changed: 91 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,152 @@
11
package timer
22

33
import (
4-
"sync"
5-
64
"github.com/robfig/cron/v3"
5+
"sync"
76
)
87

8+
type GVA_Timer interface {
9+
Timer
10+
FindTaskList() map[string]*taskManager
11+
AddTaskByFuncWithSecond(taskName string, spec string, fun func(), Desc string, option ...cron.Option) (cron.EntryID, error)
12+
AddTaskByJobWithSeconds(taskName string, spec string, job interface{ Run() }, Desc string, option ...cron.Option) (cron.EntryID, error)
13+
}
14+
915
type Timer interface {
10-
AddTaskByFunc(taskName string, spec string, task func(), option ...cron.Option) (cron.EntryID, error)
11-
AddTaskByJob(taskName string, spec string, job interface{ Run() }, option ...cron.Option) (cron.EntryID, error)
12-
FindCron(taskName string) (*cron.Cron, bool)
16+
AddTaskByFunc(taskName string, spec string, task func(), Desc string, option ...cron.Option) (cron.EntryID, error)
17+
AddTaskByJob(taskName string, spec string, job interface{ Run() }, Desc string, option ...cron.Option) (cron.EntryID, error)
18+
FindCron(taskName string) (*taskManager, bool)
1319
StartTask(taskName string)
1420
StopTask(taskName string)
1521
Remove(taskName string, id int)
1622
Clear(taskName string)
1723
Close()
1824
}
1925

26+
type task struct {
27+
EntryID cron.EntryID
28+
Spec string
29+
Desc string
30+
}
31+
32+
type taskManager struct {
33+
corn *cron.Cron
34+
tasks map[cron.EntryID]*task
35+
}
36+
2037
// timer 定时任务管理
2138
type timer struct {
22-
taskList map[string]*cron.Cron
39+
taskList map[string]*taskManager
2340
sync.Mutex
2441
}
2542

2643
// AddTaskByFunc 通过函数的方法添加任务
27-
func (t *timer) AddTaskByFunc(taskName string, spec string, task func(), option ...cron.Option) (cron.EntryID, error) {
44+
func (t *timer) AddTaskByFunc(taskName string, spec string, fun func(), Desc string, option ...cron.Option) (cron.EntryID, error) {
2845
t.Lock()
2946
defer t.Unlock()
3047
if _, ok := t.taskList[taskName]; !ok {
31-
t.taskList[taskName] = cron.New(option...)
48+
tasks := make(map[cron.EntryID]*task)
49+
t.taskList[taskName] = &taskManager{
50+
corn: cron.New(option...),
51+
tasks: tasks,
52+
}
53+
}
54+
id, err := t.taskList[taskName].corn.AddFunc(spec, fun)
55+
t.taskList[taskName].corn.Start()
56+
t.taskList[taskName].tasks[id] = &task{
57+
EntryID: id,
58+
Spec: spec,
59+
Desc: Desc,
3260
}
33-
id, err := t.taskList[taskName].AddFunc(spec, task)
34-
t.taskList[taskName].Start()
3561
return id, err
3662
}
3763

3864
// AddTaskByFuncWithSeconds 通过函数的方法使用WithSeconds添加任务
39-
func (t *timer) AddTaskByFuncWhithSecond(taskName string, spec string, task func(), option ...cron.Option) (cron.EntryID, error) {
65+
func (t *timer) AddTaskByFuncWithSecond(taskName string, spec string, fun func(), Desc string, option ...cron.Option) (cron.EntryID, error) {
4066
t.Lock()
4167
defer t.Unlock()
4268
option = append(option, cron.WithSeconds())
4369
if _, ok := t.taskList[taskName]; !ok {
44-
t.taskList[taskName] = cron.New(option...)
70+
tasks := make(map[cron.EntryID]*task)
71+
t.taskList[taskName] = &taskManager{
72+
corn: cron.New(option...),
73+
tasks: tasks,
74+
}
75+
}
76+
id, err := t.taskList[taskName].corn.AddFunc(spec, fun)
77+
t.taskList[taskName].corn.Start()
78+
t.taskList[taskName].tasks[id] = &task{
79+
EntryID: id,
80+
Spec: spec,
81+
Desc: Desc,
4582
}
46-
id, err := t.taskList[taskName].AddFunc(spec, task)
47-
t.taskList[taskName].Start()
4883
return id, err
4984
}
5085

5186
// AddTaskByJob 通过接口的方法添加任务
52-
func (t *timer) AddTaskByJob(taskName string, spec string, job interface{ Run() }, option ...cron.Option) (cron.EntryID, error) {
87+
func (t *timer) AddTaskByJob(taskName string, spec string, job interface{ Run() }, Desc string, option ...cron.Option) (cron.EntryID, error) {
5388
t.Lock()
5489
defer t.Unlock()
5590
if _, ok := t.taskList[taskName]; !ok {
56-
t.taskList[taskName] = cron.New(option...)
91+
tasks := make(map[cron.EntryID]*task)
92+
t.taskList[taskName] = &taskManager{
93+
corn: cron.New(option...),
94+
tasks: tasks,
95+
}
96+
}
97+
id, err := t.taskList[taskName].corn.AddJob(spec, job)
98+
t.taskList[taskName].corn.Start()
99+
t.taskList[taskName].tasks[id] = &task{
100+
EntryID: id,
101+
Spec: spec,
102+
Desc: Desc,
57103
}
58-
id, err := t.taskList[taskName].AddJob(spec, job)
59-
t.taskList[taskName].Start()
60104
return id, err
61105
}
62106

63107
// AddTaskByJobWithSeconds 通过接口的方法添加任务
64-
func (t *timer) AddTaskByJobWithSeconds(taskName string, spec string, job interface{ Run() }, option ...cron.Option) (cron.EntryID, error) {
108+
func (t *timer) AddTaskByJobWithSeconds(taskName string, spec string, job interface{ Run() }, Desc string, option ...cron.Option) (cron.EntryID, error) {
65109
t.Lock()
66110
defer t.Unlock()
67111
option = append(option, cron.WithSeconds())
68112
if _, ok := t.taskList[taskName]; !ok {
69-
t.taskList[taskName] = cron.New(option...)
113+
tasks := make(map[cron.EntryID]*task)
114+
t.taskList[taskName] = &taskManager{
115+
corn: cron.New(option...),
116+
tasks: tasks,
117+
}
118+
}
119+
id, err := t.taskList[taskName].corn.AddJob(spec, job)
120+
t.taskList[taskName].corn.Start()
121+
t.taskList[taskName].tasks[id] = &task{
122+
EntryID: id,
123+
Spec: spec,
124+
Desc: Desc,
70125
}
71-
id, err := t.taskList[taskName].AddJob(spec, job)
72-
t.taskList[taskName].Start()
73126
return id, err
74127
}
75128

76129
// FindCron 获取对应taskName的cron 可能会为空
77-
func (t *timer) FindCron(taskName string) (*cron.Cron, bool) {
130+
func (t *timer) FindCron(taskName string) (*taskManager, bool) {
78131
t.Lock()
79132
defer t.Unlock()
80133
v, ok := t.taskList[taskName]
81134
return v, ok
82135
}
83136

137+
// FindTaskList 获取所有的任务列表
138+
func (t *timer) FindTaskList() map[string]*taskManager {
139+
t.Lock()
140+
defer t.Unlock()
141+
return t.taskList
142+
}
143+
84144
// StartTask 开始任务
85145
func (t *timer) StartTask(taskName string) {
86146
t.Lock()
87147
defer t.Unlock()
88148
if v, ok := t.taskList[taskName]; ok {
89-
v.Start()
149+
v.corn.Start()
90150
}
91151
}
92152

@@ -95,7 +155,7 @@ func (t *timer) StopTask(taskName string) {
95155
t.Lock()
96156
defer t.Unlock()
97157
if v, ok := t.taskList[taskName]; ok {
98-
v.Stop()
158+
v.corn.Stop()
99159
}
100160
}
101161

@@ -104,7 +164,8 @@ func (t *timer) Remove(taskName string, id int) {
104164
t.Lock()
105165
defer t.Unlock()
106166
if v, ok := t.taskList[taskName]; ok {
107-
v.Remove(cron.EntryID(id))
167+
v.corn.Remove(cron.EntryID(id))
168+
delete(v.tasks, cron.EntryID(id))
108169
}
109170
}
110171

@@ -113,7 +174,7 @@ func (t *timer) Clear(taskName string) {
113174
t.Lock()
114175
defer t.Unlock()
115176
if v, ok := t.taskList[taskName]; ok {
116-
v.Stop()
177+
v.corn.Stop()
117178
delete(t.taskList, taskName)
118179
}
119180
}
@@ -123,10 +184,10 @@ func (t *timer) Close() {
123184
t.Lock()
124185
defer t.Unlock()
125186
for _, v := range t.taskList {
126-
v.Stop()
187+
v.corn.Stop()
127188
}
128189
}
129190

130-
func NewTimerTask() Timer {
131-
return &timer{taskList: make(map[string]*cron.Cron)}
191+
func NewTimerTask() GVA_Timer {
192+
return &timer{taskList: make(map[string]*taskManager)}
132193
}

server/utils/timer/timed_task_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestNewTimerTask(t *testing.T) {
2626
_tm := tm.(*timer)
2727

2828
{
29-
_, err := tm.AddTaskByFunc("func", "@every 1s", mockFunc)
29+
_, err := tm.AddTaskByFunc("func", "@every 1s", mockFunc, "测试mockfunc")
3030
assert.Nil(t, err)
3131
_, ok := _tm.taskList["func"]
3232
if !ok {
@@ -35,7 +35,7 @@ func TestNewTimerTask(t *testing.T) {
3535
}
3636

3737
{
38-
_, err := tm.AddTaskByJob("job", "@every 1s", job)
38+
_, err := tm.AddTaskByJob("job", "@every 1s", job, "测试job mockfunc")
3939
assert.Nil(t, err)
4040
_, ok := _tm.taskList["job"]
4141
if !ok {
@@ -64,4 +64,9 @@ func TestNewTimerTask(t *testing.T) {
6464
t.Error("find func")
6565
}
6666
}
67+
{
68+
a := tm.FindTaskList()
69+
b, c := tm.FindCron("job")
70+
fmt.Println(a, b, c)
71+
}
6772
}

0 commit comments

Comments
 (0)