github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/command/stop.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 type StopCommand struct { 9 Meta 10 } 11 12 func (c *StopCommand) Help() string { 13 helpText := ` 14 Usage: nomad stop [options] <job> 15 16 Stop an existing job. This command is used to signal allocations 17 to shut down for the given job ID. Upon successful deregistraion, 18 an interactive monitor session will start to display log lines as 19 the job unwinds its allocations and completes shutting down. It 20 is safe to exit the monitor early using ctrl+c. 21 22 General Options: 23 24 ` + generalOptionsUsage() + ` 25 26 Stop Options: 27 28 -detach 29 Return immediately instead of entering monitor mode. After the 30 deregister command is submitted, a new evaluation ID is printed 31 to the screen, which can be used to call up a monitor later if 32 needed using the eval-monitor command. 33 ` 34 return strings.TrimSpace(helpText) 35 } 36 37 func (c *StopCommand) Synopsis() string { 38 return "Stop a running job" 39 } 40 41 func (c *StopCommand) Run(args []string) int { 42 var detach bool 43 44 flags := c.Meta.FlagSet("stop", FlagSetClient) 45 flags.Usage = func() { c.Ui.Output(c.Help()) } 46 flags.BoolVar(&detach, "detach", false, "") 47 48 if err := flags.Parse(args); err != nil { 49 return 1 50 } 51 52 // Check that we got exactly one job 53 args = flags.Args() 54 if len(args) != 1 { 55 c.Ui.Error(c.Help()) 56 return 1 57 } 58 jobID := args[0] 59 60 // Get the HTTP client 61 client, err := c.Meta.Client() 62 if err != nil { 63 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 64 return 1 65 } 66 67 // Check if the job exists 68 if _, _, err := client.Jobs().Info(jobID, nil); err != nil { 69 c.Ui.Error(fmt.Sprintf("Error deregistering job: %s", err)) 70 return 1 71 } 72 73 // Invoke the stop 74 evalID, _, err := client.Jobs().Deregister(jobID, nil) 75 if err != nil { 76 c.Ui.Error(fmt.Sprintf("Error deregistering job: %s", err)) 77 return 1 78 } 79 80 if detach { 81 c.Ui.Output(evalID) 82 return 0 83 } 84 85 // Start monitoring the stop eval 86 mon := newMonitor(c.Ui, client) 87 return mon.monitor(evalID) 88 }