engine: resources: Make some svc cleanups

We would often actually drop the refresh because of bad checks.
This commit is contained in:
James Shubin
2025-02-02 01:43:14 -05:00
parent 7439d532c7
commit 0fb546ad61

View File

@@ -255,23 +255,23 @@ func (obj *SvcRes) Watch(ctx context.Context) error {
switch event[svc].ActiveState { switch event[svc].ActiveState {
case "active": case "active":
obj.init.Logf("started") obj.init.Logf("event: started")
case "inactive": case "inactive":
obj.init.Logf("stopped") obj.init.Logf("event: stopped")
case "reloading": case "reloading":
obj.init.Logf("reloading") obj.init.Logf("event: reloading")
case "failed": case "failed":
obj.init.Logf("failed") obj.init.Logf("event: failed")
case "activating": case "activating":
obj.init.Logf("activating") obj.init.Logf("event: activating")
case "deactivating": case "deactivating":
obj.init.Logf("deactivating") obj.init.Logf("event: deactivating")
default: default:
return fmt.Errorf("unknown svc state: %s", event[svc].ActiveState) return fmt.Errorf("unknown svc state: %s", event[svc].ActiveState)
} }
} else { } else {
// svc stopped (and ActiveState is nil...) // svc stopped (and ActiveState is nil...)
obj.init.Logf("stopped") obj.init.Logf("event: stopped")
} }
send = true 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 result := make(chan string, 1) // catch result information
var status string var status string
if obj.State == "running" { if !stateOK {
_, err = conn.StartUnitContext(ctx, svc, SystemdUnitModeFail, result) if obj.State == "running" {
} else if obj.State == "stopped" { _, err = conn.StartUnitContext(ctx, svc, SystemdUnitModeFail, result)
_, err = conn.StopUnitContext(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 err != nil {
} return false, errwrap.Wrapf(err, "unable to change running status")
if refresh { }
obj.init.Logf("Skipping reload, due to pending start/stop") 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. }
refresh = false // We did a start or stop, so a reload is not needed.
// TODO: Do we need a timeout here? // TODO: Do we need a timeout here?
select { select {
case status = <-result: case status = <-result:
case <-ctx.Done(): case <-ctx.Done():
return false, ctx.Err() return false, ctx.Err()
} }
if &status == nil { if &status == nil {
return false, fmt.Errorf("systemd service action result is nil") return false, fmt.Errorf("systemd service action result is nil")
} }
switch status { switch status {
case SystemdUnitResultDone: case SystemdUnitResultDone:
// pass // pass
case SystemdUnitResultFailed: case SystemdUnitResultFailed:
return false, fmt.Errorf("svc failed (selinux?)") return false, fmt.Errorf("svc failed (selinux?)")
default: default:
return false, fmt.Errorf("unknown systemd return string: %v", status) return false, fmt.Errorf("unknown systemd return string: %v", status)
}
} }
// XXX: also set enabled on boot // 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 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 // 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 // If a service is restarted that isn't running, it will be started