Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Refactor server index to use GetJobs
Browse files Browse the repository at this point in the history
  • Loading branch information
apostergiou committed Apr 23, 2018
1 parent 3ec8a8c commit 9175c2b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 54 deletions.
49 changes: 49 additions & 0 deletions cmd/mistryd/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -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
}
56 changes: 2 additions & 54 deletions cmd/mistryd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
15 changes: 15 additions & 0 deletions pkg/types/build_info.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package types

import (
"encoding/json"
"fmt"
"io/ioutil"
"time"
)

Expand Down Expand Up @@ -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
}

0 comments on commit 9175c2b

Please sign in to comment.