Skip to content

Commit

Permalink
fix: Fix waiting for local instance start up
Browse files Browse the repository at this point in the history
  • Loading branch information
efirs committed Sep 12, 2022
1 parent 9130c79 commit a2e1bf8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/package.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: release-binaries
on:
release:
types: [created]
workflow_call:

jobs:
releases-matrix:
Expand Down
13 changes: 10 additions & 3 deletions client/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,25 @@ func Init(config config.Config) error {
return nil
}

// Get returns an instance of client
func Get() driver.Driver {
func InitLow() error {
if D == nil {
ctx, cancel := util.GetContext(context.Background())
defer cancel()

drv, err := driver.NewDriver(ctx, cfg)
if err != nil {
util.Error(err, "tigris client initialization failed")
return err
}
D = drv
}
return nil
}

// Get returns an instance of client
func Get() driver.Driver {
if err := InitLow(); err != nil {
util.Error(err, "tigris client initialization failed")
}
return D
}

Expand Down
25 changes: 24 additions & 1 deletion cmd/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var ImageTag = "latest"
func stopContainer(client *client.Client, cname string) {
ctx := context.Background()

log.Debug().Msg("stopping local instance")

if err := client.ContainerStop(ctx, cname, nil); err != nil {
if !errdefs.IsNotFound(err) {
log.Fatal().Err(err).Str("name", cname).Msg("error stopping container")
Expand All @@ -60,17 +62,23 @@ func stopContainer(client *client.Client, cname string) {
log.Fatal().Err(err).Str("name", cname).Msg("error stopping container")
}
}

log.Debug().Msg("local instance stopped")
}

func startContainer(cli *client.Client, cname string, image string, port string, env []string) string {
ctx := context.Background()

log.Debug().Msg("starting local instance")

reader, err := cli.ImagePull(ctx, image, types.ImagePullOptions{})
if err != nil {
log.Fatal().Err(err).Str("image", image).Msg("error pulling docker image")
}
defer func() { _ = reader.Close() }()

log.Debug().Msg("local Docker image pulled")

if util.IsTTY(os.Stdout) {
if err := util.DockerShowProgress(reader); err != nil {
util.Error(err, "error pulling docker image")
Expand All @@ -92,6 +100,8 @@ func startContainer(cli *client.Client, cname string, image string, port string,
}
}

log.Debug().Msg("creating container")

resp, err := cli.ContainerCreate(ctx,
&container.Config{
Hostname: cname,
Expand All @@ -106,23 +116,34 @@ func startContainer(cli *client.Client, cname string, image string, port string,
log.Fatal().Err(err).Str("image", image).Msg("error creating container docker image")
}

log.Debug().Msg("starting container")

if err = cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
log.Fatal().Err(err).Str("image", image).Msg("error starting docker image")
}

log.Debug().Msg("local instance started successfully")

return resp.ID
}

func waitServerUp() {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()

log.Debug().Msg("waiting local instance to start")

inited := false
var err error

if err = tclient.Init(config.DefaultConfig); err != nil {
util.Error(err, "client init failed")
}

L:
for {
if !inited {
if err = tclient.Init(config.DefaultConfig); err == nil {
if err = tclient.InitLow(); err == nil {
inited = true
}
} else {
Expand All @@ -148,6 +169,8 @@ L:
if err != nil {
util.Error(err, "tigris initialization failed")
}

log.Debug().Msg("wait finished successfully")
}

var serverUpCmd = &cobra.Command{
Expand Down
3 changes: 1 addition & 2 deletions tests/db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ $cli version
$cli config show

if [ -z "$noup" ]; then
$cli local up 8081
TIGRIS_LOG_LEVEL=debug $cli local up 8081
$cli local logs >/dev/null 2>&1
fi

Expand All @@ -36,7 +36,6 @@ test_config() {
export TIGRIS_TIMEOUT=333s
export TIGRIS_PROTOCOL=https
export TIGRIS_URL=example.com:8888
$cli config show
$cli config show | grep "application_id: test_id_1"
$cli config show | grep "application_secret: test_secret_1"
$cli config show | grep "timeout: 5m33s"
Expand Down

0 comments on commit a2e1bf8

Please sign in to comment.