-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconsumer.go
249 lines (214 loc) · 6.92 KB
/
consumer.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
238
239
240
241
242
243
244
245
246
247
248
package pgq
import (
"log"
"database/sql"
"time"
"fmt"
_ "github.com/lib/pq"
)
type Consumer struct{
consumerName string
queueName string
connectString string
sleepInterval time.Duration
db *sql.DB
current_batch_id int64
eventHandler func(Event) error
batchHandler func([]Event) error
}
// Consumer name
func (self *Consumer) ConsumerName() string {
return self.consumerName
}
// Queue name
func (self *Consumer) QueueName() string {
return self.queueName
}
// Subconsumer name
func (self *Consumer) SubconsumerName() string {
return ""
}
// Set event handler
func (self *Consumer) EventHandler (handler func(Event) error) {
self.eventHandler = handler
}
// Set batch handler
func (self *Consumer) BatchHandler (handler func([]Event) error) {
self.batchHandler = handler
}
// Set sleep interval
func (self *Consumer) SleepInterval (interval time.Duration) {
self.sleepInterval = interval
}
// Current batch id
func (self *Consumer) BatchId () int64 {
return self.current_batch_id
}
// Initialise default properties
func (self *Consumer) init() {
if self.sleepInterval == 0 {
self.sleepInterval = 1*time.Second
}
db, err := sql.Open("postgres", self.connectString)
if err != nil {
log.Printf("Failed to initialize db connection %s: %v", self.connectString, err)
}
self.db = db
}
// Register consumer
func (self *Consumer) Subscribe() error {
var register_consumer int32
err := self.db.QueryRow(
"SELECT register_consumer FROM pgq.register_consumer($1, $2)",
self.queueName, self.consumerName).Scan(®ister_consumer)
if err != nil {
err := fmt.Errorf("Failed to register consumer %s on %s: %s", self.consumerName, self.queueName, err)
log.Println(err)
return err
}
if register_consumer == 1 {
log.Printf("Consumer %s registered on %s", self.consumerName, self.queueName)
} else if register_consumer == 0 {
log.Printf("Consumer %s on %s aready registered", self.consumerName, self.queueName)
}
return nil
}
// Unregister consumer
func (self *Consumer) Unsubscribe() error {
var unregister_consumer int32
err := self.db.QueryRow(
"SELECT unregister_consumer FROM pgq.unregister_consumer($1, $2)",
self.queueName, self.consumerName).Scan(&unregister_consumer)
if err != nil {
err := fmt.Errorf("Failed to unregister consumer %s from %s: %s", self.consumerName, self.queueName, err)
log.Println(err)
return err
}
log.Printf("%v of (sub)consumers %s unregistered from %s", unregister_consumer, self.consumerName, self.queueName)
return nil
}
// Get next batch
func (self *Consumer) nextBatch() ([]Event, error) {
// get new batch
var batch_id sql.NullInt64
err := self.db.QueryRow(
`SELECT next_batch FROM pgq.next_batch($1, $2)`,
self.queueName, self.consumerName).Scan(&batch_id)
if err != nil{
ret_err := fmt.Errorf("nextBatch error when getting next batch: %v", err)
return nil, ret_err
}
if !batch_id.Valid {
self.current_batch_id = 0
return nil, nil
} else {
self.current_batch_id = batch_id.Int64
}
// get batch events
rows, ev_err := self.db.Query(
`SELECT
ev_id,
ev_time,
ev_txid,
ev_retry,
ev_type,
ev_data,
ev_extra1,
ev_extra2,
ev_extra3,
ev_extra4
FROM pgq.get_batch_events($1)`, self.current_batch_id)
defer rows.Close()
if ev_err != nil {
ret_err := fmt.Errorf("nextBatch error when getting events from batch %s: %v", self.current_batch_id, ev_err)
return nil, ret_err
}
batch := []Event{}
for rows.Next() {
var event Event
rows_err := rows.Scan(
&event.Ev_id,
&event.Ev_time,
&event.Ev_txid,
&event.Ev_retry,
&event.Ev_type,
&event.Ev_data,
&event.Ev_extra1,
&event.Ev_extra2,
&event.Ev_extra3,
&event.Ev_extra4)
event.Consumer = self
batch = append(batch, event)
if rows_err != nil {
ret_err := fmt.Errorf("nextBatch error during row processing: %v", rows_err)
return nil, ret_err
}
}
rows_err := rows.Err()
if rows_err != nil {
ret_err := fmt.Errorf("nextBatch error during rows processing: %v", rows_err)
return nil, ret_err
}
return batch, nil
}
// Main working loop
func (self *Consumer) Work () error {
log.Printf("Consumer %s started", self.consumerName)
for {
batch, err := self.nextBatch()
if err != nil {
log.Println(err)
return err
}
if batch == nil {
time.Sleep(self.sleepInterval)
} else {
var err error
if self.batchHandler == nil {
err = self.defaultBatchHandler(batch)
} else {
err = self.batchHandler(batch)
}
if err != nil {
log.Println(err)
return err
}
}
close_err := self.CloseBatch()
if close_err != nil {
return close_err
}
}
return nil
}
// Default batch handler
func (self *Consumer) defaultBatchHandler(batch []Event) error {
for _, event := range batch{
var err error
if self.eventHandler == nil {
err = self.defaultEventHandler(event)
} else {
err =self.eventHandler(event)
}
if err != nil {
return err
}
}
return nil
}
// Close current batch
func (self *Consumer) CloseBatch() error {
if self.current_batch_id > 0 {
_, err := self.db.Exec("SELECT pgq.finish_batch($1)", self.current_batch_id)
if err != nil {
ret_err := fmt.Errorf("nextBatch error when closing batch %s: %v", self.current_batch_id, err)
return ret_err
}
}
return nil
}
// Default event handler
func (self *Consumer) defaultEventHandler(event Event) error {
log.Printf("%+v", event)
return nil
}