From 07a7652535d2e97239c4d4c02ae27089712e9450 Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Wed, 16 Sep 2015 16:20:19 -0700 Subject: [PATCH] command: easier exit codes, check if eval is already finished --- command/eval_monitor.go | 5 ++--- command/monitor.go | 28 +++++++++++++++++----------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/command/eval_monitor.go b/command/eval_monitor.go index b7bcb0117..78b84e5ac 100644 --- a/command/eval_monitor.go +++ b/command/eval_monitor.go @@ -22,9 +22,8 @@ Usage: nomad eval-monitor [options] real-time to the terminal. The command will exit when the given evaluation reaches a terminal - state (completed or failed). The exit status will reflect the - result of the evaluation (0 on success, 1 on failure). Exit code - 1 may also be encountered for client connectivity errors. + state (completed or failed). The exit code indicates the status + of the monitoring only; it does not reflect the evaluation status. General Options: diff --git a/command/monitor.go b/command/monitor.go index 6ad2f9c8f..4ecd8c625 100644 --- a/command/monitor.go +++ b/command/monitor.go @@ -129,10 +129,23 @@ func (m *monitor) update(eval *api.Evaluation, allocs []*api.AllocationListStub) // monitor is used to start monitoring the given evaluation ID. It // writes output directly to the monitor's ui, and returns the -// exit code for the command. The return code is 0 if monitoring -// succeeded and exited successfully, or 1 if an error was encountered -// or the eval status was returned as failed. +// exit code for the command. The return code indicates monitoring +// success or failure ONLY. It is no indication of the outcome of +// the evaluation, since conflating these values obscures things. func (m *monitor) monitor(evalID string) int { + // Check if the eval has already completed and fast-path it. + eval, _, err := m.client.Evaluations().Info(evalID, nil) + if err != nil { + m.ui.Error(fmt.Sprintf("Error reading evaluation: %s", err)) + return 1 + } + switch eval.Status { + case structs.EvalStatusComplete, structs.EvalStatusFailed: + m.ui.Info(fmt.Sprintf("Evaluation %q already finished with status %q", + evalID, eval.Status)) + return 0 + } + m.ui.Info(fmt.Sprintf("Monitoring evaluation %q", evalID)) for { // Check the current state of things @@ -165,14 +178,7 @@ func (m *monitor) monitor(evalID string) int { if eval.NextEval != "" { return m.monitor(eval.NextEval) } - - // Check if the eval is complete - switch eval.Status { - case structs.EvalStatusComplete: - return 0 - case structs.EvalStatusFailed: - return 1 - } + break } return 0