worker: make worker.Client job-agnostic

Move the fact that the worker is requesting jobs of type "osbuild" out
of the client library.

For one, require consumers to pass accepted job types to RequestJobs()
and allow querying for the job type with the new Type() function.

Also, make OSBuildArgs() and Update() generic, requiring to pass an
argument that matches the job type.
This commit is contained in:
Lars Karlitski 2020-11-01 17:57:54 +01:00
parent 07f21d089e
commit c15c17960b
2 changed files with 20 additions and 21 deletions

View file

@ -5,7 +5,6 @@ import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net"
@ -24,8 +23,9 @@ type Client struct {
type Job interface {
Id() uuid.UUID
OSBuildArgs() (*OSBuildJob, error)
Update(result *OSBuildJobResult) error
Type() string
Args(args interface{}) error
Update(result interface{}) error
Canceled() (bool, error)
UploadArtifact(name string, reader io.Reader) error
}
@ -81,7 +81,7 @@ func NewClientUnix(path string) *Client {
return &Client{server, requester}
}
func (c *Client) RequestJob() (Job, error) {
func (c *Client) RequestJob(types []string) (Job, error) {
url, err := c.server.Parse("jobs")
if err != nil {
// This only happens when "jobs" cannot be parsed.
@ -90,7 +90,7 @@ func (c *Client) RequestJob() (Job, error) {
var buf bytes.Buffer
err = json.NewEncoder(&buf).Encode(api.RequestJobJSONRequestBody{
Types: []string{"osbuild"},
Types: types,
Arch: common.CurrentArch(),
})
if err != nil {
@ -137,21 +137,19 @@ func (j *job) Id() uuid.UUID {
return j.id
}
func (j *job) OSBuildArgs() (*OSBuildJob, error) {
if j.jobType != "osbuild" {
return nil, errors.New("not an osbuild job")
}
var args OSBuildJob
err := json.Unmarshal(j.args, &args)
if err != nil {
return nil, fmt.Errorf("error parsing osbuild job arguments: %v", err)
}
return &args, nil
func (j *job) Type() string {
return j.jobType
}
func (j *job) Update(result *OSBuildJobResult) error {
func (j *job) Args(args interface{}) error {
err := json.Unmarshal(j.args, args)
if err != nil {
return fmt.Errorf("error parsing job arguments: %v", err)
}
return nil
}
func (j *job) Update(result interface{}) error {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(api.UpdateJobJSONRequestBody{
Result: result,