@@ -19,6 +19,7 @@ import (
1919 "context"
2020 "errors"
2121 "fmt"
22+ "log/slog"
2223 "slices"
2324 "strings"
2425
@@ -36,7 +37,12 @@ import (
3637
3738type AppStatusInfo struct {
3839 AppPath * paths.Path
39- Status Status
40+ State State
41+ }
42+
43+ type containerStateInfo struct {
44+ State State
45+ StatusMessage string
4046}
4147
4248// parseAppStatus takes all the containers that matches the DockerAppLabel,
@@ -52,19 +58,29 @@ type AppStatusInfo struct {
5258// starting: at least one starting
5359func parseAppStatus (containers []container.Summary ) []AppStatusInfo {
5460 apps := make ([]AppStatusInfo , 0 , len (containers ))
55- appsStatusMap := make (map [string ][]Status )
61+ appsStatusMap := make (map [string ][]containerStateInfo )
5662 for _ , c := range containers {
5763 appPath , ok := c .Labels [DockerAppPathLabel ]
5864 if ! ok {
5965 continue
6066 }
61- appsStatusMap [appPath ] = append (appsStatusMap [appPath ], StatusFromDockerState (c .State ))
67+ appsStatusMap [appPath ] = append (appsStatusMap [appPath ], containerStateInfo {
68+ State : StatusFromDockerState (c .State ),
69+ StatusMessage : c .Status ,
70+ })
71+
72+ slog .Debug ("Container status" ,
73+ slog .String ("appPath" , appPath ),
74+ slog .String ("containerID" , c .ID ),
75+ slog .String ("state" , string (c .State )),
76+ slog .String ("statusMessage" , c .Status ),
77+ )
6278 }
6379
64- appendResult := func (appPath * paths.Path , status Status ) {
80+ appendResult := func (appPath * paths.Path , status State ) {
6581 apps = append (apps , AppStatusInfo {
6682 AppPath : appPath ,
67- Status : status ,
83+ State : status ,
6884 })
6985 }
7086
@@ -74,31 +90,37 @@ func parseAppStatus(containers []container.Summary) []AppStatusInfo {
7490 appPath := paths .New (appPath )
7591
7692 // running: all running
77- if ! slices .ContainsFunc (s , func (v Status ) bool { return v != StatusRunning }) {
93+ if ! slices .ContainsFunc (s , func (v containerStateInfo ) bool { return v . State != StatusRunning }) {
7894 appendResult (appPath , StatusRunning )
7995 continue
8096 }
8197 // stopped: all stopped
82- if ! slices .ContainsFunc (s , func (v Status ) bool { return v != StatusStopped }) {
98+ if ! slices .ContainsFunc (s , func (v containerStateInfo ) bool { return v . State != StatusStopped }) {
8399 appendResult (appPath , StatusStopped )
84100 continue
85101 }
86102
87103 // ...else we have multiple different status we calculate the status
88104 // among the possible left: {failed, stopping, starting}
89- if slices .ContainsFunc (s , func (v Status ) bool { return v == StatusFailed }) {
105+ if slices .ContainsFunc (s , func (v containerStateInfo ) bool { return v . State == StatusFailed }) {
90106 appendResult (appPath , StatusFailed )
91107 continue
92108 }
93- if slices .ContainsFunc (s , func (v Status ) bool { return v == StatusStopping }) {
94- appendResult (appPath , StatusStopping )
109+ if slices .ContainsFunc (s , func (v containerStateInfo ) bool { return v . State == StatusStopped }) {
110+ appendResult (appPath , StatusFailed )
95111 continue
96112 }
97- if slices .ContainsFunc (s , func (v Status ) bool { return v == StatusStopped }) {
113+ if slices .ContainsFunc (s , func (v containerStateInfo ) bool {
114+ return v .State == StatusStopped && strings .Contains (v .StatusMessage , "Exited (0)" )
115+ }) {
98116 appendResult (appPath , StatusFailed )
99117 continue
100118 }
101- if slices .ContainsFunc (s , func (v Status ) bool { return v == StatusStarting }) {
119+ if slices .ContainsFunc (s , func (v containerStateInfo ) bool { return v .State == StatusStopping }) {
120+ appendResult (appPath , StatusStopping )
121+ continue
122+ }
123+ if slices .ContainsFunc (s , func (v containerStateInfo ) bool { return v .State == StatusStarting }) {
102124 appendResult (appPath , StatusStarting )
103125 continue
104126 }
@@ -193,7 +215,7 @@ func getRunningApp(
193215 return nil , fmt .Errorf ("failed to get running apps: %w" , err )
194216 }
195217 idx := slices .IndexFunc (apps , func (a AppStatusInfo ) bool {
196- return a .Status == StatusRunning || a .Status == StatusStarting
218+ return a .State == StatusRunning || a .State == StatusStarting
197219 })
198220 if idx == - 1 {
199221 return nil , nil
0 commit comments