-
Notifications
You must be signed in to change notification settings - Fork 7
fix: make app failed if a container is stopped #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
4105b65
99354fd
a09d652
c9deb5f
25d90c6
21e3fde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ import ( | |
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "log/slog" | ||
| "slices" | ||
| "strings" | ||
|
|
||
|
|
@@ -36,34 +37,50 @@ import ( | |
|
|
||
| type AppStatusInfo struct { | ||
| AppPath *paths.Path | ||
| Status Status | ||
| State State | ||
| } | ||
|
|
||
| type containerStateInfo struct { | ||
| State State | ||
| StatusMessage string | ||
| } | ||
|
|
||
| // parseAppStatus takes all the containers that matches the DockerAppLabel, | ||
| // and construct a map of the state of an app and all its dependencies state. | ||
| // For app that have at least 1 dependency, we calculate the overall state | ||
| // as follow: | ||
| // | ||
| // running: all running | ||
| // stopped: all stopped | ||
| // failed: at least one failed | ||
| // stopping: at least one stopping | ||
| // starting: at least one starting | ||
| // running: all running | ||
| // stopped: all stopped | ||
| // failed: at least one failed | ||
| // stopping: at least one stopping | ||
| // stopped: at least one stopped | ||
| // starting: at least one starting | ||
| func parseAppStatus(containers []container.Summary) []AppStatusInfo { | ||
| apps := make([]AppStatusInfo, 0, len(containers)) | ||
| appsStatusMap := make(map[string][]Status) | ||
| appsStatusMap := make(map[string][]containerStateInfo) | ||
| for _, c := range containers { | ||
| appPath, ok := c.Labels[DockerAppPathLabel] | ||
| if !ok { | ||
| continue | ||
| } | ||
| appsStatusMap[appPath] = append(appsStatusMap[appPath], StatusFromDockerState(c.State)) | ||
| appsStatusMap[appPath] = append(appsStatusMap[appPath], containerStateInfo{ | ||
| State: StatusFromDockerState(c.State), | ||
| StatusMessage: c.Status, | ||
| }) | ||
|
|
||
| slog.Debug("Container status", | ||
| slog.String("appPath", appPath), | ||
| slog.String("containerID", c.ID), | ||
| slog.String("state", string(c.State)), | ||
| slog.String("statusMessage", c.Status), | ||
| ) | ||
| } | ||
|
|
||
| appendResult := func(appPath *paths.Path, status Status) { | ||
| appendResult := func(appPath *paths.Path, status State) { | ||
| apps = append(apps, AppStatusInfo{ | ||
| AppPath: appPath, | ||
| Status: status, | ||
| State: status, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -73,27 +90,33 @@ func parseAppStatus(containers []container.Summary) []AppStatusInfo { | |
| appPath := paths.New(appPath) | ||
|
|
||
| // running: all running | ||
| if !slices.ContainsFunc(s, func(v Status) bool { return v != StatusRunning }) { | ||
| if !slices.ContainsFunc(s, func(v containerStateInfo) bool { return v.State != StatusRunning }) { | ||
| appendResult(appPath, StatusRunning) | ||
| continue | ||
| } | ||
| // stopped: all stopped | ||
| if !slices.ContainsFunc(s, func(v Status) bool { return v != StatusStopped }) { | ||
| if !slices.ContainsFunc(s, func(v containerStateInfo) bool { return v.State != StatusStopped }) { | ||
| appendResult(appPath, StatusStopped) | ||
| continue | ||
| } | ||
|
|
||
| // ...else we have multiple different status we calculate the status | ||
| // among the possible left: {failed, stopping, starting} | ||
| if slices.ContainsFunc(s, func(v Status) bool { return v == StatusFailed }) { | ||
| if slices.ContainsFunc(s, func(v containerStateInfo) bool { return v.State == StatusFailed }) { | ||
| appendResult(appPath, StatusFailed) | ||
| continue | ||
| } | ||
| if slices.ContainsFunc(s, func(v containerStateInfo) bool { | ||
| return v.State == StatusStopped && strings.Contains(v.StatusMessage, "Exited (0)") | ||
|
||
| }) { | ||
| appendResult(appPath, StatusFailed) | ||
| continue | ||
| } | ||
| if slices.ContainsFunc(s, func(v Status) bool { return v == StatusStopping }) { | ||
| if slices.ContainsFunc(s, func(v containerStateInfo) bool { return v.State == StatusStopping }) { | ||
| appendResult(appPath, StatusStopping) | ||
| continue | ||
| } | ||
| if slices.ContainsFunc(s, func(v Status) bool { return v == StatusStarting }) { | ||
| if slices.ContainsFunc(s, func(v containerStateInfo) bool { return v.State == StatusStarting }) { | ||
| appendResult(appPath, StatusStarting) | ||
| continue | ||
| } | ||
|
|
@@ -188,7 +211,7 @@ func getRunningApp( | |
| return nil, fmt.Errorf("failed to get running apps: %w", err) | ||
| } | ||
| idx := slices.IndexFunc(apps, func(a AppStatusInfo) bool { | ||
| return a.Status == StatusRunning || a.Status == StatusStarting | ||
| return a.State == StatusRunning || a.State == StatusStarting | ||
| }) | ||
| if idx == -1 { | ||
| return nil, nil | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ func TestParseAppStatus(t *testing.T) { | |
| tests := []struct { | ||
| name string | ||
| containerState []container.ContainerState | ||
| want Status | ||
| want State | ||
| }{ | ||
| { | ||
| name: "everything running", | ||
|
|
@@ -46,7 +46,7 @@ func TestParseAppStatus(t *testing.T) { | |
| }, | ||
| { | ||
| name: "failed container takes precedence over stopping and starting", | ||
| containerState: []container.ContainerState{container.StateRunning, container.StateDead, container.StateRemoving, container.StateRestarting}, | ||
| containerState: []container.ContainerState{container.StateRunning, container.StateDead, container.StateRemoving, container.StateRestarting, container.StateExited}, | ||
| want: StatusFailed, | ||
| }, | ||
| { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should add a test for the new check |
||
|
|
@@ -61,7 +61,7 @@ func TestParseAppStatus(t *testing.T) { | |
| }, | ||
| { | ||
| name: "starting", | ||
| containerState: []container.ContainerState{container.StateRestarting, container.StateExited}, | ||
| containerState: []container.ContainerState{container.StateRestarting}, | ||
| want: StatusStarting, | ||
| }, | ||
| } | ||
|
|
@@ -76,7 +76,7 @@ func TestParseAppStatus(t *testing.T) { | |
| }) | ||
| res := parseAppStatus(input) | ||
| require.Len(t, res, 1) | ||
| require.Equal(t, tc.want, res[0].Status) | ||
| require.Equal(t, tc.want, res[0].State) | ||
| require.Equal(t, "path1", res[0].AppPath.String()) | ||
| }) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't you update this mapping function and don't account for the status message there? If a container exited with anything that isn't 128 + 9 we could consider that a failing state, because it means that the process exited without Docker killing it.