@@ -26,7 +26,6 @@ import (
2626
2727 "strings"
2828 "sync"
29- "sync/atomic"
3029 "time"
3130
3231 "github.com/ibm-messaging/mq-golang/v5/ibmmq"
@@ -37,17 +36,6 @@ import (
3736 log "github.com/sirupsen/logrus"
3837)
3938
40- // These are really booleans but I'm using atomic updates
41- // to avoid potential races (though they'd likely be harmless) between
42- // the main code and the callbacks
43- type status struct {
44- connectedOnce int32
45- connectedQMgr int32
46- collectorEnd int32
47- collectorSilent int32
48- firstCollection int32
49- }
50-
5139var (
5240 BuildStamp string
5341 GitCommit string
5947 collector prometheus.Collector
6048 mutex sync.RWMutex
6149 retryCount = 0 // Might use this with a maxRetry to force a quit out of collector
62- st status
6350)
6451
6552func main () {
@@ -76,28 +63,34 @@ func main() {
7663 if err != nil {
7764 log .Error (err )
7865 } else {
79- st . connectedOnce = 0
80- st . connectedQMgr = 0
81- st . collectorEnd = 0
82- st . firstCollection = 0
83- st . collectorSilent = 0
66+ setConnectedOnce ( false )
67+ setConnectedQMgr ( false )
68+ setCollectorEnd ( false )
69+ setFirstCollection ( false )
70+ setCollectorSilent ( false )
8471
8572 // Start the webserver in a separate thread
8673 go startServer ()
8774
8875 // This is the main loop that tries to keep the collector connected to a queue manager
8976 // even after a failure.
90- for atomic . LoadInt32 ( & st . collectorEnd ) == 0 {
91- log .Debugf ("In main loop: qMgrConnected=%d " , atomic . LoadInt32 ( & st . connectedQMgr ))
77+ for ! isCollectorEnd () {
78+ log .Debugf ("In main loop: qMgrConnected=%v " , isConnectedQMgr ( ))
9279 err = nil // Start clean on each loop
9380
9481 // The callback will set this flag to false if there's an error while
9582 // processing the messages.
96- if atomic . LoadInt32 ( & st . connectedQMgr ) == 0 {
83+ if ! isConnectedQMgr () {
9784 mutex .Lock ()
9885 if err == nil {
9986 mqmetric .EndConnection ()
100- // Connect and open standard queues
87+ // Connect and open standard queues. If we're going to manage reconnection from
88+ // this collector, then turn off the MQ client automatic option
89+ if config .keepRunning {
90+ config .cf .CC .SingleConnect = true
91+ } else {
92+ config .cf .CC .SingleConnect = false
93+ }
10194 err = mqmetric .InitConnection (config .cf .QMgrName , config .cf .ReplyQ , & config .cf .CC )
10295 if err == nil {
10396 log .Infoln ("Connected to queue manager " + config .cf .QMgrName )
@@ -157,26 +150,26 @@ func main() {
157150 allocateAllGauges ()
158151
159152 if collector != nil {
160- atomic . StoreInt32 ( & st . collectorSilent , 1 )
153+ setCollectorSilent ( true )
161154 prometheus .Unregister (collector )
162- atomic . StoreInt32 ( & st . collectorSilent , 0 )
155+ setCollectorSilent ( false )
163156 }
164157
165158 collector = newExporter ()
166- atomic . StoreInt32 ( & st . firstCollection , 1 )
159+ setFirstCollection ( true )
167160 prometheus .MustRegister (collector )
168- atomic . StoreInt32 ( & st . connectedQMgr , 1 )
161+ setConnectedQMgr ( true )
169162
170- if atomic . LoadInt32 ( & st . connectedOnce ) == 0 {
163+ if ! isConnectedOnce () {
171164 startChannel <- true
172- atomic . StoreInt32 ( & st . connectedOnce , 1 )
165+ setConnectedOnce ( true )
173166 }
174167 } else {
175- if atomic . LoadInt32 ( & st . connectedOnce ) == 0 || ! config .keepRunning {
168+ if ! isConnectedOnce () || ! config .keepRunning {
176169 // If we've never successfully connected, then exit instead
177170 // of retrying as it probably means a config error
178171 log .Errorf ("Connection to %s has failed. %v" , config .cf .QMgrName , err )
179- atomic . StoreInt32 ( & st . collectorEnd , 1 )
172+ setCollectorEnd ( true )
180173 } else {
181174 log .Debug ("Sleeping a bit after a failure" )
182175 retryCount ++
@@ -197,7 +190,6 @@ func main() {
197190 } else {
198191 os .Exit (0 )
199192 }
200-
201193}
202194
203195func startServer () {
@@ -268,7 +260,7 @@ func stopServer() {
268260 if err != nil {
269261 log .Errorf ("Failed to shutdown metrics server: %v" , err )
270262 }
271- atomic . StoreInt32 ( & st . collectorEnd , 1 )
263+ setCollectorEnd ( true )
272264}
273265
274266/*
0 commit comments