Skip to content

Commit 5460aa3

Browse files
committed
handle ct state in helpers
1 parent 906154a commit 5460aa3

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

internal/orchestrator/helpers.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import (
1919
"context"
2020
"errors"
2121
"fmt"
22-
"log/slog"
22+
"regexp"
2323
"slices"
24+
"strconv"
2425
"strings"
2526

2627
"github.com/arduino/go-paths-helper"
@@ -50,12 +51,11 @@ type containerState struct {
5051
// For app that have at least 1 dependency, we calculate the overall state
5152
// as follow:
5253
//
53-
// running: all running
54-
// stopped: all stopped
55-
// failed: at least one failed
56-
// stopping: at least one stopping
57-
// stopped: at least one stopped
58-
// starting: at least one starting
54+
// running: all running
55+
// stopped: all stopped
56+
// failed: at least one failed
57+
// stopping: at least one stopping
58+
// starting: at least one starting
5959
func parseAppStatus(containers []container.Summary) []AppStatusInfo {
6060
apps := make([]AppStatusInfo, 0, len(containers))
6161
appsStatusMap := make(map[string][]containerState)
@@ -68,12 +68,6 @@ func parseAppStatus(containers []container.Summary) []AppStatusInfo {
6868
Status: StatusFromDockerState(c.State),
6969
StatusMessage: c.Status,
7070
})
71-
slog.Debug("Container status",
72-
slog.String("appPath", appPath),
73-
slog.String("containerID", c.ID),
74-
slog.String("state", string(c.State)),
75-
slog.String("statusMessage", c.Status),
76-
)
7771
}
7872

7973
appendResult := func(appPath *paths.Path, status Status) {
@@ -106,7 +100,7 @@ func parseAppStatus(containers []container.Summary) []AppStatusInfo {
106100
continue
107101
}
108102
if slices.ContainsFunc(s, func(v containerState) bool {
109-
return v.Status == StatusStopped && strings.Contains(v.StatusMessage, "Exited (0)")
103+
return v.Status == StatusStopped && checkExitCode(v.StatusMessage)
110104
}) {
111105
appendResult(appPath, StatusFailed)
112106
continue
@@ -272,3 +266,19 @@ func setStatusLeds(trigger LedTrigger) error {
272266
}
273267
return nil
274268
}
269+
270+
func checkExitCode(statusMessage string) bool {
271+
var exitCodeRegex = regexp.MustCompile(`Exited \((\d+)\)`)
272+
273+
matches := exitCodeRegex.FindStringSubmatch(statusMessage)
274+
if len(matches) < 2 {
275+
return false
276+
}
277+
278+
exitCode, err := strconv.Atoi(matches[1])
279+
if err != nil {
280+
return false
281+
}
282+
283+
return exitCode >= 0 && exitCode < 128
284+
}

internal/orchestrator/helpers_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,46 +27,55 @@ func TestParseAppStatus(t *testing.T) {
2727
tests := []struct {
2828
name string
2929
containerState []container.ContainerState
30+
statusMessage []string
3031
want Status
3132
}{
3233
{
3334
name: "everything running",
3435
containerState: []container.ContainerState{container.StateRunning, container.StateRunning},
36+
statusMessage: []string{"Up 5 minutes", "Up 10 minutes"},
3537
want: StatusRunning,
3638
},
3739
{
3840
name: "everything stopped",
3941
containerState: []container.ContainerState{container.StateCreated, container.StatePaused, container.StateExited},
42+
statusMessage: []string{"Created", "Paused", "Exited (137)"},
4043
want: StatusStopped,
4144
},
4245
{
4346
name: "failed container",
4447
containerState: []container.ContainerState{container.StateRunning, container.StateDead},
48+
statusMessage: []string{"Up 5 minutes", "Dead"},
4549
want: StatusFailed,
4650
},
4751
{
4852
name: "failed container takes precedence over stopping and starting",
4953
containerState: []container.ContainerState{container.StateRunning, container.StateDead, container.StateRemoving, container.StateRestarting},
54+
statusMessage: []string{"Up 5 minutes", "Dead", "Removing", "Restarting"},
5055
want: StatusFailed,
5156
},
5257
{
5358
name: "stopping",
5459
containerState: []container.ContainerState{container.StateRunning, container.StateRemoving},
60+
statusMessage: []string{"Up 5 minutes", "Removing"},
5561
want: StatusStopping,
5662
},
5763
{
5864
name: "stopping takes precedence over starting",
5965
containerState: []container.ContainerState{container.StateRunning, container.StateRestarting, container.StateRemoving},
66+
statusMessage: []string{"Up 5 minutes", "Restarting", "Removing"},
6067
want: StatusStopping,
6168
},
6269
{
6370
name: "starting",
6471
containerState: []container.ContainerState{container.StateRestarting, container.StateExited},
72+
statusMessage: []string{"Restarting", "Exited (129)"},
6573
want: StatusStarting,
6674
},
6775
{
6876
name: "failed",
6977
containerState: []container.ContainerState{container.StateRestarting, container.StateExited},
78+
statusMessage: []string{"Restarting", "Exited (0)"},
7079
want: StatusFailed,
7180
},
7281
}

0 commit comments

Comments
 (0)