admin/maintenance: skip legacy workers past their heartbeat timeout

A disconnected worker keeps its queue record until the cleanup loop removes
it, so the capability check also requires a heartbeat within the worker
timeout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-11-27 13:54:19 -08:00
1 parent 915dd259d9
commit 7e7dad030c
3 files changed
+12 -4

No files matched your search

@@ -179,7 +179,7 @@ func (mm *MaintenanceManager) IsRunning() bool {
// IsTaskTypeEnabled reports whether the running manager queues tasks of taskType
// and a registered worker can run them.
func (mm *MaintenanceManager) IsTaskTypeEnabled(taskType MaintenanceTaskType) bool {
return mm.IsRunning() && IsTaskEnabled(mm.scanner.policy, taskType) && mm.queue.hasWorkerFor(taskType)
return mm.IsRunning() && IsTaskEnabled(mm.scanner.policy, taskType) && mm.queue.hasWorkerFor(taskType, time.Duration(mm.config.WorkerTimeoutSeconds)*time.Second)
}
// Stop terminates the maintenance manager. It is a no-op when the manager is not
@@ -156,11 +156,15 @@ func TestMaintenanceManager_IsTaskTypeEnabled(t *testing.T) {
if manager.IsTaskTypeEnabled("ec_balance") {
t.Fatal("no registered worker can run ec_balance")
}
manager.queue.workers["w1"] = &MaintenanceWorker{ID: "w1", Status: "active", Capabilities: []MaintenanceTaskType{"vacuum"}}
manager.queue.workers["w1"] = &MaintenanceWorker{ID: "w1", Status: "active", LastHeartbeat: time.Now(), Capabilities: []MaintenanceTaskType{"vacuum"}}
if manager.IsTaskTypeEnabled("ec_balance") {
t.Fatal("the only worker cannot run ec_balance")
}
manager.queue.workers["w2"] = &MaintenanceWorker{ID: "w2", Status: "busy", Capabilities: []MaintenanceTaskType{"ec_balance"}}
manager.queue.workers["w2"] = &MaintenanceWorker{ID: "w2", Status: "active", LastHeartbeat: time.Now().Add(-time.Hour), Capabilities: []MaintenanceTaskType{"ec_balance"}}
if manager.IsTaskTypeEnabled("ec_balance") {
t.Fatal("a worker past its heartbeat timeout cannot run ec_balance")
}
manager.queue.workers["w3"] = &MaintenanceWorker{ID: "w3", Status: "busy", LastHeartbeat: time.Now(), Capabilities: []MaintenanceTaskType{"ec_balance"}}
if !manager.IsTaskTypeEnabled("ec_balance") {
t.Fatal("ec_balance is enabled and a worker can run it")
}
+5 -1
View File
@@ -925,13 +925,17 @@ func (mq *MaintenanceQueue) GetWorkers() []*MaintenanceWorker {
return workers
}
func (mq *MaintenanceQueue) hasWorkerFor(taskType MaintenanceTaskType) bool {
func (mq *MaintenanceQueue) hasWorkerFor(taskType MaintenanceTaskType, workerTimeout time.Duration) bool {
cutoff := time.Now().Add(-workerTimeout)
mq.mutex.RLock()
defer mq.mutex.RUnlock()
for _, worker := range mq.workers {
if worker.Status != "active" && worker.Status != "busy" {
continue
}
if workerTimeout > 0 && worker.LastHeartbeat.Before(cutoff) {
continue
}
for _, capability := range worker.Capabilities {
if capability == taskType {
return true