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)
}
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
}

View file

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

View file

@ -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
}

View file

@ -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"`

View file

@ -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),

View file

@ -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)