@@ -17,6 +17,10 @@ const (
1717 mqConsumerConcurrency = 1
1818)
1919
20+ const (
21+ consumerRetries = 10
22+ )
23+
2024// MQService
2125type MQService struct {
2226 logger hclog.Logger
@@ -61,6 +65,19 @@ func newMQService(logger hclog.Logger, config *MQConfig, datafeedService *DataFe
6165 return mq , nil
6266}
6367
68+ func (mq * MQService ) restartWithRetries (ctx context.Context ) (<- chan * proto.DataFeedReport , <- chan error , error ) {
69+ for i := 0 ; i < consumerRetries ; i ++ {
70+ mq .logger .Debug ("Restarting consumer with try" , i )
71+ time .Sleep (5 * time .Second )
72+ reports , errors , err := mq .startConsumer (ctx , mqConsumerConcurrency )
73+ if err == nil {
74+ return reports , errors , err
75+ }
76+ }
77+
78+ return nil , nil , fmt .Errorf ("failed to restart consumer after %d retries" , consumerRetries )
79+ }
80+
6481// startConsumeLoop
6582func (mq * MQService ) startConsumeLoop () {
6683 mq .logger .Debug ("listening for MQ messages..." )
@@ -80,19 +97,15 @@ func (mq *MQService) startConsumeLoop() {
8097 mq .datafeedService .queueReportingTx (ProposeOutcome , report .MarketHash , report .Outcome )
8198 case err = <- errors :
8299 mq .logger .Error ("error while consuming from message queue" , "err" , err )
83- mq .logger .Debug ("Restarting consumer..." )
84- time .Sleep (2 * time .Second )
85- reports , errors , err = mq .startConsumer (ctx , mqConsumerConcurrency )
100+ reports , errors , err = mq .restartWithRetries (ctx )
86101 if err != nil {
87- mq .logger .Error ("Got Error during consumer restart " , err )
102+ mq .logger .Error ("failed to start consumer - errors chan " , err )
88103 }
89104 case <- common .GetTerminationSignalCh ():
90105 mq .logger .Debug ("got sigterm, shuttown down mq consumer" )
91- mq .logger .Debug ("Restarting consumer..." )
92- time .Sleep (2 * time .Second )
93- reports , errors , err = mq .startConsumer (ctx , mqConsumerConcurrency )
106+ reports , errors , err = mq .restartWithRetries (ctx )
94107 if err != nil {
95- mq .logger .Error ("Got Error during consumer restart " , err )
108+ mq .logger .Error ("failed to start consumer - sigterm " , err )
96109 }
97110
98111 }
@@ -175,6 +188,14 @@ func (mq *MQService) startConsumer(
175188 }()
176189 }
177190
191+ go func () {
192+ notifyCloseError := <- mq .connection .Channel .NotifyClose (make (chan * amqp.Error ))
193+ if notifyCloseError != nil {
194+ mq .logger .Debug ("Got notifyCloseError error" )
195+ errors <- fmt .Errorf ("Connection closed: %v" , notifyCloseError )
196+ }
197+ }()
198+
178199 // stop the consumer upon sigterm
179200 go func () {
180201 <- ctx .Done ()
0 commit comments