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.
This commit is contained in:
Lars Karlitski 2020-09-08 20:49:17 +02:00 committed by Tom Gundersen
parent b03e1254e9
commit 3bedd25087
6 changed files with 25 additions and 9 deletions

View file

@ -253,7 +253,7 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
fmt.Printf("Running next job\n") fmt.Printf("Running job %v\n", job.Id())
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
go WatchJob(ctx, job) go WatchJob(ctx, job)
@ -285,7 +285,7 @@ func main() {
// flag to indicate all error kinds. // flag to indicate all error kinds.
result.Success = false result.Success = false
} else { } else {
log.Printf(" 🎉 Job completed successfully") log.Printf(" 🎉 Job completed successfully: %v", job.Id())
status = common.IBFinished status = common.IBFinished
} }

View file

@ -46,9 +46,13 @@ paths:
type: string type: string
artifact_location: artifact_location:
type: string type: string
id:
type: string
format: uuid
required: required:
- manifest - manifest
- location - location
- id
operationId: RequestJob operationId: RequestJob
requestBody: requestBody:
content: content:

View file

@ -12,6 +12,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"github.com/google/uuid"
"github.com/osbuild/osbuild-composer/internal/common" "github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro" "github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/osbuild" "github.com/osbuild/osbuild-composer/internal/osbuild"
@ -25,6 +26,7 @@ type Client struct {
} }
type Job interface { type Job interface {
Id() uuid.UUID
OSBuildArgs() (distro.Manifest, []*target.Target, error) OSBuildArgs() (distro.Manifest, []*target.Target, error)
Update(status common.ImageBuildState, result *osbuild.Result) error Update(status common.ImageBuildState, result *osbuild.Result) error
Canceled() (bool, error) Canceled() (bool, error)
@ -33,6 +35,7 @@ type Job interface {
type job struct { type job struct {
requester *http.Client requester *http.Client
id uuid.UUID
manifest distro.Manifest manifest distro.Manifest
targets []*target.Target targets []*target.Target
location string location string
@ -114,6 +117,7 @@ func (c *Client) RequestJob() (Job, error) {
return &job{ return &job{
requester: c.requester, requester: c.requester,
id: jr.Id,
manifest: jr.Manifest, manifest: jr.Manifest,
targets: jr.Targets, targets: jr.Targets,
location: location.String(), location: location.String(),
@ -121,6 +125,10 @@ func (c *Client) RequestJob() (Job, error) {
}, nil }, nil
} }
func (j *job) Id() uuid.UUID {
return j.id
}
func (j *job) OSBuildArgs() (distro.Manifest, []*target.Target, error) { func (j *job) OSBuildArgs() (distro.Manifest, []*target.Target, error) {
return j.manifest, j.targets, nil return j.manifest, j.targets, nil
} }

View file

@ -1,6 +1,7 @@
package worker package worker
import ( import (
"github.com/google/uuid"
"github.com/osbuild/osbuild-composer/internal/common" "github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro" "github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/osbuild" "github.com/osbuild/osbuild-composer/internal/osbuild"
@ -33,6 +34,7 @@ type errorResponse struct {
} }
type requestJobResponse struct { type requestJobResponse struct {
Id uuid.UUID `json:"id"`
Manifest distro.Manifest `json:"manifest"` Manifest distro.Manifest `json:"manifest"`
Targets []*target.Target `json:"targets,omitempty"` Targets []*target.Target `json:"targets,omitempty"`
Location string `json:"location"` Location string `json:"location"`

View file

@ -169,19 +169,19 @@ func (s *Server) DeleteArtifacts(id uuid.UUID) error {
return os.RemoveAll(path.Join(s.artifactsDir, id.String())) 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() token := uuid.New()
var args OSBuildJob var args OSBuildJob
jobId, err := s.jobs.Dequeue(ctx, []string{"osbuild"}, &args) jobId, err := s.jobs.Dequeue(ctx, []string{"osbuild"}, &args)
if err != nil { if err != nil {
return uuid.Nil, nil, err return uuid.Nil, uuid.Nil, nil, err
} }
if s.artifactsDir != "" { if s.artifactsDir != "" {
err := os.MkdirAll(path.Join(s.artifactsDir, "tmp", token.String()), 0700) err := os.MkdirAll(path.Join(s.artifactsDir, "tmp", token.String()), 0700)
if err != nil { 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() defer s.runningMutex.Unlock()
s.running[token] = jobId s.running[token] = jobId
return token, &args, nil return token, jobId, &args, nil
} }
func (s *Server) RunningJob(token uuid.UUID) (uuid.UUID, error) { func (s *Server) RunningJob(token uuid.UUID) (uuid.UUID, error) {
@ -255,12 +255,13 @@ func (h *apiHandlers) RequestJob(ctx echo.Context) error {
return err return err
} }
token, jobArgs, err := h.server.RequestJob(ctx.Request().Context()) token, jobId, jobArgs, err := h.server.RequestJob(ctx.Request().Context())
if err != nil { if err != nil {
return err return err
} }
return ctx.JSON(http.StatusCreated, requestJobResponse{ return ctx.JSON(http.StatusCreated, requestJobResponse{
Id: jobId,
Manifest: jobArgs.Manifest, Manifest: jobArgs.Manifest,
Targets: jobArgs.Targets, Targets: jobArgs.Targets,
Location: fmt.Sprintf("/jobs/%v", token), Location: fmt.Sprintf("/jobs/%v", token),

View file

@ -68,7 +68,7 @@ func TestCreate(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
test.TestRoute(t, server, false, "POST", "/jobs", `{}`, http.StatusCreated, 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) { func TestCancel(t *testing.T) {
@ -90,8 +90,9 @@ func TestCancel(t *testing.T) {
jobId, err := server.Enqueue(manifest, nil) jobId, err := server.Enqueue(manifest, nil)
require.NoError(t, err) require.NoError(t, err)
token, _, err := server.RequestJob(context.Background()) token, j, _, err := server.RequestJob(context.Background())
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, jobId, j)
err = server.Cancel(jobId) err = server.Cancel(jobId)
require.NoError(t, err) require.NoError(t, err)