feat(rabbitmq): Add rabbitmq module
This commit is contained in:
66
modules/rabbit/rabbit.go
Normal file
66
modules/rabbit/rabbit.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package rabbit
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.ostiwe.com/ostiwe-com/status/modules/log"
|
||||
"git.ostiwe.com/ostiwe-com/status/pkg/rabbit"
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var Channel *amqp.Channel
|
||||
|
||||
func InitConnection() {
|
||||
log.Global.Put(log.RABBIT, logrus.New())
|
||||
log.Global.Put(log.QUEUE, logrus.New())
|
||||
|
||||
rConn, err := rabbit.NewConnection(rabbit.ConnectionArgs{
|
||||
Host: os.Getenv("RABBIT_HOST"),
|
||||
Password: os.Getenv("RABBIT_PASSWORD"),
|
||||
User: os.Getenv("RABBIT_USER"),
|
||||
Port: os.Getenv("RABBIT_PORT"),
|
||||
ReconnectInterval: time.Second * 5,
|
||||
Logger: log.Global.Get(log.RABBIT),
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
rConn.IsAlive()
|
||||
|
||||
if err = declareQueues(rConn); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
channel, err := rConn.Channel()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Channel = channel
|
||||
}
|
||||
|
||||
func declareQueues(conn *rabbit.Connection) error {
|
||||
channel, err := conn.Channel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = channel.QueueDeclare(
|
||||
"tasks",
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user