@@ -20,7 +20,9 @@ import (
2020 "errors"
2121 "fmt"
2222 "log/slog"
23+ "regexp"
2324 "slices"
25+ "strconv"
2426 "strings"
2527
2628 "github.com/arduino/go-paths-helper"
@@ -50,12 +52,11 @@ type containerState struct {
5052// For app that have at least 1 dependency, we calculate the overall state
5153// as follow:
5254//
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
55+ // running: all running
56+ // stopped: all stopped
57+ // failed: at least one failed
58+ // stopping: at least one stopping
59+ // starting: at least one starting
5960func parseAppStatus (containers []container.Summary ) []AppStatusInfo {
6061 apps := make ([]AppStatusInfo , 0 , len (containers ))
6162 appsStatusMap := make (map [string ][]containerState )
@@ -68,12 +69,6 @@ func parseAppStatus(containers []container.Summary) []AppStatusInfo {
6869 Status : StatusFromDockerState (c .State ),
6970 StatusMessage : c .Status ,
7071 })
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- )
7772 }
7873
7974 appendResult := func (appPath * paths.Path , status Status ) {
@@ -105,9 +100,7 @@ func parseAppStatus(containers []container.Summary) []AppStatusInfo {
105100 appendResult (appPath , StatusFailed )
106101 continue
107102 }
108- if slices .ContainsFunc (s , func (v containerState ) bool {
109- return v .Status == StatusStopped && strings .Contains (v .StatusMessage , "Exited (0)" )
110- }) {
103+ if slices .ContainsFunc (s , func (v containerState ) bool { return v .Status == StatusStopped && checkExitCode (v ) }) {
111104 appendResult (appPath , StatusFailed )
112105 continue
113106 }
@@ -272,3 +265,20 @@ func setStatusLeds(trigger LedTrigger) error {
272265 }
273266 return nil
274267}
268+
269+ func checkExitCode (state containerState ) bool {
270+ var exitCodeRegex = regexp .MustCompile (`Exited \((\d+)\)` )
271+ result := false
272+ matches := exitCodeRegex .FindStringSubmatch (state .StatusMessage )
273+
274+ exitCode , err := strconv .Atoi (matches [1 ])
275+ if err != nil {
276+ slog .Error ("Failed to parse exit code from status message" , slog .String ("statusMessage" , state .StatusMessage ), slog .String ("error" , err .Error ()))
277+ return false
278+ }
279+ if exitCode >= 0 && exitCode < 128 {
280+ result = true
281+ }
282+
283+ return result
284+ }
0 commit comments