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.
This commit is contained in:
Ondřej Budai 2020-05-15 11:37:16 +02:00 committed by Lars Karlitski
parent 8b58d2c91a
commit fa5b775718
2 changed files with 20 additions and 9 deletions

View file

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

View file

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