67 lines
1.1 KiB
Go
67 lines
1.1 KiB
Go
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
|
|
}
|