worker/api: introduce job tokens

Don't give out job ids to workers, but `tokens`, which serve as an
indirection. This way, restarting composer won't confuse it when a stray
worker returns a result for a job that was still running. Also,
artifacts are only moved to the final location once a job finishes.

This change breaks backwards compatibility, but we're not yet promising
a stable worker API to anyone.

This drops the transition tests in server_test.go. These don't make much
sense anymore, because there's only one allowed transition, from running
to finished. They heavily relied on job slot ids, which are not easily
accessible with the `TestRoute` API. Overall, adjusting this seemed like
too much work for their benefit.
This commit is contained in:
Lars Karlitski 2020-09-07 13:12:06 +02:00 committed by Tom Gundersen
parent 783a88d8cc
commit 26b36ba704
7 changed files with 319 additions and 322 deletions

View file

@ -23,12 +23,6 @@ type Client struct {
api *api.Client
}
type Job struct {
Id uuid.UUID
Manifest distro.Manifest
Targets []*target.Target
}
func NewClient(baseURL string, conf *tls.Config) (*Client, error) {
httpClient := http.Client{
Transport: &http.Transport{
@ -61,34 +55,30 @@ func NewClientUnix(path string) *Client {
return &Client{c}
}
func (c *Client) AddJob() (*Job, error) {
response, err := c.api.PostJob(context.Background(), api.PostJobJSONRequestBody{})
func (c *Client) RequestJob() (uuid.UUID, distro.Manifest, []*target.Target, error) {
response, err := c.api.RequestJob(context.Background(), api.RequestJobJSONRequestBody{})
if err != nil {
return nil, err
return uuid.Nil, nil, nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusCreated {
var er errorResponse
_ = json.NewDecoder(response.Body).Decode(&er)
return nil, fmt.Errorf("couldn't create job, got %d: %s", response.StatusCode, er.Message)
return uuid.Nil, nil, nil, fmt.Errorf("couldn't create job, got %d: %s", response.StatusCode, er.Message)
}
var jr addJobResponse
var jr requestJobResponse
err = json.NewDecoder(response.Body).Decode(&jr)
if err != nil {
return nil, err
return uuid.Nil, nil, nil, err
}
return &Job{
jr.Id,
jr.Manifest,
jr.Targets,
}, nil
return jr.Token, jr.Manifest, jr.Targets, nil
}
func (c *Client) JobCanceled(job *Job) bool {
response, err := c.api.GetJob(context.Background(), job.Id.String())
func (c *Client) JobCanceled(token uuid.UUID) bool {
response, err := c.api.GetJob(context.Background(), token.String())
if err != nil {
return true
}
@ -98,7 +88,7 @@ func (c *Client) JobCanceled(job *Job) bool {
return true
}
var jr jobResponse
var jr getJobResponse
err = json.NewDecoder(response.Body).Decode(&jr)
if err != nil {
return true
@ -107,8 +97,8 @@ func (c *Client) JobCanceled(job *Job) bool {
return jr.Canceled
}
func (c *Client) UpdateJob(job *Job, status common.ImageBuildState, result *osbuild.Result) error {
response, err := c.api.UpdateJob(context.Background(), job.Id.String(), api.UpdateJobJSONRequestBody{
func (c *Client) UpdateJob(token uuid.UUID, status common.ImageBuildState, result *osbuild.Result) error {
response, err := c.api.UpdateJob(context.Background(), token.String(), api.UpdateJobJSONRequestBody{
Result: result,
Status: status.ToString(),
})
@ -123,9 +113,9 @@ func (c *Client) UpdateJob(job *Job, status common.ImageBuildState, result *osbu
return nil
}
func (c *Client) UploadImage(job uuid.UUID, name string, reader io.Reader) error {
_, err := c.api.PostJobArtifactWithBody(context.Background(),
job.String(), name, "application/octet-stream", reader)
func (c *Client) UploadImage(token uuid.UUID, name string, reader io.Reader) error {
_, err := c.api.UploadJobArtifactWithBody(context.Background(),
token.String(), name, "application/octet-stream", reader)
return err
}