Files
Chronos/src/Titano.Uploader/Watching/WatchFolderService.cs
T
Claude 27f0d73f05 feat(uploader): watch folders with stable-file detection and processed-file memory
Polls each folder at its interval with FileSystemWatcher hints on local
disks (network shares are polled only); a photo is queued once its size
and write time are unchanged in two consecutive checks. Files the server
confirmed are remembered by path, size and write time in
uploader-watch.json together with each folder's last session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 10:27:24 +00:00

189 lines
7.8 KiB
C#

using System.IO;
using Titano.Core.Diagnostics;
using Titano.Uploader.Sources;
namespace Titano.Uploader.Watching;
/// <summary>
/// Polls each watch folder at its interval, with <see cref="FileSystemWatcher"/> hints on local disks (change
/// notifications are unreliable on network shares, which are only polled). A photo is reported once it is stable
/// (<see cref="StableFileTracker"/>), not yet confirmed by the server (<see cref="ProcessedFileMemory"/>) and not queued.
/// </summary>
public sealed class WatchFolderService(ProcessedFileMemory memory, Func<string, bool> isQueued) : IDisposable
{
/// <summary>Re-check cadence while some file is still growing.</summary>
private static readonly TimeSpan QuickCheck = TimeSpan.FromSeconds(10);
/// <summary>Lets a burst of change notifications settle before scanning.</summary>
private static readonly TimeSpan HintDebounce = TimeSpan.FromSeconds(2);
private readonly Dictionary<string, Folder> _folders = new(StringComparer.OrdinalIgnoreCase);
private readonly ProcessedFileMemory _memory = memory;
private readonly Func<string, bool> _isQueued = isQueued;
/// <summary>Stable new photos of a folder; raised on the thread that called <see cref="Watch"/>.</summary>
public event Action<string, IReadOnlyList<SourceFile>>? FilesReady;
/// <summary>A folder became reachable (true) or unreachable (false); raised like <see cref="FilesReady"/>.</summary>
public event Action<string, bool>? AvailabilityChanged;
/// <summary>UNC paths and mapped network drives.</summary>
public static bool IsNetworkPath(string path)
{
if (path.StartsWith(@"\\", StringComparison.Ordinal)) return true;
try
{
var root = Path.GetPathRoot(Path.GetFullPath(path));
return !string.IsNullOrEmpty(root) && new DriveInfo(root).DriveType == DriveType.Network;
}
catch (Exception ex) when (ex is ArgumentException or IOException)
{
return false;
}
}
/// <summary>60 s on local disks; 5 min on network shares, where every poll lists the share.</summary>
public static TimeSpan DefaultInterval(string path) => IsNetworkPath(path) ? TimeSpan.FromMinutes(5) : TimeSpan.FromSeconds(60);
/// <summary>Starts (or restarts with a new interval) watching <paramref name="root"/>.</summary>
public void Watch(string root, TimeSpan interval)
{
Stop(root);
var folder = new Folder(root, interval, IsNetworkPath(root), SynchronizationContext.Current, this);
_folders[root] = folder;
folder.Start();
}
public void Stop(string root)
{
if (_folders.Remove(root, out var folder)) folder.Dispose();
}
public void Dispose()
{
foreach (var f in _folders.Values) f.Dispose();
_folders.Clear();
}
private void Raise(SynchronizationContext? context, Action action)
{
if (context is null) action();
else context.Post(_ => action(), null);
}
private sealed class Folder(string root, TimeSpan interval, bool network, SynchronizationContext? context, WatchFolderService owner) : IDisposable
{
private readonly StableFileTracker _tracker = new();
private readonly CancellationTokenSource _cts = new();
private FileSystemWatcher? _watcher;
private TaskCompletionSource _hint = NewHint();
private bool? _available;
public void Start() => _ = Task.Run(() => LoopAsync(_cts.Token));
public void Dispose()
{
_cts.Cancel();
_watcher?.Dispose();
_watcher = null;
}
private async Task LoopAsync(CancellationToken ct)
{
while (!ct.IsCancellationRequested)
{
// Replaced before the scan so notifications arriving during it trigger the next one.
Volatile.Write(ref _hint, NewHint());
var available = Scan(ct);
if (available != _available)
{
_available = available;
if (available && !network) StartWatcher();
owner.Raise(context, () => owner.AvailabilityChanged?.Invoke(root, available));
}
var wait = _tracker.PendingCount > 0 && QuickCheck < interval ? QuickCheck : interval;
try
{
var hint = Volatile.Read(ref _hint).Task;
if (await Task.WhenAny(Task.Delay(wait, ct), hint).ConfigureAwait(false) == hint)
await Task.Delay(HintDebounce, ct).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
return;
}
}
}
/// <summary>One complete listing; false when the folder cannot be reached.</summary>
private bool Scan(CancellationToken ct)
{
if (!Directory.Exists(root)) return false;
var present = new List<string>();
var candidates = new Dictionary<string, FileInfo>(StringComparer.OrdinalIgnoreCase);
try
{
foreach (var file in PhotoScanner.Files(root, ct))
{
present.Add(file.FullName);
if (IsKnown(file)) continue;
candidates[file.FullName] = file;
}
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
// A partial listing must not prune the memory or report files.
Log.Info("watch", $"{root} not readable: {ex.Message}");
return false;
}
catch (OperationCanceledException)
{
return _available ?? false;
}
owner._memory.Prune(root, present);
var stable = _tracker.Check(candidates.Values.Select(f => new FileObservation(f.FullName, f.Length, f.LastWriteTimeUtc)));
if (stable.Count > 0)
{
var files = stable.Select(o => PhotoScanner.ToSource(candidates[o.Path], root)).ToList();
Log.Info("watch", $"{files.Count} new file(s) in {root}");
owner.Raise(context, () => owner.FilesReady?.Invoke(root, files));
}
return true;
}
/// <summary>Already confirmed by the server, or already in the queue.</summary>
private bool IsKnown(FileInfo file) =>
owner._memory.Contains(root, file.FullName, file.Length, file.LastWriteTimeUtc) || owner._isQueued(file.FullName);
private void StartWatcher()
{
_watcher?.Dispose();
try
{
_watcher = new FileSystemWatcher(root)
{
IncludeSubdirectories = true,
NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size | NotifyFilters.LastWrite,
};
_watcher.Created += (_, _) => Hint();
_watcher.Changed += (_, _) => Hint();
_watcher.Renamed += (_, _) => Hint();
// A lost buffer means unknown changes: scan.
_watcher.Error += (_, _) => Hint();
_watcher.EnableRaisingEvents = true;
}
catch (Exception ex) when (ex is ArgumentException or IOException or PlatformNotSupportedException)
{
Log.Info("watch", $"No change notifications for {root}; polling only: {ex.Message}");
_watcher?.Dispose();
_watcher = null;
}
}
private void Hint() => Volatile.Read(ref _hint).TrySetResult();
private static TaskCompletionSource NewHint() => new(TaskCreationOptions.RunContinuationsAsynchronously);
}
}