-
Notifications
You must be signed in to change notification settings - Fork 0
/
health_checker.go
100 lines (92 loc) · 2.51 KB
/
health_checker.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package ibmmq
import (
"context"
"github.com/ibm-messaging/mq-golang/v5/ibmmq"
"time"
)
type HealthChecker struct {
name string
queueManager *ibmmq.MQQueueManager
topic string
timeout time.Duration
queue *QueueConfig
auth *MQAuth
qObject *ibmmq.MQObject
}
func NewHealthCheckerByConfig(queue *QueueConfig, auth *MQAuth, topic string, options ...string) *HealthChecker {
var name string
if len(options) >= 1 {
name = options[0]
} else {
name = "ibmmq"
}
return NewIBMMQHealthCheckerByConfig(queue, auth, topic, name, 4*time.Second)
}
func NewHealthChecker(connection *ibmmq.MQQueueManager, topic string, options ...string) *HealthChecker {
var name string
if len(options) >= 1 {
name = options[0]
} else {
name = "ibmmq"
}
return NewHealthCheckerWithTimeout(connection, topic, name, 4*time.Second)
}
func NewHealthCheckerWithTimeout(connection *ibmmq.MQQueueManager, topic string, name string, timeouts ...time.Duration) *HealthChecker {
var timeout time.Duration
if len(timeouts) >= 1 {
timeout = timeouts[0]
} else {
timeout = 4 * time.Second
}
return &HealthChecker{name: name, queueManager: connection, topic: topic, timeout: timeout}
}
func NewIBMMQHealthCheckerByConfig(queue *QueueConfig, auth *MQAuth, topic string, name string, timeouts ...time.Duration) *HealthChecker {
var timeout time.Duration
if len(timeouts) >= 1 {
timeout = timeouts[0]
} else {
timeout = 4 * time.Second
}
return &HealthChecker{name: name, queue: queue, auth: auth, topic: topic, timeout: timeout}
}
func (s *HealthChecker) Name() string {
return s.name
}
func (s *HealthChecker) Check(ctx context.Context) (map[string]interface{}, error) {
res := make(map[string]interface{})
if s.queueManager == nil {
conn, err := NewQueueManagerByConfig(*s.queue, *s.auth)
if err != nil {
return res, err
}
s.queueManager = conn
}
sd := ibmmq.NewMQSD()
sd.Options = ibmmq.MQSO_CREATE |
ibmmq.MQSO_NON_DURABLE |
ibmmq.MQSO_MANAGED
sd.ObjectString = s.topic
if s.qObject == nil {
var qObject ibmmq.MQObject
s.qObject = &qObject
}
subscriptionObject, err := s.queueManager.Sub(sd, s.qObject)
if err != nil {
return res, err
}
err = subscriptionObject.Close(0)
if err != nil {
return res, err
}
return res, nil
}
func (s *HealthChecker) Build(ctx context.Context, data map[string]interface{}, err error) map[string]interface{} {
if err == nil {
return data
}
if data == nil {
return make(map[string]interface{}, 0)
}
data["error"] = err.Error()
return data
}