Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Docker Compose container execution #687

Merged
merged 6 commits into from
Feb 9, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions internal/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/elastic/elastic-package/internal/signal"
)

const waitForHealthyTimeout = 10 * time.Minute

// Project represents a Docker Compose project.
type Project struct {
name string
Expand Down Expand Up @@ -275,20 +277,23 @@ func (p *Project) WaitForHealthy(opts CommandOptions) error {
return err
}

timeout := time.Duration(waitForHealthyTimeout)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you can push this one as var to the top of the file and name accordingly.

startTime := time.Now()
timeoutTime := startTime.Add(timeout)
interval := time.Duration(1 * time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think you can push this one to consts too.

healthy := true

containerIDs := strings.Split(strings.TrimSpace(b.String()), "\n")
for {
for time.Now().Before(timeoutTime) {
if signal.SIGINT() {
return errors.New("SIGINT: cancel waiting for policy assigned")
}

healthy := true

logger.Debugf("Wait for healthy containers: %s", strings.Join(containerIDs, ","))
descriptions, err := docker.InspectContainers(containerIDs...)
if err != nil {
return err
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's leave this empty line

for _, containerDescription := range descriptions {
logger.Debugf("Container status: %s", containerDescription.String())

Expand Down Expand Up @@ -316,12 +321,19 @@ func (p *Project) WaitForHealthy(opts CommandOptions) error {
healthy = false
mtojek marked this conversation as resolved.
Show resolved Hide resolved
}

// end loop before timeout if healthy
if healthy {
break
}

time.Sleep(time.Second)
mtojek marked this conversation as resolved.
Show resolved Hide resolved
// NOTE: using sleep does not guarantee interval but it's ok for this use case
time.Sleep(interval)
}

if !healthy {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about shuffling around with healthy var? I think it broke down system tests. You can look at the original implementation and look for the bug or revert it, and leave the timeout improvement.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the bug is obvious, I misinterpreted the logic. healthy must be reinitialized at each loop, otherwise once it become false the logic does not change it's value anymore. I tried to use it to detect the timeout, but I stumbled upon the bug. The current implementation is clearer and uses times instead of a side effect on the healthy value.

return errors.New("timeout waiting for healthy container")
}

return nil
}

Expand Down