-
Notifications
You must be signed in to change notification settings - Fork 35
/
scheduled.go
71 lines (55 loc) · 1.3 KB
/
scheduled.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
package workers
import (
"context"
"strings"
"time"
)
type scheduledWorker struct {
opts Options
done chan bool
}
func (s *scheduledWorker) run() {
for {
select {
case <-s.done:
return
default:
}
s.poll()
time.Sleep(s.opts.PollInterval)
}
}
func (s *scheduledWorker) quit() {
close(s.done)
}
func (s *scheduledWorker) poll() {
now := nowToSecondsWithNanoPrecision()
for {
rawMessage, err := s.opts.store.DequeueScheduledMessage(context.Background(), now)
if err != nil {
break
}
message, _ := NewMsg(rawMessage)
queue, _ := message.Get("queue").String()
queue = strings.TrimPrefix(queue, s.opts.Namespace)
message.Set("enqueued_at", nowToSecondsWithNanoPrecision())
s.opts.store.EnqueueMessageNow(context.Background(), queue, message.ToJson())
}
for {
rawMessage, err := s.opts.store.DequeueRetriedMessage(context.Background(), now)
if err != nil {
break
}
message, _ := NewMsg(rawMessage)
queue, _ := message.Get("queue").String()
queue = strings.TrimPrefix(queue, s.opts.Namespace)
message.Set("enqueued_at", nowToSecondsWithNanoPrecision())
s.opts.store.EnqueueMessageNow(context.Background(), queue, message.ToJson())
}
}
func newScheduledWorker(opts Options) *scheduledWorker {
return &scheduledWorker{
opts: opts,
done: make(chan bool),
}
}