summaryrefslogtreecommitdiff
path: root/plugins/jobs/brokers/amqp/redial.go
blob: 16071b780e45a860edec3347932529432475ddd2 (plain)
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
101
102
103
104
105
106
package amqp

import (
	"fmt"

	"github.com/cenkalti/backoff/v4"
	"github.com/spiral/errors"
	"github.com/streadway/amqp"
)

// redialer used to redial to the rabbitmq in case of the connection interrupts
func (j *JobsConsumer) redialer() { //nolint:gocognit
	go func() {
		const op = errors.Op("rabbitmq_redial")

		for {
			select {
			case err := <-j.conn.NotifyClose(make(chan *amqp.Error)):
				if err == nil {
					return
				}

				j.Lock()

				j.log.Error("connection closed, reconnecting", "error", err)
				expb := backoff.NewExponentialBackOff()
				// set the retry timeout (minutes)
				expb.MaxElapsedTime = j.retryTimeout
				op := func() error {
					j.log.Warn("rabbitmq reconnecting, caused by", "error", err)
					var dialErr error
					j.conn, dialErr = amqp.Dial(j.connStr)
					if dialErr != nil {
						return fmt.Errorf("fail to dial server endpoint: %v", dialErr)
					}

					j.log.Info("rabbitmq dial succeed. trying to redeclare queues and subscribers")

					// re-init connection
					errInit := j.initRabbitMQ()
					if errInit != nil {
						j.log.Error("error while redialing", "error", errInit)
						return errInit
					}

					// redeclare consume channel
					var errConnCh error
					j.consumeChan, errConnCh = j.conn.Channel()
					if errConnCh != nil {
						return errors.E(op, errConnCh)
					}

					// redeclare publish channel
					var errPubCh error
					j.publishChan, errPubCh = j.conn.Channel()
					if errPubCh != nil {
						return errors.E(op, errPubCh)
					}

					// start reading messages from the channel
					deliv, err := j.consumeChan.Consume(
						j.queue,
						j.consumeID,
						false,
						false,
						false,
						false,
						nil,
					)
					if err != nil {
						return errors.E(op, err)
					}

					// restart listener
					j.listener(deliv)

					j.log.Info("queues and subscribers redeclare succeed")
					return nil
				}

				retryErr := backoff.Retry(op, expb)
				if retryErr != nil {
					j.Unlock()
					j.log.Error("backoff failed", "error", retryErr)
					return
				}

				j.Unlock()

			case <-j.stopCh:
				err := j.publishChan.Close()
				if err != nil {
					j.log.Error("publish channel close", "error", err)
				}
				err = j.consumeChan.Close()
				if err != nil {
					j.log.Error("consume channel close", "error", err)
				}
				err = j.conn.Close()
				if err != nil {
					j.log.Error("amqp connection close", "error", err)
				}
			}
		}
	}()
}