mirror of
https://github.com/devplayer0/docker-net-dhcp
synced 2024-10-30 09:20:28 +00:00
Retry fetching Docker container info
This commit is contained in:
parent
e109e9923e
commit
3da81d344d
@ -215,7 +215,7 @@ func (m *dhcpManager) Start(ctx context.Context) error {
|
||||
return util.ErrNoContainer
|
||||
}
|
||||
|
||||
ctr, err := m.docker.ContainerInspect(ctx, ctrID)
|
||||
ctr, err := util.AwaitContainerInspect(ctx, m.docker, ctrID, pollTime)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get Docker container info: %w", err)
|
||||
}
|
||||
|
@ -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
Block a user