@@ -32,6 +32,9 @@ type Schedule interface {
32
32
33
33
// Entry consists of a schedule and the func to execute on that schedule.
34
34
type Entry struct {
35
+ Description string
36
+ Spec string
37
+
35
38
// The schedule on which this job should be run.
36
39
Schedule Schedule
37
40
@@ -45,6 +48,8 @@ type Entry struct {
45
48
46
49
// The Job to run.
47
50
Job Job
51
+
52
+ ExecTimes int // Execute times count.
48
53
}
49
54
50
55
// byTime is a wrapper for sorting the entry array by time
@@ -83,32 +88,33 @@ type FuncJob func()
83
88
func (f FuncJob ) Run () { f () }
84
89
85
90
// AddFunc adds a func to the Cron to be run on the given schedule.
86
- func (c * Cron ) AddFunc (spec string , cmd func ()) error {
87
- return c .AddJob (spec , FuncJob (cmd ))
91
+ func (c * Cron ) AddFunc (desc , spec string , cmd func ()) ( * Entry , error ) {
92
+ return c .AddJob (desc , spec , FuncJob (cmd ))
88
93
}
89
94
90
95
// AddFunc adds a Job to the Cron to be run on the given schedule.
91
- func (c * Cron ) AddJob (spec string , cmd Job ) error {
96
+ func (c * Cron ) AddJob (desc , spec string , cmd Job ) ( * Entry , error ) {
92
97
schedule , err := Parse (spec )
93
98
if err != nil {
94
- return err
99
+ return nil , err
95
100
}
96
- c .Schedule (schedule , cmd )
97
- return nil
101
+ return c .Schedule (desc , spec , schedule , cmd ), nil
98
102
}
99
103
100
104
// Schedule adds a Job to the Cron to be run on the given schedule.
101
- func (c * Cron ) Schedule (schedule Schedule , cmd Job ) {
105
+ func (c * Cron ) Schedule (desc , spec string , schedule Schedule , cmd Job ) * Entry {
102
106
entry := & Entry {
103
- Schedule : schedule ,
104
- Job : cmd ,
107
+ Description : desc ,
108
+ Spec : spec ,
109
+ Schedule : schedule ,
110
+ Job : cmd ,
105
111
}
106
- if ! c .running {
112
+ if c .running {
113
+ c .add <- entry
114
+ } else {
107
115
c .entries = append (c .entries , entry )
108
- return
109
116
}
110
-
111
- c .add <- entry
117
+ return entry
112
118
}
113
119
114
120
// Entries returns a snapshot of the cron entries.
@@ -157,6 +163,7 @@ func (c *Cron) run() {
157
163
break
158
164
}
159
165
go e .Job .Run ()
166
+ e .ExecTimes ++
160
167
e .Prev = e .Next
161
168
e .Next = e .Schedule .Next (effective )
162
169
}
@@ -192,10 +199,13 @@ func (c *Cron) entrySnapshot() []*Entry {
192
199
entries := []* Entry {}
193
200
for _ , e := range c .entries {
194
201
entries = append (entries , & Entry {
195
- Schedule : e .Schedule ,
196
- Next : e .Next ,
197
- Prev : e .Prev ,
198
- Job : e .Job ,
202
+ Description : e .Description ,
203
+ Spec : e .Spec ,
204
+ Schedule : e .Schedule ,
205
+ Next : e .Next ,
206
+ Prev : e .Prev ,
207
+ Job : e .Job ,
208
+ ExecTimes : e .ExecTimes ,
199
209
})
200
210
}
201
211
return entries
0 commit comments