-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.go
159 lines (130 loc) · 2.93 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
package cron
import (
"errors"
"log"
"sort"
"time"
)
type Entry struct {
schedule *Schedule
task *Task
next time.Time
}
type Cron struct {
entries []*Entry
add chan *Entry
stop chan struct{}
running bool
Logger Logger
}
func NewCron() *Cron {
return &Cron{
entries: make([]*Entry, 0),
add: make(chan *Entry),
stop: make(chan struct{}),
running: false,
}
}
func (c *Cron) AddTask(name string, scheduleStr string, fn func(...interface{}) error, args ...interface{}) (err error) {
if fn == nil {
err = errors.New("fn can not be nil")
return
}
schedule, err := Parse(scheduleStr)
if err != nil {
return
}
entry := &Entry{
schedule: schedule,
task: &Task{
name: name,
fn: fn,
args: args,
},
}
if !c.running {
c.entries = append(c.entries, entry)
} else {
c.add <- entry
}
return
}
func (c *Cron) IsRunning() bool {
return c.running
}
func (c *Cron) Stop() {
c.stop <- struct{}{}
}
func (c *Cron) Start() {
if c.running {
return
}
if c.Logger != nil {
c.Logger.Info("%s cron start\n", time.Now().Format("2006-01-02 15:04:05"))
} else {
log.Printf("%s cron start\n", time.Now().Format("2006-01-02 15:04:05"))
}
c.running = true
go func() {
now := time.Now()
// 计算任务的下一次执行时间
for _, entry := range c.entries {
entry.next = entry.schedule.Next(now)
}
for {
// 按时间排序
sort.Slice(c.entries, func(i, j int) bool {
return c.entries[i].next.Before(c.entries[j].next)
})
// 设置timer
var timer *time.Timer
if len(c.entries) <= 0 || c.entries[0].next.IsZero() {
timer = time.NewTimer(100000 * time.Hour)
} else {
timer = time.NewTimer(c.entries[0].next.Sub(now))
}
// select
select {
case now = <-timer.C:
for _, entry := range c.entries {
if entry.next.After(now) || entry.next.IsZero() {
break
}
go c.runTask(entry.task)
entry.next = entry.schedule.Next(now)
}
case newEntry := <-c.add:
timer.Stop()
now = time.Now()
newEntry.next = newEntry.schedule.Next(now)
c.entries = append(c.entries, newEntry)
case <-c.stop:
timer.Stop()
return
}
}
}()
return
}
func (c *Cron) runTask(task *Task) {
if c.Logger != nil {
c.Logger.Info("%s task:%s begin\n", task.name, time.Now().Format("2006-01-02 15:04:05"))
} else {
log.Printf("%s task:%s begin\n", task.name, time.Now().Format("2006-01-02 15:04:05"))
}
err := task.fn(task.args...)
if c.Logger != nil {
if err != nil {
c.Logger.Error("%s task:%s end, err: %v\n", task.name, time.Now().Format("2006-01-02 15:04:05"), err)
} else {
c.Logger.Info("%s task:%s end\n", task.name, time.Now().Format("2006-01-02 15:04:05"))
}
} else {
if err != nil {
log.Printf("%s task:%s end, err: %v\n", task.name, time.Now().Format("2006-01-02 15:04:05"), err)
} else {
log.Printf("%s task:%s end\n", task.name, time.Now().Format("2006-01-02 15:04:05"))
}
}
return
}