From 384b92939fb38e9d39be78101b38f419660a6978 Mon Sep 17 00:00:00 2001 From: Zulkhair Abdullah Daim <53172227+zulkhair@users.noreply.github.com> Date: Sat, 27 Jan 2024 01:27:01 +0000 Subject: [PATCH] fix(redis): redis not stopped properly in dev mode (#27) Closes: WORLD-728 ## Overview Fix redis not stopped properly when send sigint (ctrl+c) on dev mode ## Brief Changelog - Remove `cmd.Wait()` on `runCardinal` func so program will continue until signal channel is created - Add some message to the screen ## Testing and Verifying Test the program using command `world dev` and `world dev --debug` with this conditions : - all redis are stopped (both cardinal and cardinal-dev) - redis cardinal is up -> it will show message `Maybe redis cardinal docker is still up, run 'world cardinal stop' and try again` - redis cardinal-dev is up -> it will cleanup and rerun the redis again ## Summary by CodeRabbit - **Improvements** - Enhanced startup messages for better user feedback during development operations. - Improved error messaging for specific Redis-related issues. - Refined error handling in the Cardinal development command for clarity. --- cmd/world/cardinal/dev.go | 23 ++++++++--------------- common/tea_cmd/docker.go | 7 +------ common/tea_cmd/git_test.go | 7 ++++--- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/cmd/world/cardinal/dev.go b/cmd/world/cardinal/dev.go index bd7aa95..edbb22b 100644 --- a/cmd/world/cardinal/dev.go +++ b/cmd/world/cardinal/dev.go @@ -78,6 +78,7 @@ var devCmd = &cobra.Command{ // Create a channel to receive errors from the command cmdErr := make(chan error, 1) + fmt.Println("Starting Cardinal...") go func() { err := cardinalExecCmd.Wait() cmdErr <- err @@ -128,11 +129,7 @@ func runRedis() error { logger.Println("Starting Redis container...") cmd := exec.Command("docker", "run", "-d", "-p", fmt.Sprintf("%s:%s", RedisPort, RedisPort), "--name", "cardinal-dev-redis", "redis") cmd.Stdout = os.Stdout - - // hide stderr if not in debug mode - if logger.DebugMode { - cmd.Stderr = os.Stderr - } + cmd.Stderr = os.Stderr err := cmd.Run() if err != nil { @@ -144,6 +141,10 @@ func runRedis() error { err := sh.Run("docker", "run", "-d", "-p", fmt.Sprintf("%s:%s", RedisPort, RedisPort), "--name", "cardinal-dev-redis", "redis") if err != nil { + if sh.ExitStatus(err) == 125 { + fmt.Println("Maybe redis cardinal docker is still up, run 'world cardinal stop' and try again") + return err + } return err } } @@ -168,11 +169,7 @@ func runCardinal() (*exec.Cmd, error) { cmd := exec.Command("go", "run", ".") cmd.Stdout = os.Stdout - - // hide stderr if not in debug mode - if logger.DebugMode { - cmd.Stderr = os.Stderr - } + cmd.Stderr = os.Stderr cmd.Env = os.Environ() for k, v := range env { @@ -181,11 +178,7 @@ func runCardinal() (*exec.Cmd, error) { err = cmd.Start() if err != nil { - return cmd, err - } - err = cmd.Wait() - if err != nil { - return cmd, err + return nil, err } return cmd, nil diff --git a/common/tea_cmd/docker.go b/common/tea_cmd/docker.go index ee18d73..599b597 100644 --- a/common/tea_cmd/docker.go +++ b/common/tea_cmd/docker.go @@ -9,7 +9,6 @@ import ( "github.com/magefile/mage/sh" "pkg.world.dev/world-cli/common/config" - "pkg.world.dev/world-cli/common/logger" ) type DockerService string @@ -34,11 +33,7 @@ func dockerComposeWithCfg(cfg config.Config, args ...string) error { cmd := exec.Command("docker", args...) cmd.Stdout = os.Stdout - - // hide stderr if not in debug mode - if logger.DebugMode { - cmd.Stderr = os.Stderr - } + cmd.Stderr = os.Stderr env := os.Environ() for k, v := range cfg.DockerEnv { diff --git a/common/tea_cmd/git_test.go b/common/tea_cmd/git_test.go index 4e18394..a20224f 100644 --- a/common/tea_cmd/git_test.go +++ b/common/tea_cmd/git_test.go @@ -2,6 +2,7 @@ package tea_cmd import ( "fmt" + "github.com/magefile/mage/sh" "gotest.tools/v3/assert" "os" "testing" @@ -19,13 +20,13 @@ func TestGitCloneCmd(t *testing.T) { test := []struct { name string wantErr bool - expected string + expected int param param }{ { name: "error clone wrong address", wantErr: true, - expected: `running "git clone wrong address targetDir" failed with exit code 128`, + expected: 128, param: param{ url: "wrong address", targetDir: "targetDir", @@ -50,7 +51,7 @@ func TestGitCloneCmd(t *testing.T) { err := GitCloneCmd(tt.param.url, tt.param.targetDir, tt.param.initMsg) if tt.wantErr { - assert.Error(t, err, tt.expected) + assert.Equal(t, sh.ExitStatus(err), tt.expected) } else { assert.NilError(t, err) }