When a file is locked during hash calculation, if retries fails then do not throw exception out but rather return null hash #7
@@ -221,8 +221,23 @@ public sealed class SnapshotService : IDisposable, ISnapshotService, IHostedServ
|
|||||||
_watchers.Remove(fileSystemWatchers);
|
_watchers.Remove(fileSystemWatchers);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnChanged(object? _, FileSystemEventArgs e) => ThrottleSnapshotUpdate(e);
|
private void OnChanged(object? _, FileSystemEventArgs e)
|
||||||
private void OnRenamed(object? _, RenamedEventArgs e) => ThrottleSnapshotUpdate(e);
|
{
|
||||||
|
var fileInfo = new FileInfo(e.FullPath);
|
||||||
|
if (_options.ArchiveExtensions.Contains(fileInfo.Extension) || _options.RomExtensions.Contains(fileInfo.Extension))
|
||||||
|
{
|
||||||
|
ThrottleSnapshotUpdate(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnRenamed(object? _, RenamedEventArgs e)
|
||||||
|
{
|
||||||
|
var fileInfo = new FileInfo(e.FullPath);
|
||||||
|
if (_options.ArchiveExtensions.Contains(fileInfo.Extension) || _options.RomExtensions.Contains(fileInfo.Extension))
|
||||||
|
{
|
||||||
|
ThrottleSnapshotUpdate(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Rebuild the snapshot, if rebuild in process, cancel it and restart
|
/// Rebuild the snapshot, if rebuild in process, cancel it and restart
|
||||||
@@ -421,7 +436,7 @@ public sealed class SnapshotService : IDisposable, ISnapshotService, IHostedServ
|
|||||||
return fileInfo.LastWriteTimeUtc;
|
return fileInfo.LastWriteTimeUtc;
|
||||||
}))
|
}))
|
||||||
{
|
{
|
||||||
string hash;
|
string? hash;
|
||||||
var ext = Path.GetExtension(file).ToLowerInvariant();
|
var ext = Path.GetExtension(file).ToLowerInvariant();
|
||||||
|
|
||||||
if (!(_options.ArchiveExtensions.Contains(ext) || _options.RomExtensions.Contains(ext)))
|
if (!(_options.ArchiveExtensions.Contains(ext) || _options.RomExtensions.Contains(ext)))
|
||||||
@@ -474,12 +489,15 @@ public sealed class SnapshotService : IDisposable, ISnapshotService, IHostedServ
|
|||||||
Stopwatch stopwatch = Stopwatch.StartNew();
|
Stopwatch stopwatch = Stopwatch.StartNew();
|
||||||
hash = ComputeFirstStreamHashAsync(file, cancellationToken).Result;
|
hash = ComputeFirstStreamHashAsync(file, cancellationToken).Result;
|
||||||
stopwatch.Stop();
|
stopwatch.Stop();
|
||||||
_logger.LogDebug("Computed hash for {File} in {Time}ms", file, stopwatch.ElapsedMilliseconds);
|
if (!string.IsNullOrEmpty(hash))
|
||||||
if (_hashCache.TryGetValue(hash, out var value) && file == _cache[value].Path)
|
|
||||||
{
|
{
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
_logger.LogDebug("Computed hash for {File} in {Time}ms", file, stopwatch.ElapsedMilliseconds);
|
||||||
yield return null;
|
if (_hashCache.TryGetValue(hash, out var value) && file == _cache[value].Path)
|
||||||
continue;
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
yield return null;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
IEnumerable<(string, long, NcaMetadataWithHash)>? titlesEnumerable = null;
|
IEnumerable<(string, long, NcaMetadataWithHash)>? titlesEnumerable = null;
|
||||||
@@ -572,7 +590,7 @@ public sealed class SnapshotService : IDisposable, ISnapshotService, IHostedServ
|
|||||||
{
|
{
|
||||||
if (_debouncerCache.TryGetValue(_jsonPath, out _))
|
if (_debouncerCache.TryGetValue(_jsonPath, out _))
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Sliding debounce in progress, skipping snapshot persistence");
|
_logger.LogDebug("Sliding debounce in progress, skipping snapshot persistence");
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -821,7 +839,7 @@ public sealed class SnapshotService : IDisposable, ISnapshotService, IHostedServ
|
|||||||
|
|
||||||
// File: TinfoilVibeServer/Services/SnapshotService.cs (inside SnapshotService class)
|
// File: TinfoilVibeServer/Services/SnapshotService.cs (inside SnapshotService class)
|
||||||
|
|
||||||
private async Task<string> ComputeFirstStreamHashAsync(string filePath, CancellationToken cancellationToken = default)
|
private async Task<string?> ComputeFirstStreamHashAsync(string filePath, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
for (var attempt = 0; attempt < _options.MaxRetryCount; attempt++)
|
for (var attempt = 0; attempt < _options.MaxRetryCount; attempt++)
|
||||||
@@ -832,7 +850,7 @@ public sealed class SnapshotService : IDisposable, ISnapshotService, IHostedServ
|
|||||||
{
|
{
|
||||||
throw new IOException("File is locked");
|
throw new IOException("File is locked");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only treat NSP/XCI/XCZ as “first‑stream” files
|
// Only treat NSP/XCI/XCZ as “first‑stream” files
|
||||||
var ext = Path.GetExtension(filePath).ToLowerInvariant();
|
var ext = Path.GetExtension(filePath).ToLowerInvariant();
|
||||||
if (ext is not ".nsp" and not ".xci" and not ".xcz")
|
if (ext is not ".nsp" and not ".xci" and not ".xcz")
|
||||||
@@ -868,11 +886,16 @@ public sealed class SnapshotService : IDisposable, ISnapshotService, IHostedServ
|
|||||||
}
|
}
|
||||||
catch (IOException ex) when (attempt < _options.MaxRetryCount - 1)
|
catch (IOException ex) when (attempt < _options.MaxRetryCount - 1)
|
||||||
{
|
{
|
||||||
var delay = (int)((attempt+1) * _options.DebounceTimeoutMs * _options.RetryMultiplier);
|
var delay = (int)((attempt + 1) * _options.DebounceTimeoutMs * _options.RetryMultiplier);
|
||||||
_logger.LogWarning(ex, "Attempt {Attempt} failed for {Path}. Retrying after {Delay}.",
|
_logger.LogWarning(ex, "Attempt {Attempt} failed for {Path}. Retrying after {Delay}.",
|
||||||
attempt + 1, filePath, delay);
|
attempt + 1, filePath, delay);
|
||||||
await Task.Delay(delay, cancellationToken);
|
await Task.Delay(delay, cancellationToken);
|
||||||
}
|
}
|
||||||
|
catch (IOException)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Attempt to load {Path} failed after {retries}", filePath, attempt + 1);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
|
|||||||
Reference in New Issue
Block a user