From 9c86f4db204b662316a0ef2c89810e4ceda628ce Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Fri, 14 May 2021 12:17:44 +0200 Subject: [PATCH] [Elastic Agent] Retry deamon reload during enroll (#25590) (#25718) [Elastic Agent] Retry deamon reload during enroll (#25590) --- .../elastic-agent/pkg/agent/cmd/enroll_cmd.go | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/x-pack/elastic-agent/pkg/agent/cmd/enroll_cmd.go b/x-pack/elastic-agent/pkg/agent/cmd/enroll_cmd.go index 4dd8be03ee41..9bb74d4d01a3 100644 --- a/x-pack/elastic-agent/pkg/agent/cmd/enroll_cmd.go +++ b/x-pack/elastic-agent/pkg/agent/cmd/enroll_cmd.go @@ -252,7 +252,7 @@ func (c *enrollCmd) fleetServerBootstrap(ctx context.Context) (string, error) { var agentSubproc <-chan *os.ProcessState if agentRunning { // reload the already running agent - err = c.daemonReload(ctx) + err = c.daemonReloadWithBackoff(ctx) if err != nil { return "", errors.New(err, "failed to trigger elastic-agent daemon reload", errors.TypeApplication) } @@ -322,6 +322,28 @@ func (c *enrollCmd) prepareFleetTLS() error { return nil } +func (c *enrollCmd) daemonReloadWithBackoff(ctx context.Context) error { + err := c.daemonReload(ctx) + if err == nil { + return nil + } + + signal := make(chan struct{}) + backExp := backoff.NewExpBackoff(signal, 10*time.Second, 1*time.Minute) + + for i := 5; i >= 0; i-- { + backExp.Wait() + c.log.Info("Retrying to restart...") + err = c.daemonReload(ctx) + if err == nil { + break + } + } + + close(signal) + return err +} + func (c *enrollCmd) daemonReload(ctx context.Context) error { daemon := client.New() err := daemon.Connect(ctx)