-
Notifications
You must be signed in to change notification settings - Fork 10
/
cron.go
235 lines (191 loc) · 3.43 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
package cron
import (
"fmt"
"sync/atomic"
"time"
"github.com/qingwave/gocorex/containerx"
"github.com/go-logr/logr"
)
type Interface interface {
Run() error
Stop()
Add(*Task)
Remove(string)
Len() int
}
type Job interface {
Name() string
Exec() error
}
func SimpleJob(name string, exec func() error) Job {
return &job{
name: name,
exec: exec,
}
}
type job struct {
name string
exec func() error
}
func (j *job) Name() string {
return j.name
}
func (j *job) Exec() error {
return j.exec()
}
type Schedule interface {
Next(time.Time) time.Time
}
const RunAlways = -1
type schedule struct {
from time.Time
times int
interval time.Duration
}
func (s *schedule) nextTime(t time.Time) time.Time {
if s.from.IsZero() {
return time.Time{}
}
if s.from.After(t) {
return s.from
}
if s.interval == 0 {
return time.Time{}
}
return t.Add(s.interval)
}
func (s *schedule) Next(t time.Time) time.Time {
if s.times == 0 {
return time.Time{}
}
next := s.nextTime(t)
if !next.IsZero() {
if s.times > 0 {
s.times--
}
}
return next
}
func Every(duration time.Duration) Schedule {
return &schedule{
from: time.Now(),
times: RunAlways,
interval: duration,
}
}
func EveryAt(from time.Time, duration time.Duration) Schedule {
return &schedule{
from: time.Now(),
times: RunAlways,
interval: duration,
}
}
func Once(at time.Time) Schedule {
return &schedule{
from: at,
times: 1,
}
}
func At(from time.Time, duration time.Duration, times int) Schedule {
return &schedule{
from: from,
times: times,
interval: duration,
}
}
type Task struct {
next time.Time
Job
Schedule
}
type Cron struct {
tasks *containerx.Heap[*Task]
set containerx.Set[string]
new chan struct{}
started *atomic.Bool
logger logr.Logger
}
func NewCron(logger logr.Logger) Interface {
h := containerx.NewHeap([]*Task{}, func(x, y *Task) bool {
return x.next.Before(y.next)
})
return &Cron{
tasks: h,
set: containerx.NewSet[string](),
new: make(chan struct{}, 8),
started: new(atomic.Bool),
logger: logger,
}
}
func (c *Cron) Add(task *Task) {
if task.next.IsZero() {
task.next = task.Schedule.Next(time.Now())
}
c.tasks.Push(task)
c.set.Insert(task.Name())
if c.started.Load() {
c.new <- struct{}{}
}
}
func (c *Cron) Remove(name string) {
c.set.Delete(name)
}
func (c *Cron) Len() int {
return c.tasks.Len()
}
func (c *Cron) Run() error {
c.started.Store(true)
for {
if !c.started.Load() {
break
}
c.runTask()
}
return nil
}
const infTime time.Duration = 1<<63 - 1
func (c *Cron) runTask() {
now := time.Now()
duration := infTime
task, ok := c.tasks.Peek()
if ok {
if !c.set.Has(task.Name()) {
c.tasks.Pop()
return
}
if task.next.After(now) {
duration = task.next.Sub(now)
} else {
duration = 0
}
}
timer := time.NewTimer(duration)
defer timer.Stop()
select {
case <-c.new:
return
case <-timer.C:
}
task, ok = c.tasks.Pop()
if !ok {
return
}
go func() {
start := time.Now()
if err := task.Exec(); err != nil {
c.logger.Info(fmt.Sprintf("Run job [%s] failed: %v", task.Name(), err))
return
}
c.logger.Info(fmt.Sprintf("Run job [%s] successfully, duration %v", task.Name(), time.Since(start)))
}()
task.next = task.Next(time.Now())
if task.next.IsZero() {
c.set.Delete(task.Name())
} else {
c.tasks.Push(task)
}
}
func (c *Cron) Stop() {
c.started.Store(false)
close(c.new)
}