Skip to content

Commit

Permalink
feat #199: stopping multiple procs will not fail fast
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 committed Aug 9, 2024
1 parent 842083c commit 7de08c1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/api/pc_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ func (api *PcApi) StopProcesses(c *gin.Context) {
}
stopped, err := api.project.StopProcesses(names)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
if len(stopped) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusMultiStatus, stopped)
}
return
}

Expand Down
11 changes: 8 additions & 3 deletions src/app/project_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,18 +288,23 @@ func (p *ProjectRunner) StopProcess(name string) error {
if err != nil {
log.Err(err).Msgf("failed to stop process %s", name)
}
return nil
return err
}

func (p *ProjectRunner) StopProcesses(names []string) ([]string, error) {
stopped := make([]string, 0)
for _, name := range names {
if err := p.StopProcess(name); err == nil {
stopped = append(stopped, name)
} else {
return nil, err
}
}

if len(stopped) != len(names) {
if len(stopped) == 0 {
return stopped, fmt.Errorf("no such processes or not running: %v", names)
}
return stopped, fmt.Errorf("failed to stop some processes")
}
return stopped, nil
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (p *PcClient) stopProcesses(names []string) ([]string, error) {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusMultiStatus {
stopped := []string{}
if err = json.NewDecoder(resp.Body).Decode(&stopped); err != nil {
log.Err(err).Msgf("failed to decode stop processes %v", names)
Expand Down
43 changes: 41 additions & 2 deletions src/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,62 @@ package cmd

import (
"fmt"
"github.com/fatih/color"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"os"
"strings"
)

var (
verboseOutput = false
)

// stopCmd represents the stop command
var stopCmd = &cobra.Command{
Use: "stop [PROCESS...]",
Short: "Stop a running process",
Short: "Stop running processes",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
stopped, err := getClient().StopProcesses(args)
if err != nil {
log.Fatal().Err(err).Msgf("failed to stop processes %v", args)
}
fmt.Printf("Processes %v stopped\n", stopped)
missing := findMissingProcesses(args, stopped)
if len(missing) != 0 && !verboseOutput {
fmt.Printf("Successfully stopped some processes but encountered failures for: %s\n",
"'"+strings.Join(missing, `', '`)+`'`)
os.Exit(1)
}
for _, name := range stopped {
fmt.Printf("%s Successfully stopped %s\n", color.GreenString("✓"), name)
}
if len(missing) > 0 {
log.Error().Msgf("failed to stop some processes: %v", missing)
for _, name := range missing {
fmt.Printf("%s Failed to stop %s\n", color.RedString("✘"), name)
}
os.Exit(1)
}
},
}

func findMissingProcesses(requested, stopped []string) []string {
bMap := make(map[string]bool)
for _, str := range stopped {
bMap[str] = true
}

missing := []string{}
for _, str := range requested {
if !bMap[str] {
missing = append(missing, str)
}
}
return missing
}

func init() {
processCmd.AddCommand(stopCmd)
stopCmd.Flags().BoolVarP(&verboseOutput, "verbose", "v", verboseOutput, "verbose output")
}

0 comments on commit 7de08c1

Please sign in to comment.