From fa5b7757188ea13c81402ee142ff093faab693c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Budai?= Date: Fri, 15 May 2020 11:37:16 +0200 Subject: [PATCH] worker/server: make JobStatus() return JobStatus struct Personally, I don't like methods returning too much values. I think it's easy to assign the values in an incorrect order and thus interpreting them in a wrong way. Also, the following commits will make JobStatus() return also the JobResult, which would make the number of returned values even higher. Not a functional change. --- internal/weldr/api.go | 5 +++-- internal/worker/server.go | 24 +++++++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/internal/weldr/api.go b/internal/weldr/api.go index 1bcff2658..972301b20 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -168,8 +168,9 @@ func (api *API) getComposeState(compose store.Compose) (state common.ComposeStat return } - state, queued, started, finished, _ = api.workers.JobStatus(jobId) - return + // is it ok to ignore this error? + jobStatus, _ := api.workers.JobStatus(jobId) + return jobStatus.State, jobStatus.Queued, jobStatus.Started, jobStatus.Finished } func verifyRequestVersion(writer http.ResponseWriter, params httprouter.Params, minVersion uint) bool { diff --git a/internal/worker/server.go b/internal/worker/server.go index 768120c14..b6b514232 100644 --- a/internal/worker/server.go +++ b/internal/worker/server.go @@ -27,6 +27,13 @@ type Server struct { imageWriter WriteImageFunc } +type JobStatus struct { + State common.ComposeState + Queued time.Time + Started time.Time + Finished time.Time +} + type WriteImageFunc func(composeID uuid.UUID, imageBuildID int, reader io.Reader) error func NewServer(logger *log.Logger, jobs jobqueue.JobQueue, imageWriter WriteImageFunc) *Server { @@ -78,15 +85,15 @@ func (s *Server) Enqueue(manifest *osbuild.Manifest, targets []*target.Target) ( return s.jobs.Enqueue("osbuild", job, nil) } -func (s *Server) JobStatus(id uuid.UUID) (state common.ComposeState, queued, started, finished time.Time, err error) { +func (s *Server) JobStatus(id uuid.UUID) (*JobStatus, error) { var canceled bool var result OSBuildJobResult - queued, started, finished, canceled, err = s.jobs.JobStatus(id, &result) + queued, started, finished, canceled, err := s.jobs.JobStatus(id, &result) if err != nil { - return + return nil, err } - + state := common.CWaiting if canceled { state = common.CFailed } else if !finished.IsZero() { @@ -97,11 +104,14 @@ func (s *Server) JobStatus(id uuid.UUID) (state common.ComposeState, queued, sta } } else if !started.IsZero() { state = common.CRunning - } else { - state = common.CWaiting } - return + return &JobStatus{ + State: state, + Queued: queued, + Started: started, + Finished: finished, + }, nil } func (s *Server) JobResult(id uuid.UUID) (common.ComposeState, *common.ComposeResult, error) {