-
Notifications
You must be signed in to change notification settings - Fork 0
/
redcron.go
237 lines (206 loc) · 4.85 KB
/
redcron.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
// Package redcron provides RedCron struct to run cron jobs periodically by using Redis.
package redcron
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/rand"
"sync"
"sync/atomic"
"time"
"github.com/redis/go-redis/v9"
)
// RedCron registers and runs cron jobs.
type RedCron struct {
cfg Config
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
crons map[string]cronProperties
cronsMu sync.Mutex
stopped int32
}
// New creates a new RedCron struct.
func New(cfg Config) (c *RedCron) {
c = &RedCron{
cfg: cfg,
crons: make(map[string]cronProperties),
}
c.ctx, c.cancel = context.WithCancel(context.Background())
return c
}
// Register registers a new cron job by the given parameters. It returns the underlying RedCron.
// If the underlying RedCron is stopped, registered cron jobs won't be triggered.
func (c *RedCron) Register(name string, repeatSec int, offsetSec int, f func(context.Context)) *RedCron {
if name == "" {
panic(errors.New("name must be non-empty"))
}
if repeatSec <= 0 {
panic(errors.New("repeatSec must be greater than zero"))
}
cp := cronProperties{
name: name,
repeatSec: repeatSec,
offsetSec: offsetSec,
}
c.cronsMu.Lock()
defer c.cronsMu.Unlock()
if _, ok := c.crons[name]; ok {
panic(fmt.Errorf("cron %q already registered", name))
}
c.crons[name] = cp
go c.run(cp, f)
return c
}
// Stop stops triggering cron jobs and waits for all jobs are finished.
// When ctx has been done, all contexts of jobs are cancelled.
// If ctx is nil, all contexts of jobs are cancelled immediately.
func (c *RedCron) Stop(ctx context.Context) {
atomic.CompareAndSwapInt32(&c.stopped, 0, 1)
done := make(chan struct{})
go func() {
c.wg.Wait()
close(done)
}()
if ctx != nil {
select {
case <-ctx.Done():
case <-done:
}
}
c.cancel()
<-done
}
func (c *RedCron) run(cp cronProperties, f func(context.Context)) {
c.wg.Add(1)
defer c.wg.Done()
for c.ctx.Err() == nil && c.stopped == 0 {
var tm time.Time
select {
case <-c.ctx.Done():
return
case tm = <-time.After(getDriftInterval()):
}
if (tm.Unix()-int64(cp.offsetSec))%int64(cp.repeatSec) != 0 {
continue
}
cont := false
func() {
rctx, rcancel := context.WithTimeout(context.Background(), opTimeout)
defer rcancel()
if !c.setNX(rctx, cp, tm) {
cont = true
}
}()
if cont {
continue
}
func() {
fctx, fcancel := context.WithCancel(c.ctx)
defer fcancel()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
tkr := time.NewTicker(tickerInterval)
defer tkr.Stop()
for fctx.Err() == nil {
select {
case <-fctx.Done():
return
case <-tkr.C:
var ok bool
func() {
rctx, rcancel := context.WithTimeout(context.Background(), opTimeout)
defer rcancel()
ok = c.set(rctx, cp, tm)
}()
if !ok {
fcancel()
return
}
}
}
}()
f(fctx)
fcancel()
wg.Wait()
func() {
rctx, rcancel := context.WithTimeout(context.Background(), opTimeout)
defer rcancel()
c.del(rctx, cp)
}()
}()
}
}
func (c *RedCron) set(ctx context.Context, cp cronProperties, tm time.Time) (ok bool) {
cmd := c.cfg.Client.Set(ctx, genKey(cp), genVal(cp, tm), waitDur)
if e := cmd.Err(); e != nil {
c.cfg.performError(e, cp)
return false
}
return true
}
func (c *RedCron) setNX(ctx context.Context, cp cronProperties, tm time.Time) (ok bool) {
var cmd *redis.BoolCmd
cmd = c.cfg.Client.SetNX(ctx, genTmKey(cp, tm), genVal(cp, tm), waitDur+time.Duration(cp.repeatSec)*time.Second)
if e := cmd.Err(); e != nil {
c.cfg.performError(e, cp)
return false
}
if !cmd.Val() {
return false
}
cmd = c.cfg.Client.SetNX(ctx, genKey(cp), genVal(cp, tm), waitDur)
if e := cmd.Err(); e != nil {
c.cfg.performError(e, cp)
return false
}
if !cmd.Val() {
return false
}
return true
}
func (c *RedCron) del(ctx context.Context, cp cronProperties) (ok bool) {
cmd := c.cfg.Client.Del(ctx, genKey(cp))
if e := cmd.Err(); e != nil {
c.cfg.performError(e, cp)
return false
}
return cmd.Val() >= 1
}
type cronProperties struct {
name string
repeatSec int
offsetSec int
}
func getDriftInterval() time.Duration {
return time.Second/32 + time.Duration(rand.Int63n(int64(time.Second)/4))
}
func genKey(cp cronProperties) string {
return fmt.Sprintf("%q", cp.name)
}
func genTmKey(cp cronProperties, tm time.Time) string {
return fmt.Sprintf("%q %d", cp.name, tm.Unix())
}
func genVal(cp cronProperties, tm time.Time) string {
result := &struct {
Time time.Time
Name string
RepeatSec int
OffsetSec int
}{
Time: tm.UTC(),
Name: cp.name,
RepeatSec: cp.repeatSec,
OffsetSec: cp.offsetSec,
}
b, _ := json.Marshal(result)
return string(b)
}
const (
opTimeout = 5 * time.Second
tickerInterval = 1 * time.Second
waitDur = 1 * time.Minute
)