Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Rollback restores an inventory job that was scheduled before the install

shukieshah9cacb2682d05
diff --git a/cli/beacon/internal/endpoint/lifecycle/inventory_job_test.go b/cli/beacon/internal/endpoint/lifecycle/inventory_job_test.go
index 9e1f965b..a32f574e 100644
--- a/cli/beacon/internal/endpoint/lifecycle/inventory_job_test.go
+++ b/cli/beacon/internal/endpoint/lifecycle/inventory_job_test.go
@@ -18,6 +18,8 @@ type recordingInventoryJob struct {
supported bool
unitPath string
loadErr error
+ // scheduled makes Status report an already-loaded job, the reinstall case.
+ scheduled bool
}
func (r recordingInventoryJob) Supported() bool { return r.supported }
@@ -36,7 +38,7 @@ func (r recordingInventoryJob) Load() error {
}
func (r recordingInventoryJob) Unload() error { *r.calls = append(*r.calls, "unload"); return nil }
func (r recordingInventoryJob) RemoveUnits() { *r.calls = append(*r.calls, "remove") }
-func (r recordingInventoryJob) Status() service.Status { return service.Status{} }
+func (r recordingInventoryJob) Status() service.Status { return service.Status{Loaded: r.scheduled} }
func installFakeInventoryJob(t *testing.T, supported bool) *[]string {
return installFakeInventoryJobWithLoadError(t, supported, nil)
@@ -269,6 +271,15 @@ func TestRollbackUnloadsALoadedInventoryJob(t *testing.T) {
if len(*calls) != 0 {
t.Fatalf("rollback must not unload a job it never loaded: %q", *calls)
}
+ // A job that was already scheduled before the install is brought back, not torn down: the
+ // failed reinstall must not end inventory on a healthy endpoint.
+ *calls = nil
+ tx.InventoryLoaded = true
+ tx.InventoryWasLoaded = true
+ tx.Rollback(Manifest{})
+ if strings.Join(*calls, ",") != "load" {
+ t.Fatalf("rollback of a reinstall should re-load the previously scheduled job, got %q", *calls)
+ }
}
func TestReconcileSurfacesARefusedLoad(t *testing.T) {
diff --git a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go b/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
index ef3f5197..8e32b37e 100644
--- a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
+++ b/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
@@ -296,10 +296,13 @@ type serviceController interface {
type installRollback struct {
// InventoryJob and InventoryLoaded let a failed install unload the scheduled inventory job
// it started, so the scheduler is not left registered against rolled-back config.
- InventoryJob inventoryJobController
- InventoryLoaded bool
- Manager serviceController
- ServiceLoaded bool
+ // InventoryWasLoaded records a job that was already scheduled before this install began; a
+ // reinstall that re-enabled it and then failed must bring it back, exactly like the collector.
+ InventoryJob inventoryJobController
+ InventoryLoaded bool
+ InventoryWasLoaded bool
+ Manager serviceController
+ ServiceLoaded bool
// ServiceWasRunning records whether a collector was already up when this install began.
//
// It decides what rollback owes the machine. Unloading is right for a service this transaction
@@ -347,7 +350,7 @@ func (r *installRollback) Rollback(manifest Manifest) {
if r.ServiceLoaded {
_ = r.Manager.Unload()
}
- if r.InventoryLoaded && r.InventoryJob != nil {
+ if r.InventoryLoaded && r.InventoryJob != nil && !r.InventoryWasLoaded {
_ = r.InventoryJob.Unload()
}
@@ -371,6 +374,12 @@ func (r *installRollback) Rollback(manifest Manifest) {
if r.ServiceLoaded && r.ServiceWasRunning {
_ = r.Manager.Load()
}
+ // Same claim for the inventory job: its unit file has just been restored to what was scheduled
+ // before, so re-loading it puts the schedule back rather than leaving it stopped and, on
+ // systemd, disabled across reboot.
+ if r.InventoryLoaded && r.InventoryWasLoaded && r.InventoryJob != nil {
+ _ = r.InventoryJob.Load()
+ }
}
func Install(opts InstallOptions) (InstallResult, error) {
@@ -446,6 +455,7 @@ func Install(opts InstallOptions) (InstallResult, error) {
tx.Track(path)
}
tx.InventoryJob = inventoryJob
+ tx.InventoryWasLoaded = inventoryJob.Status().Loaded
inventoryResult, inventoryErr := reconcileInventoryJobWith(inventoryJob, InventoryJobOptions{
UserMode: cfg.UserMode,
Kind: opts.ServiceKind,