-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
57 lines (47 loc) · 1.37 KB
/
export.go
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
package fluffle
import (
"encoding/json"
"errors"
"net/http"
"github.com/rabbitmq/amqp091-go"
)
type IMQ interface {
Publish(bty []byte) error
PublishIdempotent(idempotencyKey, idempotencyValue string, bty []byte) error
Consume() <-chan amqp091.Delivery
Retry(delivery amqp091.Delivery)
}
func Start(externalConfig RabbitConfig, externalLogger iLogger) {
logger = externalLogger
config = externalConfig
connectionsChan := make(chan *connection)
go newConnection(connectionsChan)
connectionPool = connectionsChan
poolStarted = true
}
func New(name string, prefetch int) IMQ {
return new(name, prefetch)
}
type QueueStatus struct {
Name string `json:"name"`
Messages int `json:"messages"`
Error string `json:"error", omitempty`
}
// GetQueueStats gets stats API
func GetQueueStats(queueName string, rabbitConfig RabbitConfig) (qStatus QueueStatus, err error) {
req, _ := http.NewRequest("GET", getApiBaseUrl(rabbitConfig.Host, queueName), nil)
req.SetBasicAuth(rabbitConfig.UserName, rabbitConfig.Password)
res, err := http.DefaultClient.Do(req)
if err != nil {
return
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&qStatus)
if qStatus.Error != "" {
return qStatus, errors.New(qStatus.Error)
}
return
}
func getApiBaseUrl(hostName, queueName string) string {
return "https://" + hostName + ":15672" + "/api/queues/%2f/" + queueName
}