aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2023-12-26 20:59:04 -0600
committerJP Dillingham <jp@dillingham.me>2023-12-26 20:59:04 -0600
commit01aae2fabc178d03b4ee2db0d0059b8f14e2dc7a (patch)
treec70ed34f72ae443a7c69534088e6e97ffdff2020 /src
parente0453b237211da8a116d9b9dce24eae5f43d20f9 (diff)
downloadslskd-01aae2fabc178d03b4ee2db0d0059b8f14e2dc7a.tar.gz
add logic to automatically re-scan shares
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Application.cs36
1 files changed, 36 insertions, 0 deletions
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()