| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- 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"
- "goseg/docker"
- "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
- }
- contName := dockerEvent.Actor.Attributes["name"]
- switch dockerEvent.Action {
- case "stop":
- logger.Info(fmt.Sprintf("Docker: %s stopped", contName))
- if containerState, exists := config.GetContainerState()[contName]; exists {
- containerState.ActualStatus = "stopped"
- config.UpdateContainerState(contName, containerState)
- }
- case "start":
- logger.Info(fmt.Sprintf("Docker: %s started", contName))
- if containerState, exists := config.GetContainerState()[contName]; exists {
- containerState.ActualStatus = "running"
- config.UpdateContainerState(contName, containerState)
- }
- case "die":
- logger.Info(fmt.Sprintf("Docker: %s died!", contName))
- if containerState, exists := config.GetContainerState()[contName]; exists {
- containerState.ActualStatus = "died"
- containerState.DesiredStatus = "died"
- config.UpdateContainerState(contName, containerState)
- }
- default:
- logger.Info(fmt.Sprintf("%s event: %s", contName, dockerEvent.Action))
- }
- broadcast.BroadcastToClients()
- }
- }
|