fix: service hanging when error on startup has been encountered (#135)

This commit is contained in:
Mikhail Bragin 2021-10-18 13:29:26 +02:00 committed by GitHub
parent bef3b3392b
commit 74485d3b13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package cmd
import (
"github.com/cenkalti/backoff/v4"
"github.com/kardianos/service"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -9,11 +10,32 @@ import (
)
func (p *program) Start(s service.Service) error {
var backOff = &backoff.ExponentialBackOff{
InitialInterval: time.Second,
RandomizationFactor: backoff.DefaultRandomizationFactor,
Multiplier: backoff.DefaultMultiplier,
MaxInterval: 30 * time.Second,
MaxElapsedTime: 24 * 3 * time.Hour, //stop after 3 days trying
Stop: backoff.Stop,
Clock: backoff.SystemClock,
}
// Start should not block. Do the actual work async.
log.Info("starting service") //nolint
go func() {
operation := func() error {
err := runClient()
if err != nil {
log.Warnf("retrying Wiretrustee client app due to error: %v", err)
return err
}
return nil
}
err := backoff.Retry(operation, backOff)
if err != nil {
log.Errorf("exiting client retry loop due to unrecoverable error: %s", err)
return
}
}()
@ -21,7 +43,9 @@ func (p *program) Start(s service.Service) error {
}
func (p *program) Stop(s service.Service) error {
go func() {
stopCh <- 1
}()
select {
case <-cleanupCh:

View File

@ -45,7 +45,7 @@ func NewClient(ctx context.Context, addr string, ourPrivateKey wgtypes.Key, tlsE
}))
if err != nil {
log.Errorf("failed creating connection to Management Srvice %v", err)
log.Errorf("failed creating connection to Management Service %v", err)
return nil, err
}