summaryrefslogtreecommitdiff
path: root/plugins/jobs/drivers/beanstalk/redial.go
blob: 784337ad49fb283ed88bfef9c09c09f2af15e541 (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
package beanstalk

import (
	"sync/atomic"
	"time"

	"github.com/cenkalti/backoff/v4"
)

func (j *JobConsumer) redial() {
	for range j.reconnectCh {
		// backoff here
		expb := backoff.NewExponentialBackOff()
		// set the retry timeout (minutes)
		expb.MaxElapsedTime = time.Minute * 5

		op := func() error {
			err := j.pool.Redial()
			if err != nil {
				return err
			}

			j.log.Info("beanstalk redial was successful")
			// reassign a pool
			return nil
		}

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

		// restart listener
		if atomic.LoadUint32(&j.listeners) == 1 {
			// stop previous listener
			j.stopCh <- struct{}{}
			go j.listen()
		}
	}
}