Skip to content

Commit

Permalink
Wait for docker daemon and retry 12 times
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianim committed Nov 11, 2021
1 parent c900428 commit bf33c31
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
28 changes: 28 additions & 0 deletions launcher/internal/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ const (
StateChanged
// ConsoleOutput means that there was some console output by the container
ConsoleOutput
// LogMessage means there was some important log from docker
LogMessage
)

// OnContainerEventFuc is the type of a callback function which gets called on every container event
Expand Down Expand Up @@ -156,10 +158,18 @@ func (c *LaunchableContainer) Launch() error {
defer func() {
c.isLaunching = false
if err != nil {
c.handleContainerEvent(Event{
Type: LogMessage,
Data: err.Error(),
})
c.setState(OfflineState)
}
}()

if err = c.WaitForDockerDaemon(); err != nil {
return err
}

if err = c.Stop(); err != nil {
return err
}
Expand All @@ -183,6 +193,24 @@ func (c *LaunchableContainer) Launch() error {
return nil
}

func (c *LaunchableContainer) WaitForDockerDaemon() error {
var err error
var retryCount = 0

_, err = dockerClient.ContainerList(context.Background(), types.ContainerListOptions{})
for err != nil && client.IsErrConnectionFailed(err) && retryCount < 12 {
retryCount++
c.handleContainerEvent(Event{
Type: LogMessage,
Data: err.Error() + ", retrying in 5 seconds...",
})
_, err = dockerClient.ContainerList(context.Background(), types.ContainerListOptions{})
time.Sleep(time.Second * 5)
}

return err
}

// Stop stops the container
func (c *LaunchableContainer) Stop() error {
c.setState(StoppingState)
Expand Down
2 changes: 1 addition & 1 deletion launcher/internal/container/sasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewSasmContainer() (*LaunchableContainer, error) {

if e != nil {
c.handleContainerEvent(Event{
Type: ConsoleOutput,
Type: LogMessage,
Data: e.Error(),
})
}
Expand Down
10 changes: 10 additions & 0 deletions launcher/internal/gui/launchTab.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gui
import (
"fmt"
"math"
"strings"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
Expand All @@ -13,6 +14,7 @@ import (
)

var statusLabel *widget.Label = nil
var statusLabelSameMessageCount = 0

func newLaunchTab(w fyne.Window) fyne.CanvasObject {
hello := widget.NewLabel(`Welcome to the sasm docker launcher!
Expand All @@ -39,6 +41,12 @@ Once you have them installed, just click Launch :D`)

func launchAppendLog(level string, message string) {
if level != "CONTAINER" {
if statusLabel.Text == message || strings.HasSuffix(statusLabel.Text, message) {
statusLabelSameMessageCount++
message = fmt.Sprintf("(%d) %s", statusLabelSameMessageCount, message)
} else {
statusLabelSameMessageCount = 0
}
statusLabel.SetText(message)
}
appendLog(level, message)
Expand Down Expand Up @@ -103,6 +111,8 @@ func handleContainerEvent(layerProgress map[string]*widget.ProgressBar, vBox *fy

case c.ConsoleOutput:
launchAppendLog("CONTAINER", event.Data.(string))
case c.LogMessage:
launchAppendLog("LOG", event.Data.(string))
}
}
}
Expand Down

0 comments on commit bf33c31

Please sign in to comment.