@@ -8,13 +8,18 @@ import (
88 "sync/atomic"
99 "time"
1010
11- "gopkg.in/src-d/go-errors.v1 "
11+ "gopkg.in/src-d/go-queue.v0 "
1212
1313 "github.com/jpillora/backoff"
1414 "github.com/streadway/amqp"
1515 log15 "gopkg.in/inconshreveable/log15.v2"
16+ "gopkg.in/src-d/go-errors.v1"
1617)
1718
19+ func init () {
20+ queue .Register ("amqp" , NewAMQPBroker )
21+ }
22+
1823var consumerSeq uint64
1924
2025var (
@@ -52,7 +57,7 @@ type connection interface {
5257}
5358
5459// NewAMQPBroker creates a new AMQPBroker.
55- func NewAMQPBroker (url string ) (Broker , error ) {
60+ func NewAMQPBroker (url string ) (queue. Broker , error ) {
5661 conn , err := amqp .Dial (url )
5762 if err != nil {
5863 return nil , ErrConnectionFailed .New (err )
@@ -75,7 +80,6 @@ func NewAMQPBroker(url string) (Broker, error) {
7580}
7681
7782func connect (url string ) (* amqp.Connection , * amqp.Channel ) {
78-
7983 var (
8084 conn * amqp.Connection
8185 ch * amqp.Channel
@@ -184,7 +188,7 @@ func (b *AMQPBroker) newBuriedQueue(mainQueueName string) (q amqp.Queue, rex str
184188}
185189
186190// Queue returns the queue with the given name.
187- func (b * AMQPBroker ) Queue (name string ) (Queue , error ) {
191+ func (b * AMQPBroker ) Queue (name string ) (queue. Queue , error ) {
188192 buriedQueue , rex , err := b .newBuriedQueue (name )
189193 if err != nil {
190194 return nil , err
@@ -199,7 +203,7 @@ func (b *AMQPBroker) Queue(name string) (Queue, error) {
199203 amqp.Table {
200204 "x-dead-letter-exchange" : rex ,
201205 "x-dead-letter-routing-key" : name ,
202- "x-max-priority" : uint8 (PriorityUrgent ),
206+ "x-max-priority" : uint8 (queue . PriorityUrgent ),
203207 },
204208 )
205209
@@ -233,9 +237,9 @@ type AMQPQueue struct {
233237}
234238
235239// Publish publishes the given Job to the Queue.
236- func (q * AMQPQueue ) Publish (j * Job ) error {
237- if j == nil || len ( j . raw ) == 0 {
238- return ErrEmptyJob .New ()
240+ func (q * AMQPQueue ) Publish (j * queue. Job ) error {
241+ if j == nil || j . Size ( ) == 0 {
242+ return queue . ErrEmptyJob .New ()
239243 }
240244
241245 headers := amqp.Table {}
@@ -257,18 +261,18 @@ func (q *AMQPQueue) Publish(j *Job) error {
257261 MessageId : j .ID ,
258262 Priority : uint8 (j .Priority ),
259263 Timestamp : j .Timestamp ,
260- ContentType : string ( j . contentType ) ,
261- Body : j .raw ,
264+ ContentType : j . ContentType ,
265+ Body : j .Raw ,
262266 Headers : headers ,
263267 },
264268 )
265269}
266270
267271// PublishDelayed publishes the given Job with a given delay. Delayed messages
268272// wont go into the buried queue if they fail.
269- func (q * AMQPQueue ) PublishDelayed (j * Job , delay time.Duration ) error {
270- if j == nil || len ( j . raw ) == 0 {
271- return ErrEmptyJob .New ()
273+ func (q * AMQPQueue ) PublishDelayed (j * queue. Job , delay time.Duration ) error {
274+ if j == nil || j . Size ( ) == 0 {
275+ return queue . ErrEmptyJob .New ()
272276 }
273277
274278 ttl := delay / time .Millisecond
@@ -283,7 +287,7 @@ func (q *AMQPQueue) PublishDelayed(j *Job, delay time.Duration) error {
283287 "x-dead-letter-routing-key" : q .queue .Name ,
284288 "x-message-ttl" : int64 (ttl ),
285289 "x-expires" : int64 (ttl ) * 2 ,
286- "x-max-priority" : uint8 (PriorityUrgent ),
290+ "x-max-priority" : uint8 (queue . PriorityUrgent ),
287291 },
288292 )
289293 if err != nil {
@@ -300,20 +304,20 @@ func (q *AMQPQueue) PublishDelayed(j *Job, delay time.Duration) error {
300304 MessageId : j .ID ,
301305 Priority : uint8 (j .Priority ),
302306 Timestamp : j .Timestamp ,
303- ContentType : string ( j . contentType ) ,
304- Body : j .raw ,
307+ ContentType : j . ContentType ,
308+ Body : j .Raw ,
305309 },
306310 )
307311}
308312
309313type jobErr struct {
310- job * Job
314+ job * queue. Job
311315 err error
312316}
313317
314318// RepublishBuried will republish in the main queue those jobs that timed out without Ack
315319// or were Rejected with requeue = False and makes comply return true.
316- func (q * AMQPQueue ) RepublishBuried (conditions ... RepublishConditionFunc ) error {
320+ func (q * AMQPQueue ) RepublishBuried (conditions ... queue. RepublishConditionFunc ) error {
317321 if q .buriedQueue == nil {
318322 return fmt .Errorf ("buriedQueue is nil, called RepublishBuried on the internal buried queue?" )
319323 }
@@ -327,7 +331,7 @@ func (q *AMQPQueue) RepublishBuried(conditions ...RepublishConditionFunc) error
327331 defer iter .Close ()
328332
329333 retries := 0
330- var notComplying []* Job
334+ var notComplying []* queue. Job
331335 var errorsPublishing []* jobErr
332336 for {
333337 j , err := iter .(* AMQPJobIter ).nextNonBlocking ()
@@ -355,7 +359,7 @@ func (q *AMQPQueue) RepublishBuried(conditions ...RepublishConditionFunc) error
355359 return err
356360 }
357361
358- if republishConditions (conditions ).comply (j ) {
362+ if queue . RepublishConditions (conditions ).Comply (j ) {
359363 if err = q .Publish (j ); err != nil {
360364 errorsPublishing = append (errorsPublishing , & jobErr {j , err })
361365 }
@@ -391,7 +395,7 @@ func (q *AMQPQueue) handleRepublishErrors(list []*jobErr) error {
391395}
392396
393397// Transaction executes the given callback inside a transaction.
394- func (q * AMQPQueue ) Transaction (txcb TxCallback ) error {
398+ func (q * AMQPQueue ) Transaction (txcb queue. TxCallback ) error {
395399 ch , err := q .conn .connection ().Channel ()
396400 if err != nil {
397401 return ErrOpenChannel .New (err )
@@ -425,7 +429,7 @@ func (q *AMQPQueue) Transaction(txcb TxCallback) error {
425429
426430// Implements Queue. The advertisedWindow value will be the exact
427431// number of undelivered jobs in transit, not just the minium.
428- func (q * AMQPQueue ) Consume (advertisedWindow int ) (JobIter , error ) {
432+ func (q * AMQPQueue ) Consume (advertisedWindow int ) (queue. JobIter , error ) {
429433 ch , err := q .conn .connection ().Channel ()
430434 if err != nil {
431435 return nil , ErrOpenChannel .New (err )
@@ -470,20 +474,20 @@ type AMQPJobIter struct {
470474}
471475
472476// Next returns the next job in the iter.
473- func (i * AMQPJobIter ) Next () (* Job , error ) {
477+ func (i * AMQPJobIter ) Next () (* queue. Job , error ) {
474478 d , ok := <- i .c
475479 if ! ok {
476- return nil , ErrAlreadyClosed .New ()
480+ return nil , queue . ErrAlreadyClosed .New ()
477481 }
478482
479483 return fromDelivery (& d )
480484}
481485
482- func (i * AMQPJobIter ) nextNonBlocking () (* Job , error ) {
486+ func (i * AMQPJobIter ) nextNonBlocking () (* queue. Job , error ) {
483487 select {
484488 case d , ok := <- i .c :
485489 if ! ok {
486- return nil , ErrAlreadyClosed .New ()
490+ return nil , queue . ErrAlreadyClosed .New ()
487491 }
488492
489493 return fromDelivery (& d )
@@ -518,19 +522,18 @@ func (a *AMQPAcknowledger) Reject(requeue bool) error {
518522 return a .ack .Reject (a .id , requeue )
519523}
520524
521- func fromDelivery (d * amqp.Delivery ) (* Job , error ) {
522- j , err := NewJob ()
525+ func fromDelivery (d * amqp.Delivery ) (* queue. Job , error ) {
526+ j , err := queue . NewJob ()
523527 if err != nil {
524528 return nil , err
525529 }
526530
527531 j .ID = d .MessageId
528- j .Priority = Priority (d .Priority )
532+ j .Priority = queue . Priority (d .Priority )
529533 j .Timestamp = d .Timestamp
530- j .contentType = contentType (d .ContentType )
531- j .acknowledger = & AMQPAcknowledger {d .Acknowledger , d .DeliveryTag }
532- j .tag = d .DeliveryTag
533- j .raw = d .Body
534+ j .ContentType = d .ContentType
535+ j .Acknowledger = & AMQPAcknowledger {d .Acknowledger , d .DeliveryTag }
536+ j .Raw = d .Body
534537
535538 if retries , ok := d .Headers [retriesHeader ]; ok {
536539 retries , ok := retries .(int32 )
0 commit comments