From 01aae2fabc178d03b4ee2db0d0059b8f14e2dc7a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 26 Dec 2023 20:59:04 -0600 Subject: add logic to automatically re-scan shares --- src/slskd/Application.cs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 37bc4f78..be7dde15 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -761,6 +761,42 @@ namespace slskd private void Clock_EveryHour(object sender, ClockEventArgs e) { + _ = Task.Run(() => MaybeRescanShares()); + } + + private async Task MaybeRescanShares() + { + // ignore this if we're already scanning. there are multiple safeguards using + // SemaphoreSlim later in the call chain if we manage to slip by somehow, though + if (Shares.StateMonitor.CurrentValue.Scanning) + { + return; + } + + var ttl = Options.Shares.Cache.Retention; + + // no configured TTL; never re-scan automatically + if (!ttl.HasValue == default) + { + return; + } + + // "x minutes ago" + var cutoffTimestamp = DateTimeOffset.UtcNow + .AddMinutes(-ttl.Value) + .ToUnixTimeMilliseconds(); + + // get all scan records that started on or after our cutoff. we can't rely on + // EndedAt here because one or more prior scans may have failed, leaving the + // field null. we still want to attempt to re-scan if that is the case. we will rely + // on the locking implemented in the service to prevent overlapping scans + var matchingScans = await Shares.ListScansAsync(s => s.StartedAt >= cutoffTimestamp); + + if (!matchingScans.Any()) + { + // fire and forget. if something slips through the underlying logic will log. + _ = Task.Run(() => Shares.RequestScan()); + } } private void PruneFiles() -- cgit v1.2.3