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
|
package boltjobs
import (
"bytes"
"encoding/gob"
"sync/atomic"
"time"
"github.com/spiral/roadrunner/v2/utils"
bolt "go.etcd.io/bbolt"
)
func (c *consumer) listener() {
tt := time.NewTicker(time.Millisecond)
defer tt.Stop()
for {
select {
case <-c.stopCh:
c.log.Info("boltdb listener stopped")
return
case <-tt.C:
if atomic.LoadUint64(c.active) > uint64(c.prefetch) {
time.Sleep(time.Second)
continue
}
tx, err := c.db.Begin(true)
if err != nil {
c.log.Error("failed to begin writable transaction, job will be read on the next attempt", "error", err)
continue
}
b := tx.Bucket(utils.AsBytes(PushBucket))
inQb := tx.Bucket(utils.AsBytes(InQueueBucket))
// get first item
k, v := b.Cursor().First()
if k == nil && v == nil {
_ = tx.Commit()
continue
}
buf := bytes.NewReader(v)
dec := gob.NewDecoder(buf)
item := &Item{}
err = dec.Decode(item)
if err != nil {
c.rollback(err, tx)
continue
}
err = inQb.Put(utils.AsBytes(item.ID()), v)
if err != nil {
c.rollback(err, tx)
continue
}
// delete key from the PushBucket
err = b.Delete(k)
if err != nil {
c.rollback(err, tx)
continue
}
err = tx.Commit()
if err != nil {
c.rollback(err, tx)
continue
}
// attach pointer to the DB
item.attachDB(c.db, c.active, c.delayed)
// as the last step, after commit, put the item into the PQ
c.pq.Insert(item)
}
}
}
func (c *consumer) delayedJobsListener() {
tt := time.NewTicker(time.Second)
defer tt.Stop()
// just some 90's
loc, err := time.LoadLocation("UTC")
if err != nil {
c.log.Error("failed to load location, delayed jobs won't work", "error", err)
return
}
var startDate = utils.AsBytes(time.Date(1990, 1, 1, 0, 0, 0, 0, loc).Format(time.RFC3339))
for {
select {
case <-c.stopCh:
c.log.Info("boltdb listener stopped")
return
case <-tt.C:
tx, err := c.db.Begin(true)
if err != nil {
c.log.Error("failed to begin writable transaction, job will be read on the next attempt", "error", err)
continue
}
delayB := tx.Bucket(utils.AsBytes(DelayBucket))
inQb := tx.Bucket(utils.AsBytes(InQueueBucket))
cursor := delayB.Cursor()
endDate := utils.AsBytes(time.Now().UTC().Format(time.RFC3339))
for k, v := cursor.Seek(startDate); k != nil && bytes.Compare(k, endDate) <= 0; k, v = cursor.Next() {
buf := bytes.NewReader(v)
dec := gob.NewDecoder(buf)
item := &Item{}
err = dec.Decode(item)
if err != nil {
c.rollback(err, tx)
continue
}
err = inQb.Put(utils.AsBytes(item.ID()), v)
if err != nil {
c.rollback(err, tx)
continue
}
// delete key from the PushBucket
err = delayB.Delete(k)
if err != nil {
c.rollback(err, tx)
continue
}
// attach pointer to the DB
item.attachDB(c.db, c.active, c.delayed)
// as the last step, after commit, put the item into the PQ
c.pq.Insert(item)
}
err = tx.Commit()
if err != nil {
c.rollback(err, tx)
continue
}
}
}
}
func (c *consumer) rollback(err error, tx *bolt.Tx) {
errR := tx.Rollback()
if errR != nil {
c.log.Error("transaction commit error, rollback failed", "error", err, "rollback error", errR)
}
c.log.Error("transaction commit error, rollback succeed", "error", err)
}
|