mirror of
https://github.com/amir20/dozzle.git
synced 2025-12-21 13:23:07 +01:00
2
Makefile
2
Makefile
@@ -15,7 +15,7 @@ fake_assets:
|
|||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test: fake_assets
|
test: fake_assets
|
||||||
go test -cover ./...
|
go test -cover -race ./...
|
||||||
|
|
||||||
.PHONY: build
|
.PHONY: build
|
||||||
build: dist
|
build: dist
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ContainerStore struct {
|
type ContainerStore struct {
|
||||||
containers sync.Map
|
containers map[string]*Container
|
||||||
client Client
|
client Client
|
||||||
statsCollector *StatsCollector
|
statsCollector *StatsCollector
|
||||||
subscribers sync.Map
|
subscribers sync.Map
|
||||||
@@ -16,7 +16,7 @@ type ContainerStore struct {
|
|||||||
|
|
||||||
func NewContainerStore(client Client) *ContainerStore {
|
func NewContainerStore(client Client) *ContainerStore {
|
||||||
s := &ContainerStore{
|
s := &ContainerStore{
|
||||||
containers: sync.Map{},
|
containers: make(map[string]*Container),
|
||||||
client: client,
|
client: client,
|
||||||
subscribers: sync.Map{},
|
subscribers: sync.Map{},
|
||||||
statsCollector: NewStatsCollector(client),
|
statsCollector: NewStatsCollector(client),
|
||||||
@@ -29,11 +29,10 @@ func NewContainerStore(client Client) *ContainerStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *ContainerStore) List() []Container {
|
func (s *ContainerStore) List() []Container {
|
||||||
containers := make([]Container, 0)
|
containers := make([]Container, 0, len(s.containers))
|
||||||
s.containers.Range(func(_, value any) bool {
|
for _, c := range s.containers {
|
||||||
containers = append(containers, value.(Container))
|
containers = append(containers, *c)
|
||||||
return true
|
}
|
||||||
})
|
|
||||||
|
|
||||||
return containers
|
return containers
|
||||||
}
|
}
|
||||||
@@ -58,7 +57,7 @@ func (s *ContainerStore) init(ctx context.Context) {
|
|||||||
|
|
||||||
for _, c := range containers {
|
for _, c := range containers {
|
||||||
c := c // create a new variable to avoid capturing the loop variable
|
c := c // create a new variable to avoid capturing the loop variable
|
||||||
s.containers.Store(c.ID, c)
|
s.containers[c.ID] = &c
|
||||||
}
|
}
|
||||||
|
|
||||||
events := make(chan ContainerEvent)
|
events := make(chan ContainerEvent)
|
||||||
@@ -70,20 +69,19 @@ func (s *ContainerStore) init(ctx context.Context) {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case event := <-events:
|
case event := <-events:
|
||||||
log.Tracef("received event: %+v", event)
|
log.Debugf("received event: %+v", event)
|
||||||
switch event.Name {
|
switch event.Name {
|
||||||
case "start":
|
case "start":
|
||||||
if container, err := s.client.FindContainer(event.ActorID); err == nil {
|
if container, err := s.client.FindContainer(event.ActorID); err == nil {
|
||||||
log.Debugf("container %s started", container.ID)
|
log.Debugf("container %s started", container.ID)
|
||||||
s.containers.Store(container.ID, container)
|
s.containers[container.ID] = &container
|
||||||
}
|
}
|
||||||
case "destroy":
|
case "destroy":
|
||||||
log.Debugf("container %s destroyed", event.ActorID)
|
log.Debugf("container %s destroyed", event.ActorID)
|
||||||
s.containers.Delete(event.ActorID)
|
delete(s.containers, event.ActorID)
|
||||||
|
|
||||||
case "die":
|
case "die":
|
||||||
if value, ok := s.containers.Load(event.ActorID); ok {
|
if container, ok := s.containers[event.ActorID]; ok {
|
||||||
container := value.(Container)
|
|
||||||
log.Debugf("container %s died", container.ID)
|
log.Debugf("container %s died", container.ID)
|
||||||
container.State = "exited"
|
container.State = "exited"
|
||||||
}
|
}
|
||||||
@@ -92,8 +90,7 @@ func (s *ContainerStore) init(ctx context.Context) {
|
|||||||
if event.Name == "health_status: healthy" {
|
if event.Name == "health_status: healthy" {
|
||||||
healthy = "healthy"
|
healthy = "healthy"
|
||||||
}
|
}
|
||||||
if value, ok := s.containers.Load(event.ActorID); ok {
|
if container, ok := s.containers[event.ActorID]; ok {
|
||||||
container := value.(Container)
|
|
||||||
log.Debugf("container %s is %s", container.ID, healthy)
|
log.Debugf("container %s is %s", container.ID, healthy)
|
||||||
container.Health = healthy
|
container.Health = healthy
|
||||||
}
|
}
|
||||||
@@ -108,9 +105,9 @@ func (s *ContainerStore) init(ctx context.Context) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
case stat := <-stats:
|
case stat := <-stats:
|
||||||
if container, ok := s.containers.Load(stat.ID); ok {
|
if container, ok := s.containers[stat.ID]; ok {
|
||||||
stat.ID = ""
|
stat.ID = ""
|
||||||
container.(Container).Stats.Push(stat)
|
container.Stats.Push(stat)
|
||||||
}
|
}
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user