11package timer
22
33import (
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+
915type 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 )
1319StartTask (taskName string )
1420StopTask (taskName string )
1521Remove (taskName string , id int )
1622Clear (taskName string )
1723Close ()
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 定时任务管理
2138type timer struct {
22- taskList map [string ]* cron. Cron
39+ taskList map [string ]* taskManager
2340sync.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 ) {
2845t .Lock ()
2946defer t .Unlock ()
3047if _ , 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 ()
3561return 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 ) {
4066t .Lock ()
4167defer t .Unlock ()
4268option = append (option , cron .WithSeconds ())
4369if _ , 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 ()
4883return 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 ) {
5388t .Lock ()
5489defer t .Unlock ()
5590if _ , 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 ()
60104return 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 ) {
65109t .Lock ()
66110defer t .Unlock ()
67111option = append (option , cron .WithSeconds ())
68112if _ , 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 ()
73126return 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 ) {
78131t .Lock ()
79132defer t .Unlock ()
80133v , ok := t .taskList [taskName ]
81134return 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 开始任务
85145func (t * timer ) StartTask (taskName string ) {
86146t .Lock ()
87147defer t .Unlock ()
88148if 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) {
95155t .Lock ()
96156defer t .Unlock ()
97157if 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) {
104164t .Lock ()
105165defer t .Unlock ()
106166if 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) {
113174t .Lock ()
114175defer t .Unlock ()
115176if v , ok := t .taskList [taskName ]; ok {
116- v .Stop ()
177+ v .corn . Stop ()
117178delete (t .taskList , taskName )
118179}
119180}
@@ -123,10 +184,10 @@ func (t *timer) Close() {
123184t .Lock ()
124185defer t .Unlock ()
125186for _ , 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}
0 commit comments