| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package rectify
- // this package is for watching the event bus and rectifying mismatches
- // between the desired and actual state
- import (
- "fmt"
- "goseg/broadcast"
- "goseg/config"
- "log/slog"
- "os"
- "github.com/docker/docker/api/types/events"
- )
- var (
- logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
- )
- func DockerSubscriptionHandler() {
- for {
- event := <-docker.EventBus
- dockerEvent, ok := event.Data.(events.Message) // assert the type
- if !ok {
- logger.Error("Failed to assert Docker event data type")
- continue
- }
- contID := dockerEvent.Actor.ID
- contName := dockerEvent.Actor.Attributes["name"]
- switch dockerEvent.Action {
- case "stop":
- logger.Info(fmt.Sprintf("Docker: %s stopped", contName))
- if containerState, exists := config.GSContainers[contID]; exists {
- containerState.Status = "stopped"
- config.GSContainers[contID] = containerState
- }
- case "start":
- logger.Info(fmt.Sprintf("Docker: %s started", contName))
- if containerState, exists := config.GSContainers[contID]; exists {
- containerState.Status = "started"
- config.GSContainers[contID] = containerState
- }
- default:
- logger.Info(fmt.Sprintf("Docker event: %s", dockerEvent.Action))
- }
- broadcast.BroadcastToClients()
- }
- }
|