osbuild-worker: fix "crashing" on worker registration issues

When the osbuild worker cannot register itself with the server
on startup the worker will "crash". This is inconsistent with the
existing behavior in `workerHeartbeat()` which deals with connectivity
or other server issue gracefully and retries periodically.

To unify the behavior this commit changes the behavior and only
issues a `logrus.Warnf` instead of the previous `Falalf` when
the registration fails.

Co-authored-by: Florian Schüller <florian.schueller@redhat.com>
This commit is contained in:
Michael Vogt 2024-09-10 10:04:08 +02:00
parent 751ad6af69
commit 3df26ed79c
2 changed files with 29 additions and 1 deletions

View file

@ -123,7 +123,8 @@ func NewClient(conf ClientConfig) (*Client, error) {
}
err = client.registerWorker()
if err != nil {
return client, err
// workerHeartbeat below will periodically retry to register
logrus.Warnf("Error registering worker on startup, %v. Trying again later…", err)
}
go client.workerHeartbeat()
return client, nil

View file

@ -1,12 +1,14 @@
package worker_test
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"github.com/osbuild/osbuild-composer/internal/jobqueue/fsjobqueue"
@ -137,3 +139,28 @@ func TestProxy(t *testing.T) {
// - cancel
require.Equal(t, 6, proxy.calls)
}
func TestNewClientWorkerNoErrorOnRegisterWorkerFailure(t *testing.T) {
logrusOutput := bytes.NewBuffer(nil)
logrus.SetOutput(logrusOutput)
apiCalls := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
apiCalls++
require.Equal(t, "/api/image-builder-worker/v1/workers", req.URL.Path)
w.WriteHeader(400)
_, err := w.Write([]byte(`{"reason":"reason", "details": "details"}`))
require.NoError(t, err)
}))
defer srv.Close()
client, err := worker.NewClient(worker.ClientConfig{
BaseURL: srv.URL,
BasePath: "/api/image-builder-worker/v1",
})
require.NoError(t, err)
require.NotNil(t, client)
require.Equal(t, 1, apiCalls)
require.Contains(t, logrusOutput.String(), `Error registering worker on startup, error registering worker: 400`)
}