From 39f9aa4cde45ea36dd45ad9ff6666a732c2007f7 Mon Sep 17 00:00:00 2001 From: SusanHex Date: Tue, 5 May 2026 05:20:53 +0000 Subject: [PATCH] Enable RE-Actor to wait for container start, even if it closes --- src/main.go | 62 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/src/main.go b/src/main.go index 9c88500..a1e74c4 100644 --- a/src/main.go +++ b/src/main.go @@ -9,7 +9,7 @@ import ( "fmt" "io" "log/slog" - "os" + "time" "github.com/SusanHex/RE-Actor/src/config" "github.com/SusanHex/RE-Actor/src/utils" @@ -51,38 +51,50 @@ func main() { panic(fmt.Sprintf(`Container name: "%s" matched %d containers. Please ensure that the container name is unique to one container.`, app_config.ContainerName, len(containers))) } ctr := containers[0] - ctr_inspection, err := cli.ContainerInspect(ctx, ctr.ID) - if err != nil { - panic(err) - } - is_tty := ctr_inspection.Config.Tty - slog.Debug("Found container:", "ID", ctr.ID, "TTY", is_tty) - reader, err := cli.ContainerLogs(ctx, ctr.ID, container.LogsOptions{ - ShowStdout: true, - ShowStderr: true, - Follow: true, - Since: "1s", - }) - if err != nil { - panic(err) - } - for { - message, err := utils.GetContainerLog(reader, is_tty) + slog.Debug("Waiting for running container...") + for { + ctr_inspection, err := cli.ContainerInspect(ctx, ctr.ID) if err != nil { - if errors.Is(err, io.EOF) { - slog.Error("Container closed, exiting...") - os.Exit(0) - } panic(err) } - if len(message) == 0 { + + if !ctr_inspection.State.Running { + time.Sleep(100 * time.Millisecond) continue } - slog.Debug(fmt.Sprintf(`Got message of %d bytes`, len(message))) - err = utils.PerformActionIfMatch(app_config, action, message) + slog.Debug("Found running container:", "ID", ctr.ID, "TTY", ctr_inspection.Config.Tty) + + reader, err := cli.ContainerLogs(ctx, ctr.ID, container.LogsOptions{ + ShowStdout: true, + ShowStderr: true, + Follow: true, + Since: "1s", + }) if err != nil { panic(err) } + + for { + message, err := utils.GetContainerLog(reader, ctr_inspection.Config.Tty) + + if err != nil { + if errors.Is(err, io.EOF) { + slog.Debug("Container closed...") + break + } else { + panic(err) + } + + } + if len(message) == 0 { + continue + } + slog.Debug(fmt.Sprintf(`Got message of %d bytes`, len(message))) + err = utils.PerformActionIfMatch(app_config, action, message) + if err != nil { + panic(err) + } + } } }