Skip to content

Commit

Permalink
fix: docker compose path and redis healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
smsunarto committed Jun 25, 2024
1 parent 00978ff commit 1dbc6f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
30 changes: 26 additions & 4 deletions cmd/world/cardinal/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package cardinal
import (
"context"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"time"

"github.com/charmbracelet/lipgloss"
"github.com/rotisserie/eris"
Expand All @@ -16,6 +18,7 @@ import (

"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 @@ -76,7 +79,7 @@ var devCmd = &cobra.Command{
// If any of the services terminates, the entire group will be terminated.
group, ctx := errgroup.WithContext(cmd.Context())
group.Go(func() error {
if err := startRedis(ctx); err != nil {
if err := startRedis(ctx, cfg); err != nil {
return eris.Wrap(err, "Encountered an error with Redis")
}
return eris.Wrap(ErrGracefulExit, "Redis terminated")
Expand Down Expand Up @@ -125,6 +128,25 @@ func startCardinalDevMode(ctx context.Context, cfg *config.Config, prettyLog boo
fmt.Println("Starting Cardinal...")
fmt.Println(style.BoldText.Render("Press Ctrl+C to stop\n"))

// 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
}

// Move into the cardinal directory
if err := os.Chdir(filepath.Join(cfg.RootDir, cfg.GameDir)); err != nil {
return eris.New("Unable to find cardinal directory. Are you in the project root?")
Expand Down Expand Up @@ -202,14 +224,14 @@ func startCardinalDevMode(ctx context.Context, cfg *config.Config, prettyLog boo
///////////////////

// startRedis runs Redis in a Docker container
func startRedis(ctx context.Context) error {
func startRedis(ctx context.Context, cfg *config.Config) error {
// Create an error group for managing redis lifecycle
group := new(errgroup.Group)

// Start Redis container
group.Go(func() error {
if err := teacmd.DockerStart(&config.Config{Detach: true, Build: false},
[]teacmd.DockerService{teacmd.DockerServiceRedis}); err != nil {
cfg.Detach = true
if err := teacmd.DockerStart(cfg, []teacmd.DockerService{teacmd.DockerServiceRedis}); err != nil {
return eris.Wrap(err, "Encountered an error with Redis")
}
return nil
Expand Down
3 changes: 1 addition & 2 deletions common/teacmd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func dockerCompose(args ...string) error {
}

func dockerComposeWithCfg(cfg *config.Config, args ...string) error {
yml := path.Join(cfg.RootDir, "docker-compose.yml")
args = append([]string{"compose", "-f", yml}, args...)
args = append([]string{"compose"}, args...)

cmd := exec.Command("docker", args...)
cmd.Stdout = os.Stdout
Expand Down

0 comments on commit 1dbc6f3

Please # to comment.