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
…71)

Closes: WORLD-XXX

<!---
Add a prefix to indicate what kind of release this pull request
corresponds to:
  feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
--->

## Overview

> Description of the overall background and high-level changes that this
PR introduces

<!---
Example: This pull request improves documentation of area A by adding
...
--->

## Brief Changelog

<!---
Example:
- The metadata is stored in the blob store on job creation time as a
persistent artifact
- Deployments RPC transmits only the blob storage reference
- Daemons retrieve the RPC data from the blob cache
--->

## Testing and Verifying

<!---
Pick one of the following options:

- This change is a trivial rework/code cleanup without any test
coverage.

- This change is already covered by existing tests, such as <describe
test>.

- This change added tests and can be verified as follows:
    - Added unit test that validates ...
    - Added integration tests for end-to-end deployment with ...
    - Extended integration test for ...
    - Manually verified the change by ...
--->
  • Loading branch information
smsunarto authored Jun 25, 2024
1 parent b0fefd8 commit 00978ff
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 00978ff

Please # to comment.