Skip to content

Commit

Permalink
fleetctl: optimize status command
Browse files Browse the repository at this point in the history
Use the Unit API instead of Units API to speed up lookups on
environments with a large number of distinct services.

Fixes coreos#1340
  • Loading branch information
royvandewater committed Sep 3, 2015
1 parent 8c2dc37 commit cd23d91
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions fleetctl/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"

"github.com/coreos/fleet/job"
"github.com/coreos/fleet/schema"
)

var cmdStatusUnits = &Command{
Expand All @@ -44,47 +43,34 @@ func init() {
}

func runStatusUnits(args []string) (exit int) {
units, err := cAPI.Units()
if err != nil {
stderr("Error retrieving unit: %v", err)
return 1
}

uMap := make(map[string]*schema.Unit, len(args))
for _, u := range units {
if u != nil {
u := u
uMap[u.Name] = u
}
}

names := make([]string, len(args))
for i, arg := range args {
name := unitNameMangle(arg)
names[i] = name
unit, err := cAPI.Unit(name)
if err != nil {
stderr("Error retrieving unit: %v", err)
return 1
}

u, ok := uMap[name]
if !ok {
if unit == nil {
stderr("Unit %s does not exist.", name)
return 1
} else if suToGlobal(*u) {
stderr("Unable to determine status of global unit %s.", name)
} else if suToGlobal(*unit) {
stderr("Unable to determine status of global unit %s.", unit.Name)
return 1
} else if job.JobState(u.CurrentState) == job.JobStateInactive {
stderr("Unit %s does not appear to be loaded.", name)
} else if job.JobState(unit.CurrentState) == job.JobStateInactive {
stderr("Unit %s does not appear to be loaded.", unit.Name)
return 1
}
}

for i, name := range names {
// This extra newline is here to match systemctl status output
if i != 0 {
fmt.Printf("\n")
}

if exit = runCommand(uMap[name].MachineID, "systemctl", "status", "-l", name); exit != 0 {
if exit = runCommand(unit.MachineID, "systemctl", "status", "-l", unit.Name); exit != 0 {
break
}
}

return
}

0 comments on commit cd23d91

Please sign in to comment.