Skip to content

Commit

Permalink
Check for operational message since container start instead of pollin…
Browse files Browse the repository at this point in the history
…g api (#8)
  • Loading branch information
LucaScorpion committed Aug 24, 2023
1 parent 8ece64d commit ba4eb31
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
19 changes: 4 additions & 15 deletions cmd/instant-sonar.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"os/user"
"path/filepath"
"strings"
"time"
)

type options struct {
Expand Down Expand Up @@ -93,7 +92,7 @@ func main() {
}

log.Verboseln("Waiting for SonarQube to be operational")
out := cli.FollowContainerLogStream(qubeContId)
out := cli.FollowContainerLogStream(qubeContId, cli.InspectContainer(qubeContId).State.StartedAt)
bufOut := bufio.NewReader(out)

for {
Expand All @@ -111,19 +110,9 @@ func main() {

sonarApi := sonar.NewApiClient("http://127.0.0.1:9000", opts.username, opts.password)

log.Verboseln("Waiting for SonarQube API to be up")
var lastErr error
for i := 0; i < 30; i++ {
lastErr = sonarApi.Ping()
if lastErr == nil || errors.Is(lastErr, sonar.ErrUnauthorized) {
break
}

time.Sleep(time.Second)
}

if lastErr != nil {
log.Errorln(lastErr)
log.Verboseln("Checking SonarQube API")
if err := sonarApi.Ping(); err != nil {
log.Errorln(err)
os.Exit(1)
}

Expand Down
11 changes: 10 additions & 1 deletion internal/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ func (c *Client) StartContainer(id string) {
}
}

func (c *Client) FollowContainerLogStream(id string) io.ReadCloser {
func (c *Client) FollowContainerLogStream(id string, since string) io.ReadCloser {
out, err := c.Cli.ContainerLogs(context.Background(), id, dockerTypes.ContainerLogsOptions{
Follow: true,
ShowStdout: true,
ShowStderr: true,
Since: since,
})
if err != nil {
panic(err)
Expand Down Expand Up @@ -99,3 +100,11 @@ func (c *Client) CopyDirToContainer(id, src, dest string) {
panic(err)
}
}

func (c *Client) InspectContainer(id string) dockerTypes.ContainerJSON {
insp, err := c.Cli.ContainerInspect(context.Background(), id)
if err != nil {
panic(err)
}
return insp
}

0 comments on commit ba4eb31

Please sign in to comment.