From 0fb546ad616edc7968210160d40e42f2538a3492 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sun, 2 Feb 2025 01:43:14 -0500 Subject: [PATCH] engine: resources: Make some svc cleanups We would often actually drop the refresh because of bad checks. --- engine/resources/svc.go | 74 +++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/engine/resources/svc.go b/engine/resources/svc.go index e15b9743..3186149c 100644 --- a/engine/resources/svc.go +++ b/engine/resources/svc.go @@ -255,23 +255,23 @@ func (obj *SvcRes) Watch(ctx context.Context) error { switch event[svc].ActiveState { case "active": - obj.init.Logf("started") + obj.init.Logf("event: started") case "inactive": - obj.init.Logf("stopped") + obj.init.Logf("event: stopped") case "reloading": - obj.init.Logf("reloading") + obj.init.Logf("event: reloading") case "failed": - obj.init.Logf("failed") + obj.init.Logf("event: failed") case "activating": - obj.init.Logf("activating") + obj.init.Logf("event: activating") case "deactivating": - obj.init.Logf("deactivating") + obj.init.Logf("event: deactivating") default: return fmt.Errorf("unknown svc state: %s", event[svc].ActiveState) } } else { // svc stopped (and ActiveState is nil...) - obj.init.Logf("stopped") + obj.init.Logf("event: stopped") } send = true @@ -370,35 +370,37 @@ func (obj *SvcRes) CheckApply(ctx context.Context, apply bool) (bool, error) { result := make(chan string, 1) // catch result information var status string - if obj.State == "running" { - _, err = conn.StartUnitContext(ctx, svc, SystemdUnitModeFail, result) - } else if obj.State == "stopped" { - _, err = conn.StopUnitContext(ctx, svc, SystemdUnitModeFail, result) - } - if err != nil { - return false, errwrap.Wrapf(err, "unable to change running status") - } - if refresh { - obj.init.Logf("Skipping reload, due to pending start/stop") - } - refresh = false // We did a start or stop, so a reload is not needed. + if !stateOK { + if obj.State == "running" { + _, err = conn.StartUnitContext(ctx, svc, SystemdUnitModeFail, result) + } else if obj.State == "stopped" { + _, err = conn.StopUnitContext(ctx, svc, SystemdUnitModeFail, result) + } + if err != nil { + return false, errwrap.Wrapf(err, "unable to change running status") + } + if refresh { + obj.init.Logf("skipping reload, due to pending start/stop") + } + refresh = false // We did a start or stop, so a reload is not needed. - // TODO: Do we need a timeout here? - select { - case status = <-result: - case <-ctx.Done(): - return false, ctx.Err() - } - if &status == nil { - return false, fmt.Errorf("systemd service action result is nil") - } - switch status { - case SystemdUnitResultDone: - // pass - case SystemdUnitResultFailed: - return false, fmt.Errorf("svc failed (selinux?)") - default: - return false, fmt.Errorf("unknown systemd return string: %v", status) + // TODO: Do we need a timeout here? + select { + case status = <-result: + case <-ctx.Done(): + return false, ctx.Err() + } + if &status == nil { + return false, fmt.Errorf("systemd service action result is nil") + } + switch status { + case SystemdUnitResultDone: + // pass + case SystemdUnitResultFailed: + return false, fmt.Errorf("svc failed (selinux?)") + default: + return false, fmt.Errorf("unknown systemd return string: %v", status) + } } // XXX: also set enabled on boot @@ -407,7 +409,7 @@ func (obj *SvcRes) CheckApply(ctx context.Context, apply bool) (bool, error) { return false, nil // success } - obj.init.Logf("Reloading...") + obj.init.Logf("reloading...") // From: https://www.freedesktop.org/software/systemd/man/latest/org.freedesktop.systemd1.html // If a service is restarted that isn't running, it will be started