Retry fetching Docker container info
parent
e109e9923e
commit
3da81d344d
@ -1,5 +1,42 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/client"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
OptionsKeyGeneric = "com.docker.network.generic"
|
||||
)
|
||||
|
||||
func AwaitContainerInspect(ctx context.Context, docker *client.Client, id string, interval time.Duration) (types.ContainerJSON, error) {
|
||||
var err error
|
||||
ctrChan := make(chan types.ContainerJSON)
|
||||
go func() {
|
||||
for {
|
||||
var ctr types.ContainerJSON
|
||||
ctr, err = docker.ContainerInspect(ctx, id)
|
||||
if err == nil {
|
||||
ctrChan <- ctr
|
||||
return
|
||||
}
|
||||
|
||||
time.Sleep(interval)
|
||||
}
|
||||
}()
|
||||
|
||||
var dummy types.ContainerJSON
|
||||
select {
|
||||
case link := <-ctrChan:
|
||||
return link, nil
|
||||
case <-ctx.Done():
|
||||
if err != nil {
|
||||
log.WithError(err).WithField("id", id).Error("Failed to await container by index")
|
||||
}
|
||||
return dummy, ctx.Err()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue