diff --git a/cmd/mistryd/job.go b/cmd/mistryd/job.go index beacf25..d091f6d 100644 --- a/cmd/mistryd/job.go +++ b/cmd/mistryd/job.go @@ -9,6 +9,7 @@ import ( "fmt" "html/template" "io" + "io/ioutil" "log" "os" "path/filepath" @@ -277,3 +278,51 @@ func GetState(path, project, id string) (string, error) { } return "", fmt.Errorf("job with id=%s not found error", id) } + +func GetJobs(path string) ([]Job, error) { + var jobs []Job + + projects, err := ioutil.ReadDir(path) + if err != nil { + return nil, fmt.Errorf("cannot scan projects; ", err) + } + + for _, p := range projects { + pendingPath := filepath.Join(path, p.Name(), "pending") + readyPath := filepath.Join(path, p.Name(), "ready") + pendingJobs, err := ioutil.ReadDir(pendingPath) + if err != nil { + return nil, fmt.Errorf("cannot scan pending jobs of project %s; %s", p.Name(), err) + } + readyJobs, err := ioutil.ReadDir(readyPath) + if err != nil { + return nil, fmt.Errorf("cannot scan ready jobs of project %s; %s", p.Name(), err) + } + + for _, j := range pendingJobs { + bi, err := types.GetBuildInfo(filepath.Join(pendingPath, j.Name(), BuildInfoFname)) + if err != nil { + return nil, fmt.Errorf("cannot read build_info file of job %s; %s", j.Name(), err) + } + jobs = append(jobs, Job{ + ID: j.Name(), + Project: p.Name(), + StartedAt: bi.StartedAt, + State: "pending"}) + } + + for _, j := range readyJobs { + bi, err := types.GetBuildInfo(filepath.Join(readyPath, j.Name(), BuildInfoFname)) + if err != nil { + return nil, fmt.Errorf("cannot read build_info file of job %s; %s", j.Name(), err) + } + jobs = append(jobs, Job{ + ID: j.Name(), + Project: p.Name(), + StartedAt: bi.StartedAt, + State: "ready"}) + } + } + + return jobs, nil +} diff --git a/cmd/mistryd/server.go b/cmd/mistryd/server.go index 048fb43..fdcf606 100644 --- a/cmd/mistryd/server.go +++ b/cmd/mistryd/server.go @@ -135,69 +135,17 @@ func (s *Server) HandleNewJob(w http.ResponseWriter, r *http.Request) { // HandleIndex returns all available jobs. func (s *Server) HandleIndex(w http.ResponseWriter, r *http.Request) { - var jobs []Job - if r.Method != "GET" { http.Error(w, "Expected GET, got "+r.Method, http.StatusMethodNotAllowed) return } - projects, err := ioutil.ReadDir(s.cfg.BuildPath) + jobs, err := GetJobs(s.cfg.BuildPath) if err != nil { - s.Log.Print("cannot scan projects; ", err) + s.Log.Printf("cannot get jobs for path %s; %s", s.cfg.BuildPath, err) w.WriteHeader(http.StatusInternalServerError) return } - - for _, p := range projects { - pendingPath := filepath.Join(s.cfg.BuildPath, p.Name(), "pending") - readyPath := filepath.Join(s.cfg.BuildPath, p.Name(), "ready") - pendingJobs, err := ioutil.ReadDir(pendingPath) - if err != nil { - s.Log.Printf("cannot scan pending jobs of project %s; %s", p.Name(), err) - w.WriteHeader(http.StatusInternalServerError) - return - } - readyJobs, err := ioutil.ReadDir(readyPath) - if err != nil { - s.Log.Printf("cannot scan ready jobs of project %s; %s", p.Name(), err) - w.WriteHeader(http.StatusInternalServerError) - return - } - - for _, j := range pendingJobs { - bi := types.BuildInfo{} - biBlob, err := ioutil.ReadFile(filepath.Join(pendingPath, j.Name(), BuildInfoFname)) - if err != nil { - s.Log.Printf("cannot read build_info file of job %s; %s", j.Name(), err) - w.WriteHeader(http.StatusInternalServerError) - return - } - err = json.Unmarshal(biBlob, &bi) - jobs = append(jobs, Job{ - ID: j.Name(), - Project: p.Name(), - StartedAt: bi.StartedAt, - State: "pending"}) - } - - for _, j := range readyJobs { - bi := types.BuildInfo{} - biBlob, err := ioutil.ReadFile(filepath.Join(readyPath, j.Name(), BuildInfoFname)) - if err != nil { - s.Log.Printf("cannot read build_info file of job %s; %s", j.Name(), err) - w.WriteHeader(http.StatusInternalServerError) - return - } - err = json.Unmarshal(biBlob, &bi) - jobs = append(jobs, Job{ - ID: j.Name(), - Project: p.Name(), - StartedAt: bi.StartedAt, - State: "ready"}) - } - } - sort.Slice(jobs, func(i, j int) bool { return jobs[j].StartedAt.Before(jobs[i].StartedAt) }) diff --git a/pkg/types/build_info.go b/pkg/types/build_info.go index ec9d7fa..63c1f0f 100644 --- a/pkg/types/build_info.go +++ b/pkg/types/build_info.go @@ -1,7 +1,9 @@ package types import ( + "encoding/json" "fmt" + "io/ioutil" "time" ) @@ -39,3 +41,16 @@ type ErrImageBuild struct { func (e ErrImageBuild) Error() string { return fmt.Sprintf("could not build docker image '%s': %s", e.Image, e.Err) } + +func GetBuildInfo(path string) (BuildInfo, error) { + bi := BuildInfo{} + biBlob, err := ioutil.ReadFile(path) + if err != nil { + return BuildInfo{}, fmt.Errorf("cannot read build_info file; %s", err) + } + err = json.Unmarshal(biBlob, &bi) + if err != nil { + return BuildInfo{}, fmt.Errorf("cannot unmarshal the build_info file; %s", err) + } + return bi, nil +}