Additional logging
This commit is contained in:
@@ -20,6 +20,7 @@ public sealed class SnapshotService : IDisposable, ISnapshotService
|
||||
private readonly ConfigManager _config;
|
||||
private readonly NSPExtractor _nspExtractor;
|
||||
private readonly ArchiveHandler _archiveHandler;
|
||||
private readonly ILogger<SnapshotService> _logger;
|
||||
private readonly string _jsonPath;
|
||||
private readonly string _snapshotPath;
|
||||
private readonly List<FileSystemWatcher> _watchers = new();
|
||||
@@ -27,11 +28,12 @@ public sealed class SnapshotService : IDisposable, ISnapshotService
|
||||
private string? _currentSnapshotHash;
|
||||
public event EventHandler? SnapshotRebuilt;
|
||||
|
||||
public SnapshotService(ConfigManager config, NSPExtractor nspExtractor, ArchiveHandler archiveHandler)
|
||||
public SnapshotService(ConfigManager config, NSPExtractor nspExtractor, ArchiveHandler archiveHandler, ILogger<SnapshotService> logger)
|
||||
{
|
||||
_config = config;
|
||||
_nspExtractor = nspExtractor;
|
||||
_archiveHandler = archiveHandler;
|
||||
_logger = logger;
|
||||
_jsonPath = Path.Combine(AppContext.BaseDirectory, _config.Settings.SnapshotFile);
|
||||
_snapshotPath = Path.Combine(AppContext.BaseDirectory, _config.Settings.SnapshotBackupFile);
|
||||
|
||||
@@ -70,35 +72,34 @@ public sealed class SnapshotService : IDisposable, ISnapshotService
|
||||
|
||||
private void InitializeFileSystemWatcher(string path)
|
||||
{
|
||||
if (Directory.Exists(path))
|
||||
if (!Directory.Exists(path)) return;
|
||||
var watcher = new FileSystemWatcher
|
||||
{
|
||||
var _watcher = new FileSystemWatcher
|
||||
{
|
||||
Path = path,
|
||||
IncludeSubdirectories = true,
|
||||
NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName |
|
||||
NotifyFilters.Size | NotifyFilters.LastWrite
|
||||
};
|
||||
_watcher.Created += OnChanged;
|
||||
_watcher.Changed += OnChanged;
|
||||
_watcher.Deleted += OnChanged;
|
||||
_watcher.Renamed += OnRenamed;
|
||||
_watcher.EnableRaisingEvents = true;
|
||||
Path = path,
|
||||
IncludeSubdirectories = true,
|
||||
NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName |
|
||||
NotifyFilters.Size | NotifyFilters.LastWrite
|
||||
};
|
||||
watcher.Created += OnChanged;
|
||||
watcher.Changed += OnChanged;
|
||||
watcher.Deleted += OnChanged;
|
||||
watcher.Renamed += OnRenamed;
|
||||
watcher.EnableRaisingEvents = true;
|
||||
|
||||
_watchers.Add(_watcher);
|
||||
}
|
||||
_watchers.Add(watcher);
|
||||
}
|
||||
|
||||
#region FileSystemWatcher
|
||||
|
||||
private void OnChanged(object? _, FileSystemEventArgs e) => ThrottleSnapshotUpdate();
|
||||
private void OnRenamed(object? _, RenamedEventArgs e) => ThrottleSnapshotUpdate();
|
||||
private void OnChanged(object? _, FileSystemEventArgs e) => ThrottleSnapshotUpdate(e);
|
||||
private void OnRenamed(object? _, RenamedEventArgs e) => ThrottleSnapshotUpdate(e);
|
||||
|
||||
private void ThrottleSnapshotUpdate()
|
||||
private void ThrottleSnapshotUpdate(FileSystemEventArgs fileSystemEventArgs)
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(250);
|
||||
_logger.LogDebug("File system event {EventType} on {Path}", fileSystemEventArgs.ChangeType, fileSystemEventArgs.FullPath);
|
||||
UpdateSnapshot();
|
||||
});
|
||||
}
|
||||
@@ -110,8 +111,9 @@ public sealed class SnapshotService : IDisposable, ISnapshotService
|
||||
private void BuildSnapshot()
|
||||
{
|
||||
var cfg = _config.Settings;
|
||||
_logger.LogInformation("Rebuilding snapshot (root dirs: {Count})", cfg.RootDirectories.Length);
|
||||
var entries = new List<FileEntry>();
|
||||
var index = LoadSnapshotIndex(); // <‑ new
|
||||
var index = LoadSnapshotIndex();
|
||||
|
||||
var snapshotChanged = false;
|
||||
foreach (var dir in cfg.RootDirectories)
|
||||
@@ -149,11 +151,16 @@ public sealed class SnapshotService : IDisposable, ISnapshotService
|
||||
title = _archiveHandler.TryExtractTitleInfo(file);
|
||||
}
|
||||
|
||||
if (title == null)
|
||||
{
|
||||
_logger.LogInformation("Failed to process {File}", file);
|
||||
}
|
||||
// 4) update cache
|
||||
_cache[file] = new CachedFile(file, hash, title);
|
||||
|
||||
// 5) add to snapshot
|
||||
entries.Add(new FileEntry(file, new FileInfo(file).Length, hash, title));
|
||||
_logger.LogInformation("Added {File} to snapshot (hash={Hash}", file, hash);
|
||||
snapshotChanged = true;
|
||||
}
|
||||
}
|
||||
@@ -180,6 +187,7 @@ public sealed class SnapshotService : IDisposable, ISnapshotService
|
||||
var newHash = ComputeSnapshotHash(entries);
|
||||
if (_currentSnapshotHash != newHash)
|
||||
{
|
||||
_logger.LogInformation("Snapshot hash changed – persisting new snapshot");
|
||||
_currentSnapshotHash = newHash;
|
||||
File.WriteAllText(_jsonPath, JsonSerializer.Serialize(entries));
|
||||
File.WriteAllText(_snapshotPath, JsonSerializer.Serialize(entries));
|
||||
|
||||
Reference in New Issue
Block a user