1package command
2
3import (
4	"fmt"
5	"strings"
6
7	"github.com/hashicorp/nomad/api"
8	"github.com/hashicorp/nomad/api/contexts"
9	"github.com/posener/complete"
10)
11
12type JobEvalCommand struct {
13	Meta
14	forceRescheduling bool
15}
16
17func (c *JobEvalCommand) Help() string {
18	helpText := `
19Usage: nomad job eval [options] <job_id>
20
21  Force an evaluation of the provided job ID. Forcing an evaluation will trigger the scheduler
22  to re-evaluate the job. The force flags allow operators to force the scheduler to create
23  new allocations under certain scenarios.
24
25General Options:
26
27  ` + generalOptionsUsage() + `
28
29Eval Options:
30
31  -force-reschedule
32    Force reschedule failed allocations even if they are not currently
33    eligible for rescheduling.
34
35  -detach
36    Return immediately instead of entering monitor mode. The ID
37    of the evaluation created will be printed to the screen, which can be
38    used to examine the evaluation using the eval-status command.
39
40  -verbose
41    Display full information.
42`
43	return strings.TrimSpace(helpText)
44}
45
46func (c *JobEvalCommand) Synopsis() string {
47	return "Force an evaluation for the job"
48}
49
50func (c *JobEvalCommand) AutocompleteFlags() complete.Flags {
51	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
52		complete.Flags{
53			"-force-reschedule": complete.PredictNothing,
54			"-detach":           complete.PredictNothing,
55			"-verbose":          complete.PredictNothing,
56		})
57}
58
59func (c *JobEvalCommand) AutocompleteArgs() complete.Predictor {
60	return complete.PredictFunc(func(a complete.Args) []string {
61		client, err := c.Meta.Client()
62		if err != nil {
63			return nil
64		}
65
66		resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Jobs, nil)
67		if err != nil {
68			return []string{}
69		}
70		return resp.Matches[contexts.Jobs]
71	})
72}
73
74func (c *JobEvalCommand) Name() string { return "job eval" }
75
76func (c *JobEvalCommand) Run(args []string) int {
77	var detach, verbose bool
78
79	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
80	flags.Usage = func() { c.Ui.Output(c.Help()) }
81	flags.BoolVar(&c.forceRescheduling, "force-reschedule", false, "")
82	flags.BoolVar(&detach, "detach", false, "")
83	flags.BoolVar(&verbose, "verbose", false, "")
84
85	if err := flags.Parse(args); err != nil {
86		return 1
87	}
88
89	// Check that we either got no jobs or exactly one.
90	args = flags.Args()
91	if len(args) != 1 {
92		c.Ui.Error("This command takes one argument: <job>")
93		c.Ui.Error(commandErrorText(c))
94		return 1
95	}
96
97	// Get the HTTP client
98	client, err := c.Meta.Client()
99	if err != nil {
100		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
101		return 1
102	}
103
104	// Truncate the id unless full length is requested
105	length := shortId
106	if verbose {
107		length = fullId
108	}
109	// Call eval endpoint
110	jobID := args[0]
111
112	opts := api.EvalOptions{
113		ForceReschedule: c.forceRescheduling,
114	}
115	evalId, _, err := client.Jobs().EvaluateWithOpts(jobID, opts, nil)
116	if err != nil {
117		c.Ui.Error(fmt.Sprintf("Error evaluating job: %s", err))
118		return 1
119	}
120
121	if detach {
122		c.Ui.Output(fmt.Sprintf("Created eval ID: %q ", limit(evalId, length)))
123		return 0
124	}
125
126	mon := newMonitor(c.Ui, client, length)
127	return mon.monitor(evalId, false)
128}
129