From ba4eb3166db1796e5604d60cad89b3a5c4bf15cc Mon Sep 17 00:00:00 2001 From: Luca Scalzotto Date: Thu, 24 Aug 2023 11:16:06 +0200 Subject: [PATCH] Check for operational message since container start instead of polling api (#8) --- cmd/instant-sonar.go | 19 ++++--------------- internal/docker/client.go | 11 ++++++++++- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/cmd/instant-sonar.go b/cmd/instant-sonar.go index 1bd62a5..232d48a 100644 --- a/cmd/instant-sonar.go +++ b/cmd/instant-sonar.go @@ -14,7 +14,6 @@ import ( "os/user" "path/filepath" "strings" - "time" ) type options struct { @@ -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 { @@ -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) } diff --git a/internal/docker/client.go b/internal/docker/client.go index 43111d4..c31cf2b 100644 --- a/internal/docker/client.go +++ b/internal/docker/client.go @@ -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) @@ -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 +}