From 5cd4c1bebe1303badd342fd7667e6b32c050e65f Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Tue, 19 Mar 2024 16:58:56 +0100 Subject: [PATCH] e2e: fix TestWalletFormUnconfirmed --- autopilot/autopilot.go | 3 +++ autopilot/scanner.go | 40 ++++++++++++++------------------------- autopilot/scanner_test.go | 4 ---- 3 files changed, 17 insertions(+), 30 deletions(-) diff --git a/autopilot/autopilot.go b/autopilot/autopilot.go index 6693690bc..286b3253a 100644 --- a/autopilot/autopilot.go +++ b/autopilot/autopilot.go @@ -282,6 +282,9 @@ func (ap *Autopilot) Run() error { return } + // prune hosts that have been offline for too long + ap.s.PruneHosts(ap.shutdownCtx, autopilot.Config.Hosts) + // Log worker id chosen for this maintenance iteration. workerID, err := w.ID(ap.shutdownCtx) if err != nil { diff --git a/autopilot/scanner.go b/autopilot/scanner.go index ebab01468..e82fd8a2b 100644 --- a/autopilot/scanner.go +++ b/autopilot/scanner.go @@ -32,7 +32,6 @@ type ( // a bit, we currently use inline interfaces to avoid having to update the // scanner tests with every interface change bus interface { - Autopilot(ctx context.Context, id string) (autopilot api.Autopilot, err error) Hosts(ctx context.Context, opts api.GetHostsOptions) ([]hostdb.Host, error) HostsForScanning(ctx context.Context, opts api.HostsForScanningOptions) ([]hostdb.HostAddress, error) RemoveOfflineHosts(ctx context.Context, minRecentScanFailures uint64, maxDowntime time.Duration) (uint64, error) @@ -199,44 +198,33 @@ func (s *scanner) tryPerformHostScan(ctx context.Context, w scanWorker, force bo go func(st string) { defer s.wg.Done() - var interrupted bool for resp := range s.launchScanWorkers(ctx, w, s.launchHostScans()) { if s.isInterrupted() || s.ap.isStopped() { - interrupted = true break } if resp.err != nil && !strings.Contains(resp.err.Error(), "connection refused") { s.logger.Error(resp.err) } } - - // fetch the config right before removing offline hosts to get the most - // recent settings in case they were updated while scanning. - autopilot, err := s.bus.Autopilot(ctx, s.ap.id) - if err != nil { - s.logger.Errorf("tryPerformHostScan: failed to fetch autopilot config: %v", err) - return - } - hostCfg := autopilot.Config.Hosts - maxDowntime := time.Duration(hostCfg.MaxDowntimeHours) * time.Hour - minRecentScanFailures := hostCfg.MinRecentScanFailures - - if !interrupted && maxDowntime > 0 { - s.logger.Debugf("removing hosts that have been offline for more than %v and have failed at least %d scans", maxDowntime, minRecentScanFailures) - removed, err := s.bus.RemoveOfflineHosts(ctx, minRecentScanFailures, maxDowntime) - if err != nil { - s.logger.Errorf("error occurred while removing offline hosts, err: %v", err) - } else if removed > 0 { - s.logger.Infof("removed %v offline hosts", removed) - } - } - s.mu.Lock() s.scanning = false s.logger.Debugf("%s finished after %v", st, time.Since(s.scanningLastStart)) s.mu.Unlock() }(scanType) - return +} + +func (s *scanner) PruneHosts(ctx context.Context, cfg api.HostsConfig) { + maxDowntime := time.Duration(cfg.MaxDowntimeHours) * time.Hour + minRecentScanFailures := cfg.MinRecentScanFailures + if maxDowntime > 0 { + s.logger.Debugf("removing hosts that have been offline for more than %v and have failed at least %d scans", maxDowntime, minRecentScanFailures) + removed, err := s.bus.RemoveOfflineHosts(ctx, minRecentScanFailures, maxDowntime) + if err != nil { + s.logger.Errorf("error occurred while removing offline hosts, err: %v", err) + } else if removed > 0 { + s.logger.Infof("removed %v offline hosts", removed) + } + } } func (s *scanner) tryUpdateTimeout() { diff --git a/autopilot/scanner_test.go b/autopilot/scanner_test.go index c1f20908e..50d5792e1 100644 --- a/autopilot/scanner_test.go +++ b/autopilot/scanner_test.go @@ -20,10 +20,6 @@ type mockBus struct { reqs []string } -func (b *mockBus) Autopilot(ctx context.Context, id string) (autopilot api.Autopilot, err error) { - return api.Autopilot{}, nil -} - func (b *mockBus) Hosts(ctx context.Context, opts api.GetHostsOptions) ([]hostdb.Host, error) { b.reqs = append(b.reqs, fmt.Sprintf("%d-%d", opts.Offset, opts.Offset+opts.Limit))