Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add creation time to job status #1540

Merged
merged 5 commits into from
Aug 16, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 41 additions & 27 deletions command/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ const (

type StatusCommand struct {
Meta
length int
showEvals, verbose bool
length int
evals bool
verbose bool
time bool
}

func (c *StatusCommand) Help() string {
Expand All @@ -44,6 +46,9 @@ Status Options:
-evals
Display the evaluations associated with the job.

-time
Display allocation creation time.

-verbose
Display full information.
`
Expand All @@ -60,8 +65,9 @@ func (c *StatusCommand) Run(args []string) int {
flags := c.Meta.FlagSet("status", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&short, "short", false, "")
flags.BoolVar(&c.showEvals, "evals", false, "")
flags.BoolVar(&c.evals, "evals", false, "")
flags.BoolVar(&c.verbose, "verbose", false, "")
flags.BoolVar(&c.time, "time", false, "")

if err := flags.Parse(args); err != nil {
return 1
Expand Down Expand Up @@ -95,22 +101,12 @@ func (c *StatusCommand) Run(args []string) int {
return 1
}

// No output if we have no jobs
if len(jobs) == 0 {
// No output if we have no jobs
c.Ui.Output("No running jobs")
return 0
}

out := make([]string, len(jobs)+1)
out[0] = "ID|Type|Priority|Status"
for i, job := range jobs {
out[i+1] = fmt.Sprintf("%s|%s|%d|%s",
job.ID,
job.Type,
job.Priority,
job.Status)
} else {
c.Ui.Output(createStatusListOutput(jobs))
}
c.Ui.Output(formatList(out))
return 0
}

Expand All @@ -126,16 +122,7 @@ func (c *StatusCommand) Run(args []string) int {
return 1
}
if len(jobs) > 1 && strings.TrimSpace(jobID) != jobs[0].ID {
out := make([]string, len(jobs)+1)
out[0] = "ID|Type|Priority|Status"
for i, job := range jobs {
out[i+1] = fmt.Sprintf("%s|%s|%d|%s",
job.ID,
job.Type,
job.Priority,
job.Status)
}
c.Ui.Output(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", formatList(out)))
c.Ui.Output(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs)))
return 0
}
// Prefix lookup matched a single job
Expand Down Expand Up @@ -306,7 +293,7 @@ func (c *StatusCommand) outputJobInfo(client *api.Client, job *api.Job) error {
}
}

if c.verbose || c.showEvals {
if c.verbose || c.evals {
c.Ui.Output(c.Colorize().Color("\n[bold]Evaluations[reset]"))
c.Ui.Output(formatList(evals))
}
Expand All @@ -320,6 +307,9 @@ func (c *StatusCommand) outputJobInfo(client *api.Client, job *api.Job) error {
if len(jobAllocs) > 0 {
allocs = make([]string, len(jobAllocs)+1)
allocs[0] = "ID|Eval ID|Node ID|Task Group|Desired|Status"
if c.time {
allocs[0] += "|Created"
}
for i, alloc := range jobAllocs {
allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s",
limit(alloc.ID, c.length),
Expand All @@ -328,6 +318,10 @@ func (c *StatusCommand) outputJobInfo(client *api.Client, job *api.Job) error {
alloc.TaskGroup,
alloc.DesiredStatus,
alloc.ClientStatus)
if c.time {
allocs[i+1] += fmt.Sprintf("|%s",
c.formatUnixNanoTime(alloc.CreateTime))
}
}

c.Ui.Output(formatList(allocs))
Expand Down Expand Up @@ -364,6 +358,12 @@ func (c *StatusCommand) outputFailedPlacements(failedEval *api.Evaluation) {
}
}

// formatUnixNanoTime is a helper for formatting time for output.
func (c *StatusCommand) formatUnixNanoTime(nano int64) string {
Copy link
Contributor

@dadgar dadgar Aug 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to same file as formatTime and it doesn't have to be an instance method

t := time.Unix(0, nano)
return formatTime(t)
}

// convertApiJob is used to take a *api.Job and convert it to an *struct.Job.
// This function is just a hammer and probably needs to be revisited.
func convertApiJob(in *api.Job) (*structs.Job, error) {
Expand All @@ -379,3 +379,17 @@ func convertApiJob(in *api.Job) (*structs.Job, error) {
}
return structJob, nil
}

// list general information about a list of jobs
func createStatusListOutput(jobs []*api.JobListStub) string {
out := make([]string, len(jobs)+1)
out[0] = "ID|Type|Priority|Status"
for i, job := range jobs {
out[i+1] = fmt.Sprintf("%s|%s|%d|%s",
job.ID,
job.Type,
job.Priority,
job.Status)
}
return formatList(out)
}
47 changes: 42 additions & 5 deletions command/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import (
"testing"

"github.com/mitchellh/cli"
"github.com/hashicorp/nomad/testutil"
"github.com/hashicorp/nomad/api"
)

func TestStatusCommand_Implements(t *testing.T) {
var _ cli.Command = &StatusCommand{}
}

func TestStatusCommand_Run(t *testing.T) {
srv, client, url := testServer(t, nil)
srv, client, url := testServer(t, func(c *testutil.TestServerConfig) {
c.DevMode = true
})
defer srv.Stop()

ui := new(cli.MockUi)
Expand All @@ -33,14 +37,22 @@ func TestStatusCommand_Run(t *testing.T) {

// Register two jobs
job1 := testJob("job1_sfx")
evalId, _, err := client.Jobs().Register(job1, nil)
evalId1, _, err := client.Jobs().Register(job1, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if code := waitForSuccess(ui, client, fullId, t, evalId1); code != 0 {
t.Fatalf("status code non zero saw %d", code)
}

job2 := testJob("job2_sfx")
if _, _, err := client.Jobs().Register(job2, nil); err != nil {
evalId2, _, err := client.Jobs().Register(job2, nil);
if err != nil {
t.Fatalf("err: %s", err)
}
if code := waitForSuccess(ui, client, fullId, t, evalId2); code != 0 {
t.Fatalf("status code non zero saw %d", code)
}

// Query again and check the result
if code := cmd.Run([]string{"-address=" + url}); code != 0 {
Expand Down Expand Up @@ -98,6 +110,25 @@ func TestStatusCommand_Run(t *testing.T) {
if !strings.Contains(out, "Allocations") {
t.Fatalf("should dump allocations")
}
if strings.Contains(out, "Created") {
t.Fatal("should not have created header")
}
ui.OutputWriter.Reset()

// Query a single job in time mode
if code := cmd.Run([]string{"-address=" + url, "-time", "job1_sfx"}); code != 0 {
t.Fatalf("expected exit 0, got: %d", code)
}
out = ui.OutputWriter.String()
if strings.Contains(out, "job2_sfx") || !strings.Contains(out, "job1_sfx") {
t.Fatalf("expected only job1_sfx, got: %s", out)
}
if !strings.Contains(out, "Allocations") {
t.Fatal("should dump allocations")
}
if !strings.Contains(out, "Created") {
t.Fatal("should have created header")
}
ui.OutputWriter.Reset()

// Query jobs with prefix match
Expand Down Expand Up @@ -134,7 +165,7 @@ func TestStatusCommand_Run(t *testing.T) {
if strings.Contains(out, "Allocations") {
t.Fatalf("should not dump allocations")
}
if strings.Contains(out, evalId) {
if strings.Contains(out, evalId1) {
t.Fatalf("should not contain full identifiers, got %s", out)
}
ui.OutputWriter.Reset()
Expand All @@ -144,7 +175,7 @@ func TestStatusCommand_Run(t *testing.T) {
t.Fatalf("expected exit 0, got: %d", code)
}
out = ui.OutputWriter.String()
if !strings.Contains(out, evalId) {
if !strings.Contains(out, evalId1) {
t.Fatalf("should contain full identifiers, got %s", out)
}
}
Expand All @@ -170,3 +201,9 @@ func TestStatusCommand_Fails(t *testing.T) {
t.Fatalf("expected failed query error, got: %s", out)
}
}

func waitForSuccess(ui cli.Ui, client *api.Client, length int, t *testing.T, evalId string) int {
mon := newMonitor(ui, client, length)
monErr := mon.monitor(evalId, false)
return monErr
}
2 changes: 1 addition & 1 deletion command/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func testServer(
}

func testJob(jobID string) *api.Job {
task := api.NewTask("task1", "exec").
task := api.NewTask("task1", "raw_exec").
SetConfig("command", "/bin/sleep").
Require(&api.Resources{
MemoryMB: 256,
Expand Down