Forráskód Böngészése

container runner logic

reid 2 éve
szülő
commit
507a818620
2 módosított fájl, 15 hozzáadás és 7 törlés
  1. 7 6
      docker/docker.go
  2. 8 1
      rectify/rectify.go

+ 7 - 6
docker/docker.go

@@ -228,12 +228,13 @@ func StartContainer(containerName string, containerType string) (structs.Contain
 		return containerState, fmt.Errorf("failed to inspect container %s: %v", containerName, err)
 	}
 	containerState = structs.ContainerState{
-		ID:            containerDetails.ID,
-		Name:          containerName,
-		Image:         desiredImage,
-		DesiredStatus: desiredStatus,
-		ActualStatus:  containerDetails.State.Status,
-		CreatedAt:     containerDetails.Created,
+		ID:            containerDetails.ID,           // container id hash
+		Name:          containerName,                 // name (eg @p)
+		Image:         desiredImage,                  // full repo:tag@hash string
+		Type:          containerType,                 // eg `vere`
+		DesiredStatus: desiredStatus,                 // what the user sets
+		ActualStatus:  containerDetails.State.Status, // what the daemon reports
+		CreatedAt:     containerDetails.Created,      // this is a string
 	}
 	return containerState, err
 }

+ 8 - 1
rectify/rectify.go

@@ -18,10 +18,12 @@ var (
 	logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
 )
 
+// receives events via docker.EventBus
+// compares actual state to desired state
 func DockerSubscriptionHandler() {
 	for {
 		event := <-docker.EventBus
-		dockerEvent, ok := event.Data.(events.Message) // assert the type
+		dockerEvent, ok := event.Data.(events.Message)
 		if !ok {
 			logger.Error("Failed to assert Docker event data type")
 			continue
@@ -35,6 +37,10 @@ func DockerSubscriptionHandler() {
 			if containerState, exists := config.GetContainerState()[contName]; exists {
 				containerState.ActualStatus = "stopped"
 				config.UpdateContainerState(contName, containerState)
+				// start it again if this isn't what the user wants
+				if containerState.DesiredStatus != "stopped" {
+					docker.StartContainer(contName, containerState.Type)
+				}
 			}
 
 		case "start":
@@ -49,6 +55,7 @@ func DockerSubscriptionHandler() {
 			logger.Info(fmt.Sprintf("Docker: %s died!", contName))
 			if containerState, exists := config.GetContainerState()[contName]; exists {
 				containerState.ActualStatus = "died"
+				// we don't want infinite restart loop
 				containerState.DesiredStatus = "died"
 				config.UpdateContainerState(contName, containerState)
 			}