Repository navigation
Expand file tree
/
Copy pathperiodic_enqueuer.go
More file actions
187 lines (156 loc) · 4.47 KB
/
Copy pathperiodic_enqueuer.go
File metadata and controls
187 lines (156 loc) · 4.47 KB
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
package work
import (
"fmt"
"log/slog"
"math/rand"
"strconv"
"strings"
"time"
"github.com/gomodule/redigo/redis"
"github.com/robfig/cron/v3"
)
const (
periodicEnqueuerSleep = 2 * time.Minute
periodicEnqueuerHorizon = 4 * time.Minute
)
type periodicEnqueuer struct {
namespace string
pool Pool
periodicJobs []*periodicJob
scheduledPeriodicJobs []*scheduledPeriodicJob
stopChan chan struct{}
doneStoppingChan chan struct{}
logger StructuredLogger
}
type periodicJob struct {
jobName string
spec string
schedule cron.Schedule
}
type scheduledPeriodicJob struct {
scheduledAt time.Time
scheduledAtEpoch int64
*periodicJob
}
func newPeriodicEnqueuer(
namespace string,
pool Pool,
periodicJobs []*periodicJob,
logger StructuredLogger,
) *periodicEnqueuer {
return &periodicEnqueuer{
namespace: namespace,
pool: pool,
periodicJobs: periodicJobs,
stopChan: make(chan struct{}),
doneStoppingChan: make(chan struct{}),
logger: logger,
}
}
func (pe *periodicEnqueuer) start() {
go pe.loop()
}
func (pe *periodicEnqueuer) stop() {
pe.stopChan <- struct{}{}
<-pe.doneStoppingChan
}
func (pe *periodicEnqueuer) loop() {
// Begin reaping periodically
timer := time.NewTimer(periodicEnqueuerSleep + time.Duration(rand.Intn(30))*time.Second)
defer timer.Stop()
if pe.shouldEnqueue() {
err := pe.enqueue()
if err != nil {
pe.logger.Error("periodic_enqueuer.loop.enqueue", errAttr(err))
}
}
for {
select {
case <-pe.stopChan:
pe.doneStoppingChan <- struct{}{}
return
case t := <-timer.C:
timer.Reset(periodicEnqueuerSleep + time.Duration(rand.Intn(30))*time.Second)
shouldEnqueue := pe.shouldEnqueue()
pe.logger.Debug("periodic_enqueuer.loop",
slog.Time("enqueue_time", t),
slog.Bool("should_enqueue", shouldEnqueue))
if shouldEnqueue {
err := pe.enqueue()
if err != nil {
pe.logger.Error("periodic_enqueuer.loop.enqueue", errAttr(err))
}
}
}
}
}
func (pe *periodicEnqueuer) enqueue() error {
now := nowEpochSeconds()
nowTime := time.Unix(now, 0)
horizon := nowTime.Add(periodicEnqueuerHorizon)
conn := pe.pool.Get()
defer conn.Close()
for _, pj := range pe.periodicJobs {
for t := pj.schedule.Next(nowTime); t.Before(horizon); t = pj.schedule.Next(t) {
epoch := t.Unix()
id := makeUniquePeriodicID(pj.jobName, pj.spec, epoch)
job := &Job{
Name: pj.jobName,
ID: id,
// This is technically wrong, but this lets the bytes be
// identical for the same periodic job instance. If we don't do
// this, we'd need to use a different approach -- probably
// giving each periodic job its own history of the past 100
// periodic jobs, and only scheduling a job if it's not in the
// history.
EnqueuedAt: epoch,
Args: nil,
// Set the next activation time as the deadline for the current one.
StartingDeadline: pj.schedule.Next(t).Unix(),
}
pe.logger.Debug("periodic_enqueuer.enqueue",
slog.Time("job_scheduled_time", t),
slog.String("job_name", pj.jobName),
slog.String("job_id", id),
)
rawJSON, err := job.serialize()
if err != nil {
return err
}
_, err = conn.Do("ZADD", redisKeyScheduled(pe.namespace), epoch, rawJSON)
if err != nil {
return err
}
}
}
_, err := conn.Do("SET", redisKeyLastPeriodicEnqueue(pe.namespace), now)
return err
}
func (pe *periodicEnqueuer) shouldEnqueue() bool {
conn := pe.pool.Get()
defer conn.Close()
lastEnqueue, err := redis.Int64(conn.Do("GET", redisKeyLastPeriodicEnqueue(pe.namespace)))
if err == redis.ErrNil {
return true
} else if err != nil {
pe.logger.Error("periodic_enqueuer.should_enqueue", errAttr(err))
return true
}
return lastEnqueue < (nowEpochSeconds() - int64(periodicEnqueuerSleep/time.Second))
}
const periodicIDPrefix = "periodic:"
// parsePeriodicEpoch extracts the tick epoch from an ID like "periodic:<name>:<spec>:<epoch>".
// Job.EnqueuedAt is not suitable for this: the requeuer overwrites it with the time the job was moved to the queue.
func parsePeriodicEpoch(id string) (int64, bool) {
if !strings.HasPrefix(id, periodicIDPrefix) {
return 0, false
}
epoch, err := strconv.ParseInt(id[strings.LastIndexByte(id, ':')+1:], 10, 64)
if err != nil {
return 0, false
}
return epoch, true
}
func makeUniquePeriodicID(name, spec string, epoch int64) string {
return fmt.Sprintf("%s%s:%s:%d", periodicIDPrefix, name, spec, epoch)
}