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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
package amqp
import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/google/uuid"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/spiral/errors"
"github.com/spiral/roadrunner/v2/pkg/events"
priorityqueue "github.com/spiral/roadrunner/v2/pkg/priority_queue"
jobState "github.com/spiral/roadrunner/v2/pkg/state/job"
"github.com/spiral/roadrunner/v2/plugins/config"
"github.com/spiral/roadrunner/v2/plugins/jobs/job"
"github.com/spiral/roadrunner/v2/plugins/jobs/pipeline"
"github.com/spiral/roadrunner/v2/plugins/logger"
)
type JobConsumer struct {
sync.Mutex
log logger.Logger
pq priorityqueue.Queue
eh events.Handler
pipeline atomic.Value
// amqp connection
conn *amqp.Connection
consumeChan *amqp.Channel
publishChan chan *amqp.Channel
consumeID string
connStr string
retryTimeout time.Duration
//
// prefetch QoS AMQP
//
prefetch int
//
// pipeline's priority
//
priority int64
exchangeName string
queue string
exclusive bool
exchangeType string
routingKey string
multipleAck bool
requeueOnFail bool
delayCache map[string]struct{}
listeners uint32
stopCh chan struct{}
}
// NewAMQPConsumer initializes rabbitmq pipeline
func NewAMQPConsumer(configKey string, log logger.Logger, cfg config.Configurer, e events.Handler, pq priorityqueue.Queue) (*JobConsumer, error) {
const op = errors.Op("new_amqp_consumer")
// we need to obtain two parts of the amqp information here.
// firs part - address to connect, it is located in the global section under the amqp pluginName
// second part - queues and other pipeline information
// if no such key - error
if !cfg.Has(configKey) {
return nil, errors.E(op, errors.Errorf("no configuration by provided key: %s", configKey))
}
// if no global section
if !cfg.Has(pluginName) {
return nil, errors.E(op, errors.Str("no global amqp configuration, global configuration should contain amqp addrs"))
}
// PARSE CONFIGURATION START -------
var pipeCfg Config
var globalCfg GlobalCfg
err := cfg.UnmarshalKey(configKey, &pipeCfg)
if err != nil {
return nil, errors.E(op, err)
}
pipeCfg.InitDefault()
err = cfg.UnmarshalKey(pluginName, &globalCfg)
if err != nil {
return nil, errors.E(op, err)
}
globalCfg.InitDefault()
// PARSE CONFIGURATION END -------
jb := &JobConsumer{
log: log,
pq: pq,
eh: e,
consumeID: uuid.NewString(),
stopCh: make(chan struct{}),
// TODO to config
retryTimeout: time.Minute * 5,
delayCache: make(map[string]struct{}, 100),
priority: pipeCfg.Priority,
publishChan: make(chan *amqp.Channel, 1),
routingKey: pipeCfg.RoutingKey,
queue: pipeCfg.Queue,
exchangeType: pipeCfg.ExchangeType,
exchangeName: pipeCfg.Exchange,
prefetch: pipeCfg.Prefetch,
exclusive: pipeCfg.Exclusive,
multipleAck: pipeCfg.MultipleAck,
requeueOnFail: pipeCfg.RequeueOnFail,
}
jb.conn, err = amqp.Dial(globalCfg.Addr)
if err != nil {
return nil, errors.E(op, err)
}
// save address
jb.connStr = globalCfg.Addr
err = jb.initRabbitMQ()
if err != nil {
return nil, errors.E(op, err)
}
pch, err := jb.conn.Channel()
if err != nil {
return nil, errors.E(op, err)
}
jb.publishChan <- pch
// run redialer and requeue listener for the connection
jb.redialer()
return jb, nil
}
func FromPipeline(pipeline *pipeline.Pipeline, log logger.Logger, cfg config.Configurer, e events.Handler, pq priorityqueue.Queue) (*JobConsumer, error) {
const op = errors.Op("new_amqp_consumer_from_pipeline")
// we need to obtain two parts of the amqp information here.
// firs part - address to connect, it is located in the global section under the amqp pluginName
// second part - queues and other pipeline information
// only global section
if !cfg.Has(pluginName) {
return nil, errors.E(op, errors.Str("no global amqp configuration, global configuration should contain amqp addrs"))
}
// PARSE CONFIGURATION -------
var globalCfg GlobalCfg
err := cfg.UnmarshalKey(pluginName, &globalCfg)
if err != nil {
return nil, errors.E(op, err)
}
globalCfg.InitDefault()
// PARSE CONFIGURATION -------
jb := &JobConsumer{
log: log,
eh: e,
pq: pq,
consumeID: uuid.NewString(),
stopCh: make(chan struct{}),
retryTimeout: time.Minute * 5,
delayCache: make(map[string]struct{}, 100),
publishChan: make(chan *amqp.Channel, 1),
routingKey: pipeline.String(routingKey, ""),
queue: pipeline.String(queue, "default"),
exchangeType: pipeline.String(exchangeType, "direct"),
exchangeName: pipeline.String(exchangeKey, "amqp.default"),
prefetch: pipeline.Int(prefetch, 10),
priority: int64(pipeline.Int(priority, 10)),
exclusive: pipeline.Bool(exclusive, false),
multipleAck: pipeline.Bool(multipleAsk, false),
requeueOnFail: pipeline.Bool(requeueOnFail, false),
}
jb.conn, err = amqp.Dial(globalCfg.Addr)
if err != nil {
return nil, errors.E(op, err)
}
// save address
jb.connStr = globalCfg.Addr
err = jb.initRabbitMQ()
if err != nil {
return nil, errors.E(op, err)
}
pch, err := jb.conn.Channel()
if err != nil {
return nil, errors.E(op, err)
}
jb.publishChan <- pch
// register the pipeline
// error here is always nil
_ = jb.Register(context.Background(), pipeline)
// run redialer for the connection
jb.redialer()
return jb, nil
}
func (j *JobConsumer) Push(ctx context.Context, job *job.Job) error {
const op = errors.Op("rabbitmq_push")
// check if the pipeline registered
// load atomic value
pipe := j.pipeline.Load().(*pipeline.Pipeline)
if pipe.Name() != job.Options.Pipeline {
return errors.E(op, errors.Errorf("no such pipeline: %s, actual: %s", job.Options.Pipeline, pipe.Name()))
}
err := j.handleItem(ctx, fromJob(job))
if err != nil {
return errors.E(op, err)
}
return nil
}
// handleItem
func (j *JobConsumer) handleItem(ctx context.Context, msg *Item) error {
const op = errors.Op("rabbitmq_handle_item")
select {
case pch := <-j.publishChan:
// return the channel back
defer func() {
j.publishChan <- pch
}()
// convert
table, err := pack(msg.ID(), msg)
if err != nil {
return errors.E(op, err)
}
const op = errors.Op("amqp_handle_item")
// handle timeouts
if msg.Options.DelayDuration() > 0 {
// TODO declare separate method for this if condition
// TODO dlx cache channel??
delayMs := int64(msg.Options.DelayDuration().Seconds() * 1000)
tmpQ := fmt.Sprintf("delayed-%d.%s.%s", delayMs, j.exchangeName, j.queue)
_, err = pch.QueueDeclare(tmpQ, true, false, false, false, amqp.Table{
dlx: j.exchangeName,
dlxRoutingKey: j.routingKey,
dlxTTL: delayMs,
dlxExpires: delayMs * 2,
})
if err != nil {
return errors.E(op, err)
}
err = pch.QueueBind(tmpQ, tmpQ, j.exchangeName, false, nil)
if err != nil {
return errors.E(op, err)
}
// insert to the local, limited pipeline
err = pch.Publish(j.exchangeName, tmpQ, false, false, amqp.Publishing{
Headers: table,
ContentType: contentType,
Timestamp: time.Now().UTC(),
DeliveryMode: amqp.Persistent,
Body: msg.Body(),
})
if err != nil {
return errors.E(op, err)
}
j.delayCache[tmpQ] = struct{}{}
return nil
}
// insert to the local, limited pipeline
err = pch.Publish(j.exchangeName, j.routingKey, false, false, amqp.Publishing{
Headers: table,
ContentType: contentType,
Timestamp: time.Now(),
DeliveryMode: amqp.Persistent,
Body: msg.Body(),
})
if err != nil {
return errors.E(op, err)
}
return nil
case <-ctx.Done():
return errors.E(op, errors.TimeOut, ctx.Err())
}
}
func (j *JobConsumer) Register(_ context.Context, p *pipeline.Pipeline) error {
j.pipeline.Store(p)
return nil
}
func (j *JobConsumer) Run(_ context.Context, p *pipeline.Pipeline) error {
const op = errors.Op("rabbit_consume")
pipe := j.pipeline.Load().(*pipeline.Pipeline)
if pipe.Name() != p.Name() {
return errors.E(op, errors.Errorf("no such pipeline registered: %s", pipe.Name()))
}
// protect connection (redial)
j.Lock()
defer j.Unlock()
var err error
j.consumeChan, err = j.conn.Channel()
if err != nil {
return errors.E(op, err)
}
err = j.consumeChan.Qos(j.prefetch, 0, false)
if err != nil {
return errors.E(op, err)
}
// start reading messages from the channel
deliv, err := j.consumeChan.Consume(
j.queue,
j.consumeID,
false,
false,
false,
false,
nil,
)
if err != nil {
return errors.E(op, err)
}
// run listener
j.listener(deliv)
j.eh.Push(events.JobEvent{
Event: events.EventPipeActive,
Driver: pipe.Driver(),
Pipeline: pipe.Name(),
Start: time.Now(),
})
return nil
}
func (j *JobConsumer) State(ctx context.Context) (*jobState.State, error) {
const op = errors.Op("amqp_driver_state")
select {
case pch := <-j.publishChan:
defer func() {
j.publishChan <- pch
}()
q, err := pch.QueueInspect(j.queue)
if err != nil {
return nil, errors.E(op, err)
}
return &jobState.State{
Queue: q.Name,
Active: int64(q.Messages),
}, nil
case <-ctx.Done():
return nil, errors.E(op, errors.TimeOut, ctx.Err())
}
}
func (j *JobConsumer) Pause(_ context.Context, p string) {
pipe := j.pipeline.Load().(*pipeline.Pipeline)
if pipe.Name() != p {
j.log.Error("no such pipeline", "requested pause on: ", p)
}
l := atomic.LoadUint32(&j.listeners)
// no active listeners
if l == 0 {
j.log.Warn("no active listeners, nothing to pause")
return
}
atomic.AddUint32(&j.listeners, ^uint32(0))
// protect connection (redial)
j.Lock()
defer j.Unlock()
err := j.consumeChan.Cancel(j.consumeID, true)
if err != nil {
j.log.Error("cancel publish channel, forcing close", "error", err)
errCl := j.consumeChan.Close()
if errCl != nil {
j.log.Error("force close failed", "error", err)
return
}
return
}
j.eh.Push(events.JobEvent{
Event: events.EventPipePaused,
Driver: pipe.Driver(),
Pipeline: pipe.Name(),
Start: time.Now(),
})
}
func (j *JobConsumer) Resume(_ context.Context, p string) {
pipe := j.pipeline.Load().(*pipeline.Pipeline)
if pipe.Name() != p {
j.log.Error("no such pipeline", "requested resume on: ", p)
}
// protect connection (redial)
j.Lock()
defer j.Unlock()
l := atomic.LoadUint32(&j.listeners)
// no active listeners
if l == 1 {
j.log.Warn("amqp listener already in the active state")
return
}
var err error
j.consumeChan, err = j.conn.Channel()
if err != nil {
j.log.Error("create channel on rabbitmq connection", "error", err)
return
}
err = j.consumeChan.Qos(j.prefetch, 0, false)
if err != nil {
j.log.Error("qos set failed", "error", err)
return
}
// start reading messages from the channel
deliv, err := j.consumeChan.Consume(
j.queue,
j.consumeID,
false,
false,
false,
false,
nil,
)
if err != nil {
j.log.Error("consume operation failed", "error", err)
return
}
// run listener
j.listener(deliv)
// increase number of listeners
atomic.AddUint32(&j.listeners, 1)
j.eh.Push(events.JobEvent{
Event: events.EventPipeActive,
Driver: pipe.Driver(),
Pipeline: pipe.Name(),
Start: time.Now(),
})
}
func (j *JobConsumer) Stop(context.Context) error {
j.stopCh <- struct{}{}
pipe := j.pipeline.Load().(*pipeline.Pipeline)
j.eh.Push(events.JobEvent{
Event: events.EventPipeStopped,
Driver: pipe.Driver(),
Pipeline: pipe.Name(),
Start: time.Now(),
})
return nil
}
|