Skip to content

Commit 7103cc3

Browse files
authored
fix: fixes initital load not being ready yet (#2763)
1 parent cd877c9 commit 7103cc3

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

internal/docker/container_store.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package docker
22

33
import (
44
"context"
5+
"sync"
56

67
"github.com/puzpuzpuz/xsync/v3"
78
log "github.com/sirupsen/logrus"
@@ -12,6 +13,7 @@ type ContainerStore struct {
1213
subscribers *xsync.MapOf[context.Context, chan ContainerEvent]
1314
client Client
1415
statsCollector *StatsCollector
16+
wg sync.WaitGroup
1517
}
1618

1719
func NewContainerStore(ctx context.Context, client Client) *ContainerStore {
@@ -20,17 +22,10 @@ func NewContainerStore(ctx context.Context, client Client) *ContainerStore {
2022
client: client,
2123
subscribers: xsync.NewMapOf[context.Context, chan ContainerEvent](),
2224
statsCollector: NewStatsCollector(client),
25+
wg: sync.WaitGroup{},
2326
}
2427

25-
containers, err := s.client.ListContainers()
26-
if err != nil {
27-
log.Fatalf("error while listing containers: %v", err)
28-
}
29-
30-
for _, c := range containers {
31-
c := c // create a new variable to avoid capturing the loop variable
32-
s.containers.Store(c.ID, &c)
33-
}
28+
s.wg.Add(1)
3429

3530
go s.init(ctx)
3631
go s.statsCollector.StartCollecting(ctx)
@@ -39,6 +34,7 @@ func NewContainerStore(ctx context.Context, client Client) *ContainerStore {
3934
}
4035

4136
func (s *ContainerStore) List() []Container {
37+
s.wg.Wait()
4238
containers := make([]Container, 0)
4339
s.containers.Range(func(_ string, c *Container) bool {
4440
containers = append(containers, *c)
@@ -70,10 +66,14 @@ func (s *ContainerStore) init(ctx context.Context) {
7066
if containers, err := s.client.ListContainers(); err == nil {
7167
for _, c := range containers {
7268
c := c // create a new variable to avoid capturing the loop variable
73-
s.containers.LoadOrStore(c.ID, &c)
69+
s.containers.Store(c.ID, &c)
7470
}
71+
} else {
72+
log.Fatalf("error listing containers: %v", err)
7573
}
7674

75+
s.wg.Done()
76+
7777
for {
7878
select {
7979
case event := <-events:

0 commit comments

Comments
 (0)