worker: move Job type to the jobqueue package

The main purpose of this is to share the structs between the server
and the client, and let the compiler ensure that our marshaling and
unmarshaling matches.

In the future we also want to make it easier to write unittests for
this code.

Signed-off-by: Tom Gundersen <teg@jklm.no>
This commit is contained in:
Tom Gundersen 2019-10-29 14:04:51 +01:00 committed by Lars Karlitski
parent fabd40da1c
commit aa404dcb99
3 changed files with 16 additions and 23 deletions

View file

@ -8,6 +8,8 @@ import (
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
"github.com/osbuild/osbuild-composer/internal/jobqueue"
) )
type ComposerClient struct { type ComposerClient struct {
@ -25,7 +27,7 @@ func NewClient() *ComposerClient {
return &ComposerClient{client} return &ComposerClient{client}
} }
func (c *ComposerClient) AddJob() (*Job, error) { func (c *ComposerClient) AddJob() (*jobqueue.Job, error) {
type request struct { type request struct {
} }
@ -41,7 +43,7 @@ func (c *ComposerClient) AddJob() (*Job, error) {
return nil, errors.New("couldn't create job") return nil, errors.New("couldn't create job")
} }
job := &Job{} job := &jobqueue.Job{}
err = json.NewDecoder(response.Body).Decode(job) err = json.NewDecoder(response.Body).Decode(job)
if err != nil { if err != nil {
return nil, err return nil, err
@ -50,13 +52,9 @@ func (c *ComposerClient) AddJob() (*Job, error) {
return job, nil return job, nil
} }
func (c *ComposerClient) UpdateJob(job *Job, status string) error { func (c *ComposerClient) UpdateJob(job *jobqueue.Job, status string) error {
type request struct {
Status string `json:"status"`
}
var b bytes.Buffer var b bytes.Buffer
json.NewEncoder(&b).Encode(&request{status}) json.NewEncoder(&b).Encode(&jobqueue.JobStatus{status})
req, err := http.NewRequest("PATCH", "http://localhost/job-queue/v1/jobs/"+job.ID.String(), &b) req, err := http.NewRequest("PATCH", "http://localhost/job-queue/v1/jobs/"+job.ID.String(), &b)
if err != nil { if err != nil {
return err return err

View file

@ -6,9 +6,7 @@ import (
"net" "net"
"net/http" "net/http"
"github.com/osbuild/osbuild-composer/internal/pipeline"
"github.com/osbuild/osbuild-composer/internal/store" "github.com/osbuild/osbuild-composer/internal/store"
"github.com/osbuild/osbuild-composer/internal/target"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
@ -77,11 +75,7 @@ func statusResponseError(writer http.ResponseWriter, code int, errors ...string)
func (api *API) addJobHandler(writer http.ResponseWriter, request *http.Request, _ httprouter.Params) { func (api *API) addJobHandler(writer http.ResponseWriter, request *http.Request, _ httprouter.Params) {
type requestBody struct { type requestBody struct {
} }
type replyBody struct { type replyBody Job
ID uuid.UUID `json:"id"`
Pipeline *pipeline.Pipeline `json:"pipeline"`
Targets []*target.Target `json:"targets"`
}
contentType := request.Header["Content-Type"] contentType := request.Header["Content-Type"]
if len(contentType) != 1 || contentType[0] != "application/json" { if len(contentType) != 1 || contentType[0] != "application/json" {
@ -103,9 +97,7 @@ func (api *API) addJobHandler(writer http.ResponseWriter, request *http.Request,
} }
func (api *API) updateJobHandler(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { func (api *API) updateJobHandler(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
type requestBody struct { type requestBody JobStatus
Status string `json:"status"`
}
contentType := request.Header["Content-Type"] contentType := request.Header["Content-Type"]
if len(contentType) != 1 || contentType[0] != "application/json" { if len(contentType) != 1 || contentType[0] != "application/json" {

View file

@ -1,4 +1,4 @@
package main package jobqueue
import ( import (
"encoding/json" "encoding/json"
@ -6,15 +6,18 @@ import (
"os/exec" "os/exec"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/osbuild/osbuild-composer/internal/pipeline" "github.com/osbuild/osbuild-composer/internal/pipeline"
"github.com/osbuild/osbuild-composer/internal/target" "github.com/osbuild/osbuild-composer/internal/target"
) )
type Job struct { type Job struct {
ID uuid.UUID `json:"id"` ID uuid.UUID `json:"id"`
Pipeline pipeline.Pipeline `json:"pipeline"` Pipeline *pipeline.Pipeline `json:"pipeline"`
Targets []target.Target `json:"targets"` Targets []*target.Target `json:"targets"`
}
type JobStatus struct {
Status string `json:"status"`
} }
func (job *Job) Run() error { func (job *Job) Run() error {