handle download errors on shards better

This commit is contained in:
rootdarkarchon
2024-01-18 21:00:14 +01:00
parent b0b349ca3a
commit 9f82f8a25a
2 changed files with 12 additions and 5 deletions

View File

@@ -69,7 +69,8 @@ public sealed class CachedFileProvider : IDisposable
}
var fileName = FilePathUtil.GetFilePath(_basePath, hash);
using var fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
var tempFileName = fileName + ".dl";
var fileStream = new FileStream(tempFileName, FileMode.Create, FileAccess.ReadWrite);
var bufferSize = response.Content.Headers.ContentLength > 1024 * 1024 ? 4096 : 1024;
var buffer = new byte[bufferSize];
@@ -79,6 +80,8 @@ public sealed class CachedFileProvider : IDisposable
{
await fileStream.WriteAsync(buffer.AsMemory(0, bytesRead)).ConfigureAwait(false);
}
await fileStream.DisposeAsync().ConfigureAwait(false);
File.Move(tempFileName, fileName, true);
_metrics.IncGauge(MetricsAPI.GaugeFilesTotal);
_metrics.IncGauge(MetricsAPI.GaugeFilesTotalSize, FilePathUtil.GetFileInfoForHash(_basePath, hash).Length);
@@ -90,7 +93,7 @@ public sealed class CachedFileProvider : IDisposable
if (fi == null && IsMainServer) return;
await _downloadSemaphore.WaitAsync().ConfigureAwait(false);
if (fi == null && !_currentTransfers.ContainsKey(hash))
if ((fi.Length == 0 || fi == null) && !_currentTransfers.ContainsKey(hash))
{
_currentTransfers[hash] = Task.Run(async () =>
{

View File

@@ -123,7 +123,6 @@ public class ShardFileCleanupService : IHostedService
var prevTimeForcedDeletion = DateTime.Now.Subtract(TimeSpan.FromHours(forcedDeletionAfterHours));
DirectoryInfo dir = new(_cacheDir);
var allFilesInDir = dir.GetFiles("*", SearchOption.AllDirectories);
int fileCounter = 0;
foreach (var file in allFilesInDir)
{
@@ -141,8 +140,13 @@ public class ShardFileCleanupService : IHostedService
_logger.LogInformation("File forcefully deleted: {fileName}, {fileSize}MiB", file.Name, ByteSize.FromBytes(file.Length).MebiBytes);
file.Delete();
}
fileCounter++;
else if (file.Length == 0 && !string.Equals(file.Extension, ".dl", StringComparison.OrdinalIgnoreCase))
{
_metrics.DecGauge(MetricsAPI.GaugeFilesTotalSize, file.Length);
_metrics.DecGauge(MetricsAPI.GaugeFilesTotal);
_logger.LogInformation("File with size 0 deleted: {filename}", file.Name);
file.Delete();
}
ct.ThrowIfCancellationRequested();
}