Skip to content

Commit 9f354b3

Browse files
committed
check function in status
1 parent 76faefe commit 9f354b3

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

internal/orchestrator/helpers.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ import (
1919
"context"
2020
"errors"
2121
"fmt"
22-
"log/slog"
23-
"regexp"
2422
"slices"
25-
"strconv"
2623
"strings"
2724

2825
"github.com/arduino/go-paths-helper"
@@ -43,8 +40,7 @@ type AppStatusInfo struct {
4340
}
4441

4542
type containerState struct {
46-
Status Status
47-
StatusMessage string
43+
Status Status
4844
}
4945

5046
// parseAppStatus takes all the containers that matches the DockerAppLabel,
@@ -66,8 +62,7 @@ func parseAppStatus(containers []container.Summary) []AppStatusInfo {
6662
continue
6763
}
6864
appsStatusMap[appPath] = append(appsStatusMap[appPath], containerState{
69-
Status: StatusFromDockerState(c.State),
70-
StatusMessage: c.Status,
65+
Status: StatusFromDockerState(c.State, c.Status),
7166
})
7267
}
7368

@@ -100,7 +95,7 @@ func parseAppStatus(containers []container.Summary) []AppStatusInfo {
10095
appendResult(appPath, StatusFailed)
10196
continue
10297
}
103-
if slices.ContainsFunc(s, func(v containerState) bool { return v.Status == StatusStopped && checkExitCode(v) }) {
98+
if slices.ContainsFunc(s, func(v containerState) bool { return v.Status == StatusStopped }) {
10499
appendResult(appPath, StatusFailed)
105100
continue
106101
}
@@ -265,20 +260,3 @@ func setStatusLeds(trigger LedTrigger) error {
265260
}
266261
return nil
267262
}
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-
}

internal/orchestrator/status.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ package orchestrator
1717

1818
import (
1919
"fmt"
20+
"log/slog"
21+
"regexp"
22+
"strconv"
2023

2124
"github.com/docker/docker/api/types/container"
2225
)
@@ -31,18 +34,25 @@ const (
3134
StatusFailed Status = "failed"
3235
)
3336

34-
func StatusFromDockerState(s container.ContainerState) Status {
37+
func StatusFromDockerState(s container.ContainerState, statusMessage string) Status {
3538
switch s {
3639
case container.StateRunning:
3740
return StatusRunning
3841
case container.StateRestarting:
3942
return StatusStarting
4043
case container.StateRemoving:
4144
return StatusStopping
42-
case container.StateCreated, container.StateExited, container.StatePaused:
45+
case container.StateCreated, container.StatePaused, container.StateExited:
46+
failed, err := checkExitCode(statusMessage)
47+
if err != nil {
48+
slog.Warn("Failed to check exit code", slog.String("statusMessage", statusMessage), slog.String("error", err.Error()))
49+
} else if failed {
50+
return StatusFailed
51+
}
4352
return StatusStopped
4453
case container.StateDead:
4554
return StatusFailed
55+
4656
default:
4757
panic("unreachable")
4858
}
@@ -65,3 +75,19 @@ func (s Status) Validate() error {
6575
func (s Status) AllowedStatuses() []Status {
6676
return []Status{StatusStarting, StatusRunning, StatusStopping, StatusStopped, StatusFailed}
6777
}
78+
79+
func checkExitCode(statusMessage string) (bool, error) {
80+
var exitCodeRegex = regexp.MustCompile(`Exited \((\d+)\)`)
81+
matches := exitCodeRegex.FindStringSubmatch(statusMessage)
82+
83+
exitCode, err := strconv.Atoi(matches[1])
84+
if err != nil {
85+
slog.Error("Failed to parse exit code from status message", slog.String("statusMessage", statusMessage), slog.String("error", err.Error()))
86+
return false, fmt.Errorf("failed to parse exit code from status message: %w", err)
87+
}
88+
if exitCode >= 0 && exitCode < 128 {
89+
return true, nil
90+
}
91+
92+
return false, nil
93+
}

0 commit comments

Comments
 (0)