Skip to content

Commit

Permalink
feat: use redis in docker compose, remove go mod vendor prepare step
Browse files Browse the repository at this point in the history
  • Loading branch information
smsunarto committed Jun 22, 2024
1 parent b0fefd8 commit f51da62
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 59 deletions.
58 changes: 5 additions & 53 deletions cmd/world/cardinal/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@ package cardinal
import (
"context"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"time"

"github.com/charmbracelet/lipgloss"
"github.com/magefile/mage/sh"
"github.com/rotisserie/eris"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"

"pkg.world.dev/world-cli/common"
"pkg.world.dev/world-cli/common/config"
"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/common/teacmd"
"pkg.world.dev/world-cli/tea/style"
)

Expand Down Expand Up @@ -211,45 +208,10 @@ func startRedis(ctx context.Context) error {

// Start Redis container
group.Go(func() error {
//nolint:gosec // not applicable
cmd := exec.Command(
"docker", "run", "-d", "-p", fmt.Sprintf("%s:%s", RedisPort, RedisPort),
"--name", "cardinal-dev-redis", "redis",
)

// Retry starting Redis container until successful
isDockerRunSuccessful := false
for !isDockerRunSuccessful {
if err := cmd.Run(); err != nil {
logger.Println("Failed to start Redis container. Retrying after cleanup...")
if err := stopRedis(); err != nil {
logger.Println("Failed to stop Redis container")
}
time.Sleep(1 * time.Second)
continue
}
isDockerRunSuccessful = true
if err := teacmd.DockerStart(&config.Config{Detach: true, Build: false},
[]teacmd.DockerService{teacmd.DockerServiceRedis}); err != nil {
return eris.Wrap(err, "Encountered an error with Redis")
}

// Check and wait until Redis is running and is available in the expected port
isRedisHealthy := false
for !isRedisHealthy {
redisAddress := fmt.Sprintf("localhost:%s", RedisPort)
conn, err := net.DialTimeout("tcp", redisAddress, time.Second)
if err != nil {
logger.Printf("Failed to connect to Redis at %s: %s\n", redisAddress, err)
time.Sleep(1 * time.Second)
continue
}

// Cleanup connection
if err := conn.Close(); err != nil {
continue
}

isRedisHealthy = true
}

return nil
})

Expand All @@ -259,7 +221,7 @@ func startRedis(ctx context.Context) error {
// 2) The parent context is canceled for whatever reason.
group.Go(func() error {
<-ctx.Done()
if err := stopRedis(); err != nil {
if err := teacmd.DockerStop([]teacmd.DockerService{teacmd.DockerServiceRedis}); err != nil {
return err
}
return nil
Expand All @@ -272,16 +234,6 @@ func startRedis(ctx context.Context) error {
return nil
}

func stopRedis() error {
logger.Println("Stopping cardinal-dev-redis container")
if err := sh.Run("docker", "rm", "-f", "cardinal-dev-redis"); err != nil {
logger.Println("Failed to delete Redis container automatically")
logger.Println("Please delete it manually with `docker rm -f cardinal-dev-redis`")
return err
}
return nil
}

///////////////////
// Utils Helpers //
///////////////////
Expand Down
1 change: 1 addition & 0 deletions cmd/world/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,6 @@ func contextWithSigterm(ctx context.Context) context.Context {
fmt.Println(textStyle.Render("Cancellation signal received. Terminating..."))
}
}()

return ctx
}
6 changes: 0 additions & 6 deletions common/teacmd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,9 @@ func prepareDir(dir string) error {
if err = os.Chdir(dir); err != nil {
return err
}
if err = sh.Rm("./vendor"); err != nil {
return err
}
if err = sh.Run("go", "mod", "tidy"); err != nil {
return err
}
if err = sh.Run("go", "mod", "vendor"); err != nil {
return err
}
if err = os.Chdir(startDir); err != nil {
return err
}
Expand Down

0 comments on commit f51da62

Please sign in to comment.