Skip to content

Commit

Permalink
fix(redis): redis not stopped properly in dev mode (#27)
Browse files Browse the repository at this point in the history
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

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## 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.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
zulkhair committed Jan 27, 2024
1 parent 7fe2073 commit 384b929
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
23 changes: 8 additions & 15 deletions cmd/world/cardinal/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}
Expand All @@ -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 {
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions common/tea_cmd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions common/tea_cmd/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tea_cmd

import (
"fmt"
"github.com/magefile/mage/sh"
"gotest.tools/v3/assert"
"os"
"testing"
Expand All @@ -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",
Expand All @@ -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)
}
Expand Down

0 comments on commit 384b929

Please sign in to comment.