From 3bedd2508786ba62f08f95e339dcc0a27a0b446c Mon Sep 17 00:00:00 2001 From: Lars Karlitski Date: Tue, 8 Sep 2020 20:49:17 +0200 Subject: [PATCH] worker/api: send job id to worker after all Full circle. After switching the worker to not operate on jobs directly, send the id anyway, so that workers can print it in their logs. --- cmd/osbuild-worker/main.go | 4 ++-- internal/worker/api/openapi.yml | 4 ++++ internal/worker/client.go | 8 ++++++++ internal/worker/json.go | 2 ++ internal/worker/server.go | 11 ++++++----- internal/worker/server_test.go | 5 +++-- 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/cmd/osbuild-worker/main.go b/cmd/osbuild-worker/main.go index 06eb30570..909fdcdc6 100644 --- a/cmd/osbuild-worker/main.go +++ b/cmd/osbuild-worker/main.go @@ -253,7 +253,7 @@ func main() { log.Fatal(err) } - fmt.Printf("Running next job\n") + fmt.Printf("Running job %v\n", job.Id()) ctx, cancel := context.WithCancel(context.Background()) go WatchJob(ctx, job) @@ -285,7 +285,7 @@ func main() { // flag to indicate all error kinds. result.Success = false } else { - log.Printf(" 🎉 Job completed successfully") + log.Printf(" 🎉 Job completed successfully: %v", job.Id()) status = common.IBFinished } diff --git a/internal/worker/api/openapi.yml b/internal/worker/api/openapi.yml index 96ce9af28..8f208fba5 100644 --- a/internal/worker/api/openapi.yml +++ b/internal/worker/api/openapi.yml @@ -46,9 +46,13 @@ paths: type: string artifact_location: type: string + id: + type: string + format: uuid required: - manifest - location + - id operationId: RequestJob requestBody: content: diff --git a/internal/worker/client.go b/internal/worker/client.go index 2a61b626e..824ddaee7 100644 --- a/internal/worker/client.go +++ b/internal/worker/client.go @@ -12,6 +12,7 @@ import ( "net/http" "net/url" + "github.com/google/uuid" "github.com/osbuild/osbuild-composer/internal/common" "github.com/osbuild/osbuild-composer/internal/distro" "github.com/osbuild/osbuild-composer/internal/osbuild" @@ -25,6 +26,7 @@ type Client struct { } type Job interface { + Id() uuid.UUID OSBuildArgs() (distro.Manifest, []*target.Target, error) Update(status common.ImageBuildState, result *osbuild.Result) error Canceled() (bool, error) @@ -33,6 +35,7 @@ type Job interface { type job struct { requester *http.Client + id uuid.UUID manifest distro.Manifest targets []*target.Target location string @@ -114,6 +117,7 @@ func (c *Client) RequestJob() (Job, error) { return &job{ requester: c.requester, + id: jr.Id, manifest: jr.Manifest, targets: jr.Targets, location: location.String(), @@ -121,6 +125,10 @@ func (c *Client) RequestJob() (Job, error) { }, nil } +func (j *job) Id() uuid.UUID { + return j.id +} + func (j *job) OSBuildArgs() (distro.Manifest, []*target.Target, error) { return j.manifest, j.targets, nil } diff --git a/internal/worker/json.go b/internal/worker/json.go index 06aadd943..5e50feb22 100644 --- a/internal/worker/json.go +++ b/internal/worker/json.go @@ -1,6 +1,7 @@ package worker import ( + "github.com/google/uuid" "github.com/osbuild/osbuild-composer/internal/common" "github.com/osbuild/osbuild-composer/internal/distro" "github.com/osbuild/osbuild-composer/internal/osbuild" @@ -33,6 +34,7 @@ type errorResponse struct { } type requestJobResponse struct { + Id uuid.UUID `json:"id"` Manifest distro.Manifest `json:"manifest"` Targets []*target.Target `json:"targets,omitempty"` Location string `json:"location"` diff --git a/internal/worker/server.go b/internal/worker/server.go index 285611480..9378f39bb 100644 --- a/internal/worker/server.go +++ b/internal/worker/server.go @@ -169,19 +169,19 @@ func (s *Server) DeleteArtifacts(id uuid.UUID) error { return os.RemoveAll(path.Join(s.artifactsDir, id.String())) } -func (s *Server) RequestJob(ctx context.Context) (uuid.UUID, *OSBuildJob, error) { +func (s *Server) RequestJob(ctx context.Context) (uuid.UUID, uuid.UUID, *OSBuildJob, error) { token := uuid.New() var args OSBuildJob jobId, err := s.jobs.Dequeue(ctx, []string{"osbuild"}, &args) if err != nil { - return uuid.Nil, nil, err + return uuid.Nil, uuid.Nil, nil, err } if s.artifactsDir != "" { err := os.MkdirAll(path.Join(s.artifactsDir, "tmp", token.String()), 0700) if err != nil { - return uuid.Nil, nil, fmt.Errorf("cannot create artifact directory: %v", err) + return uuid.Nil, uuid.Nil, nil, fmt.Errorf("cannot create artifact directory: %v", err) } } @@ -189,7 +189,7 @@ func (s *Server) RequestJob(ctx context.Context) (uuid.UUID, *OSBuildJob, error) defer s.runningMutex.Unlock() s.running[token] = jobId - return token, &args, nil + return token, jobId, &args, nil } func (s *Server) RunningJob(token uuid.UUID) (uuid.UUID, error) { @@ -255,12 +255,13 @@ func (h *apiHandlers) RequestJob(ctx echo.Context) error { return err } - token, jobArgs, err := h.server.RequestJob(ctx.Request().Context()) + token, jobId, jobArgs, err := h.server.RequestJob(ctx.Request().Context()) if err != nil { return err } return ctx.JSON(http.StatusCreated, requestJobResponse{ + Id: jobId, Manifest: jobArgs.Manifest, Targets: jobArgs.Targets, Location: fmt.Sprintf("/jobs/%v", token), diff --git a/internal/worker/server_test.go b/internal/worker/server_test.go index eb0969cc8..ee3200613 100644 --- a/internal/worker/server_test.go +++ b/internal/worker/server_test.go @@ -68,7 +68,7 @@ func TestCreate(t *testing.T) { require.NoError(t, err) test.TestRoute(t, server, false, "POST", "/jobs", `{}`, http.StatusCreated, - `{"manifest":{"sources":{},"pipeline":{}}}`, "location", "artifact_location", "created") + `{"manifest":{"sources":{},"pipeline":{}}}`, "id", "location", "artifact_location") } func TestCancel(t *testing.T) { @@ -90,8 +90,9 @@ func TestCancel(t *testing.T) { jobId, err := server.Enqueue(manifest, nil) require.NoError(t, err) - token, _, err := server.RequestJob(context.Background()) + token, j, _, err := server.RequestJob(context.Background()) require.NoError(t, err) + require.Equal(t, jobId, j) err = server.Cancel(jobId) require.NoError(t, err)