88 lines
1.4 KiB
Go
88 lines
1.4 KiB
Go
package rabbit
|
|
|
|
import (
|
|
"context"
|
|
|
|
rmq "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/rabbitmqamqp"
|
|
)
|
|
|
|
type Manage struct {
|
|
m *rmq.AmqpManagement
|
|
xName string
|
|
x rmq.IExchangeSpecification
|
|
qName string
|
|
q rmq.IQueueSpecification
|
|
bPath string
|
|
b rmq.IBindingSpecification
|
|
}
|
|
|
|
func (m *Manage) Init(ctx context.Context, rm *rmq.AmqpManagement,
|
|
rx rmq.IExchangeSpecification, rq rmq.IQueueSpecification,
|
|
rb rmq.IBindingSpecification) {
|
|
|
|
m.m = rm
|
|
m.x = rx
|
|
m.q = rq
|
|
m.b = rb
|
|
}
|
|
|
|
func (m *Manage) DeclareExchangeQueue(ctx context.Context) error {
|
|
_, err := m.m.DeclareExchange(ctx, m.x)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = m.m.DeclareQueue(ctx, m.q)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *Manage) DeclareAndBind(ctx context.Context) error {
|
|
xinfo, err := m.m.DeclareExchange(ctx, m.x)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
m.xName = xinfo.Name()
|
|
|
|
qinfo, err := m.m.DeclareQueue(ctx, m.q)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
m.qName = qinfo.Name()
|
|
|
|
bPath, err := m.m.Bind(ctx, m.b)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
m.bPath = bPath
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *Manage) UnbindAndDelete(ctx context.Context) (purged int, err error) {
|
|
err = m.m.Unbind(ctx, m.bPath)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = m.m.DeleteExchange(ctx, m.xName)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
purged, err = m.m.PurgeQueue(ctx, m.qName)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = m.m.DeleteQueue(ctx, m.qName)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|