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 return
} }
if state.State == StateOn { if state.State == StateOn {
err = octoprint.ReStart() octoprint.ReStart()
if err != nil {
logger.Print(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write(json.RawMessage(fmt.Sprintf(`{"error": "%s"}`, err.Error())))
return
}
} else { } else {
err := octoprint.Stop() octoprint.Stop()
if err != nil {
logger.Print(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write(json.RawMessage(fmt.Sprintf(`{"error": "%s"}`, err.Error())))
return
}
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }

View File

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