Skip to content

Commit

Permalink
stop command
Browse files Browse the repository at this point in the history
  • Loading branch information
praktiskt committed Nov 5, 2020
1 parent f85df5a commit c68202a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cluster/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type Cluster struct {
HostURL url.URL
ConfigURL url.URL
ClusterURL url.URL
DatasetsURL url.URL
OverviewURL url.URL
Jobs struct {
Expand Down Expand Up @@ -47,6 +48,7 @@ func New(hostURL string) Cluster {

cl.HostURL = h
cl.ConfigURL = tools.UrlOrFail(h, "/config")
cl.ClusterURL = tools.UrlOrFail(h, "/cluster")
cl.OverviewURL = tools.UrlOrFail(h, "/overview")
cl.DatasetsURL = tools.UrlOrFail(h, "/datasets")

Expand Down
2 changes: 1 addition & 1 deletion cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
// stopCmd represents the stop command
var stopCmd = &cobra.Command{
Use: "stop",
Short: "A brief description of your command",
Short: "Stop a running job, or the entire cluster.",
}

func init() {
Expand Down
28 changes: 24 additions & 4 deletions cmd/stopCluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,39 @@ package cmd

import (
"fmt"
"io/ioutil"

"github.com/parnurzeal/gorequest"
"github.com/spf13/cobra"
)

var iKnowWhatImDoing bool

// stopClusterCmd represents the stopCluster command
var stopClusterCmd = &cobra.Command{
Use: "cluster",
Short: "A brief description of your command",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("stopCluster called")
Use: "cluster",
Short: "shut down the cluster",
PreRun: func(cmd *cobra.Command, args []string) { InitCluster() },
RunE: func(cmd *cobra.Command, args []string) error {
if !iKnowWhatImDoing {
return fmt.Errorf("you don't know what you're doing")
}
resp, _, _ := gorequest.
New().
Delete(cl.ClusterURL.String()).
End()
//TODO: Error management
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
msg := string(body)
Print(msg)
return nil
},
}

func init() {
stopCmd.AddCommand(stopClusterCmd)
stopClusterCmd.Flags().BoolVar(&iKnowWhatImDoing, "i-know-what-im-doing", false, "you need to pass this flag for the call to work :)")
}
29 changes: 25 additions & 4 deletions cmd/stopJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,36 @@ package cmd
import (
"fmt"

"github.com/parnurzeal/gorequest"
"github.com/spf13/cobra"
)

// stopJobCmd represents the stopJob command
var stopJobCmd = &cobra.Command{
Use: "job",
Short: "A brief description of your command",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("stopJob called")
Use: "job <job id>",
Short: "Stop a currently running job",
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) { InitCluster() },
RunE: func(cmd *cobra.Command, args []string) error {
jid := args[0]
if len(jid) != 32 {
return fmt.Errorf("`%v` is not a valid job id", jid)
}

//TODO: Currently uses yarn-cancel as opposed to just /stop (which doesn't seem to work)
stopURL := fmt.Sprintf("%v/%v/yarn-cancel", cl.Jobs.URL.String(), jid)
resp, body, _ := gorequest.
New().
Get(stopURL).
End()

if resp.StatusCode == 202 {
fmt.Printf("Successfully cancelled job %v\n", jid)
} else {
fmt.Println("Failed to cancelled job: " + body)
}

return nil
},
}

Expand Down

0 comments on commit c68202a

Please sign in to comment.