Skip to content

Commit

Permalink
Merge pull request #32 from convox/speed-processes
Browse files Browse the repository at this point in the history
Speed up process list
  • Loading branch information
mattmanning committed Sep 18, 2015
2 parents 7c8c7ba + 7935924 commit 8d802a8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 26 deletions.
30 changes: 29 additions & 1 deletion api/controllers/processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"net/http"
"sort"
"sync"

"github.com/convox/rack/api/models"
Expand All @@ -26,7 +27,28 @@ func ProcessList(rw http.ResponseWriter, r *http.Request) error {
return err
}

return RenderJson(rw, processes)
final := models.Processes{}
psch := make(chan models.Process)
errch := make(chan error)

for _, p := range processes {
p := p
go p.FetchStatsAsync(psch, errch)
}

for range processes {
err := <-errch

if err != nil {
return err
}

final = append(final, <-psch)
}

sort.Sort(final)

return RenderJson(rw, final)
}

func ProcessShow(rw http.ResponseWriter, r *http.Request) error {
Expand All @@ -46,6 +68,12 @@ func ProcessShow(rw http.ResponseWriter, r *http.Request) error {
return err
}

err = p.FetchStats()

if err != nil {
return err
}

return RenderJson(rw, p)
}

Expand Down
2 changes: 0 additions & 2 deletions api/models/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,7 @@ func (a *App) RunAttached(process, command string, rw io.ReadWriter) error {
binds := []string{}
host := ""

fmt.Println("start")
pss, err := ListProcesses(a.Name)
fmt.Println("done")

if err != nil {
return err
Expand Down
62 changes: 39 additions & 23 deletions api/models/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,29 +227,6 @@ func fetchProcess(app string, task ecs.Task, td ecs.TaskDefinition, cd ecs.Conta
ps.Ports = append(ps.Ports, fmt.Sprintf("%d:%d", port.PublicPort, port.PrivatePort))
}

stch := make(chan *docker.Stats)
dnch := make(chan bool)

options := docker.StatsOptions{
ID: containers[0].ID,
Stats: stch,
Done: dnch,
}

go d.Stats(options)

stat := <-stch
dnch <- true

pcpu := stat.PreCPUStats.CPUUsage.TotalUsage
psys := stat.PreCPUStats.SystemCPUUsage

ps.Cpu = float64(int(calculateCPUPercent(pcpu, psys, stat)*10000)) / 10000

if stat.MemoryStats.Limit > 0 {
ps.Memory = float64(int(float64(stat.MemoryStats.Usage)/float64(stat.MemoryStats.Limit)*10000)) / 10000
}

psch <- ps
}

Expand Down Expand Up @@ -285,6 +262,45 @@ func (p *Process) Docker() (*docker.Client, error) {
return Docker(fmt.Sprintf("http://%s:2376", p.Host))
}

func (p *Process) FetchStats() error {
d, err := p.Docker()

if err != nil {
return fmt.Errorf("could not communicate with docker")
}

stch := make(chan *docker.Stats)
dnch := make(chan bool)

options := docker.StatsOptions{
ID: p.containerId,
Stats: stch,
Done: dnch,
Stream: false,
}

go d.Stats(options)

stat := <-stch
dnch <- true

pcpu := stat.PreCPUStats.CPUUsage.TotalUsage
psys := stat.PreCPUStats.SystemCPUUsage

p.Cpu = float64(int(calculateCPUPercent(pcpu, psys, stat)*10000)) / 10000

if stat.MemoryStats.Limit > 0 {
p.Memory = float64(int(float64(stat.MemoryStats.Usage)/float64(stat.MemoryStats.Limit)*10000)) / 10000
}

return nil
}

func (p *Process) FetchStatsAsync(psch chan Process, errch chan error) {
errch <- p.FetchStats()
psch <- *p
}

func (p *Process) Stop() error {
req := &ecs.StopTaskInput{
Cluster: aws.String(os.Getenv("CLUSTER")),
Expand Down

0 comments on commit 8d802a8

Please sign in to comment.