forked from ZhiHanZ/cron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.go
306 lines (255 loc) · 6.57 KB
/
cron.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// This library implements a cron spec parser and runner. See the README for
// more details.
package cron
import (
"sort"
"sync"
"sync/atomic"
"time"
"golang.org/x/sync/singleflight"
)
type entries []*Entry
// Cron keeps track of any number of entries, invoking the associated func as
// specified by the schedule. It may be started, stopped, and the entries may
// be inspected while running.
type Cron struct {
entries entries
stop chan struct{}
running bool
work chan workEntry
maxWorkers int
runningJobs atomic.Int64
mu sync.Mutex
singleFlight singleflight.Group
}
type workEntry struct {
f func()
name string
}
// Job is an interface for submitted cron jobs.
type Job interface {
Run()
}
// The Schedule describes a job's duty cycle.
type Schedule interface {
// Return the next activation time, later than the given time.
// Next is invoked initially, and then each time the job is run.
Next(time.Time) time.Time
}
// Entry consists of a schedule and the func to execute on that schedule.
type Entry struct {
// The schedule on which this job should be run.
Schedule Schedule
// The next time the job will run. This is the zero time if Cron has not been
// started or this entry's schedule is unsatisfiable
NextScheduleTime time.Time
// The Job to run.
Job Job
// Unique name to identify the Entry so as to be able to remove it later.
Name string
}
// byTime is a wrapper for sorting the entry array by time
// (with zero time at the end).
type byTime []*Entry
func (s byTime) Len() int { return len(s) }
func (s byTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s byTime) Less(i, j int) bool {
// Two zero times should return false.
// Otherwise, zero is "greater" than any other time.
// (To sort it at the end of the list.)
if s[i].NextScheduleTime.IsZero() {
return false
}
if s[j].NextScheduleTime.IsZero() {
return true
}
return s[i].NextScheduleTime.Before(s[j].NextScheduleTime)
}
type Option func(*Cron)
func WithMaxWorkers(maxWorkers int) Option {
return func(c *Cron) {
c.maxWorkers = maxWorkers
}
}
// New returns a new Cron job runner.
func New(opts ...Option) *Cron {
c := &Cron{
entries: nil,
stop: make(chan struct{}),
mu: sync.Mutex{},
work: make(chan workEntry, 2048),
running: false,
maxWorkers: 256,
runningJobs: atomic.Int64{},
singleFlight: singleflight.Group{},
}
for _, opt := range opts {
opt(c)
}
return c
}
func (c *Cron) worker() {
for {
select {
case w := <-c.work:
// avoid each job being executed multiple times at the same time. DoChan() will discard
// duplicate calls, so we don't need to check if the job is already running.
c.singleFlight.DoChan(w.name, func() (interface{}, error) {
c.runningJobs.Add(1)
defer c.runningJobs.Add(-1)
w.f()
return nil, nil
})
case <-c.stop:
return
}
}
}
func (c *Cron) QueuingJobs() int {
return len(c.work)
}
func (c *Cron) RunningJobs() int {
return int(c.runningJobs.Load())
}
// FuncJob A wrapper that turns a func() into a cron.Job
type FuncJob func()
func (f FuncJob) Run() { f() }
// AddFunc adds a func to the Cron to be run on the given schedule.
func (c *Cron) AddFunc(spec string, cmd func(), name string, tz *time.Location) {
c.AddJob(spec, FuncJob(cmd), name, tz)
}
// AddFunc adds a Job to the Cron to be run on the given schedule.
func (c *Cron) AddJob(spec string, cmd Job, name string, tz *time.Location) {
c.Schedule(Parse(spec, tz), cmd, name)
}
// RemoveJob removes a Job from the Cron based on name.
func (c *Cron) RemoveJob(name string) {
c.mu.Lock()
defer c.mu.Unlock()
i := c.entries.pos(name)
if i == -1 {
return
}
c.entries = c.entries[:i+copy(c.entries[i:], c.entries[i+1:])]
}
func (entrySlice entries) pos(name string) int {
for p, e := range entrySlice {
if e.Name == name {
return p
}
}
return -1
}
// Schedule adds a Job to the Cron to be run on the given schedule.
func (c *Cron) Schedule(schedule Schedule, cmd Job, name string) {
c.mu.Lock()
defer c.mu.Unlock()
newEntry := &Entry{
Schedule: schedule,
Job: cmd,
Name: name,
}
i := c.entries.pos(newEntry.Name)
if i != -1 {
return
}
c.entries = append(c.entries, newEntry)
newEntry.NextScheduleTime = newEntry.Schedule.Next(time.Now().Local())
}
// Entries returns a snapshot of the cron entries.
func (c *Cron) Entries() []Entry {
return c.sortEntries()
}
// Start the cron scheduler in its own go-routine.
func (c *Cron) Start() {
c.mu.Lock()
defer c.mu.Unlock()
if c.running {
return
}
c.running = true
// Start worker pool
for i := 0; i < c.maxWorkers; i++ {
go c.worker()
}
go c.run()
}
func (c *Cron) sortEntries() []Entry {
c.mu.Lock()
defer c.mu.Unlock()
sort.Sort(byTime(c.entries))
entries := []Entry{}
for _, e := range c.entries {
entries = append(entries, Entry{
Schedule: e.Schedule,
NextScheduleTime: e.NextScheduleTime,
Job: e.Job,
Name: e.Name,
})
}
return entries
}
func (c *Cron) bounceNextScheduleTime(name string) {
c.mu.Lock()
defer c.mu.Unlock()
for i := range c.entries {
if c.entries[i].Name == name {
c.entries[i].NextScheduleTime = c.entries[i].Schedule.Next(time.Now().Local())
}
}
}
func (c *Cron) step() bool {
// Determine the next entry to run.
var effective time.Time
entries := c.sortEntries()
if len(entries) == 0 || entries[0].NextScheduleTime.IsZero() {
// If there are no entries yet, just sleep 1 second
effective = time.Now().Local().Add(1 * time.Second)
} else {
effective = entries[0].NextScheduleTime
}
now := time.Now().Local()
select {
case <-time.After(effective.Sub(now)):
// reload entries, since the entries might be changed during the wait.
entries := c.sortEntries()
now := time.Now().Local()
for _, e := range entries {
// run every entry whose next time was less than now.
if e.NextScheduleTime.After(now) {
return true
}
c.bounceNextScheduleTime(e.Name)
c.work <- workEntry{
f: e.Job.Run,
name: e.Name,
}
}
return true
case <-c.stop:
return false
case <-time.After(time.Second):
// avoid block
return true
}
}
// Run the scheduler.. this is private just due to the need to synchronize
// access to the 'running' state variable.
func (c *Cron) run() {
// Figure out the next activation times for each entry.
for _, entry := range c.Entries() {
c.bounceNextScheduleTime(entry.Name)
}
for c.step() {
}
}
// Stop the cron scheduler.
func (c *Cron) Stop() {
c.mu.Lock()
defer c.mu.Unlock()
if !c.running {
return
}
close(c.stop)
c.running = false
}