Fix: process handling of docoer-compose

Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
This commit is contained in:
Thomas Klaehn
2025-12-22 07:42:36 +01:00
parent 35db2c1f58
commit 66c2d8793d
2 changed files with 18 additions and 27 deletions

View File

@@ -112,21 +112,9 @@ func handle_patch_printerstate(w http.ResponseWriter, r *http.Request) {
return
}
if state.State == StateOn {
err = octoprint.ReStart()
if err != nil {
logger.Print(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write(json.RawMessage(fmt.Sprintf(`{"error": "%s"}`, err.Error())))
return
}
octoprint.ReStart()
} else {
err := octoprint.Stop()
if err != nil {
logger.Print(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write(json.RawMessage(fmt.Sprintf(`{"error": "%s"}`, err.Error())))
return
}
octoprint.Stop()
}
w.WriteHeader(http.StatusOK)
}

View File

@@ -19,7 +19,7 @@ type DockerState struct {
const (
start_cmd = "docker-compose -f /etc/printctrl/octoprint/docker-compose.yml up -d"
stop_cmd = "docker-compose -f /etc/printctrl/octoprint/docker-compose.yml down"
status_cmd = "docker ps"
status_cmd = "docker ps -a"
)
var (
@@ -30,22 +30,21 @@ func init() {
logger.SetFlags(log.Llongfile | log.Ltime)
}
func Start() error {
func Start() {
p := process.NewProcess(start_cmd)
return p.Start()
p.Start()
p.Wait()
}
func Stop() error {
func Stop() {
p := process.NewProcess(stop_cmd)
return p.Start()
p.Start()
p.Wait()
}
func ReStart() error {
err := Stop()
if err != nil {
logger.Print(err)
}
return Start()
func ReStart() {
Stop()
Start()
}
func Status() DockerState {
@@ -58,7 +57,7 @@ func Status() DockerState {
std := <-p.StdoutChannel
if strings.Contains(std, "octoprint/octoprint") {
res := strings.Split(std, " ")
if len(res) != 7 {
if len(res) < 7 {
logger.Printf("Unkown number pf parameters (%d vs. 7)", len(res))
} else {
state.ContainerID = res[0]
@@ -67,7 +66,11 @@ func Status() DockerState {
state.Created = res[3]
state.Status = res[4]
state.Ports = res[5]
state.Name = res[6]
if len(res) == 7 {
state.Name = res[6]
} else if len(res) == 9 {
state.Name = res[8]
}
}
}
}