use chunks for cleanup
This commit is contained in:
@@ -16,7 +16,6 @@ using Microsoft.AspNetCore.SignalR;
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Security.Policy;
|
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
@@ -230,6 +229,7 @@ public class ServerFilesController : ControllerBase
|
|||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
fileLock.Release();
|
fileLock.Release();
|
||||||
|
fileLock.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -305,6 +305,7 @@ public class ServerFilesController : ControllerBase
|
|||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
fileLock.Release();
|
fileLock.Release();
|
||||||
|
fileLock.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -388,6 +389,7 @@ public class ServerFilesController : ControllerBase
|
|||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
fileLock.Release();
|
fileLock.Release();
|
||||||
|
fileLock.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -18,6 +18,10 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="IDisposableAnalyzers" Version="4.0.7">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="lz4net" Version="1.0.15.93" />
|
<PackageReference Include="lz4net" Version="1.0.15.93" />
|
||||||
<PackageReference Include="Meziantou.Analyzer" Version="2.0.93">
|
<PackageReference Include="Meziantou.Analyzer" Version="2.0.93">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
|||||||
@@ -164,12 +164,19 @@ public class FileCleanupService : IHostedService
|
|||||||
var prevTimeForcedDeletion = DateTime.Now.Subtract(TimeSpan.FromHours(forcedDeletionAfterHours));
|
var prevTimeForcedDeletion = DateTime.Now.Subtract(TimeSpan.FromHours(forcedDeletionAfterHours));
|
||||||
DirectoryInfo dir = new(_cacheDir);
|
DirectoryInfo dir = new(_cacheDir);
|
||||||
var allFilesInDir = dir.GetFiles("*", SearchOption.AllDirectories);
|
var allFilesInDir = dir.GetFiles("*", SearchOption.AllDirectories);
|
||||||
var allFiles = await dbContext.Files.ToListAsync().ConfigureAwait(false);
|
int filesToTake = 10000;
|
||||||
int fileCounter = 0;
|
var filesChunk = await dbContext.Files.Take(filesToTake).ToListAsync().ConfigureAwait(false);
|
||||||
foreach (var fileCache in allFiles.Where(f => f.Uploaded))
|
int iterations = 1;
|
||||||
|
var allFiles = new List<FileCache>();
|
||||||
|
while (filesChunk.Any())
|
||||||
|
{
|
||||||
|
int fileCounter = 0;
|
||||||
|
|
||||||
|
foreach (var fileCache in filesChunk.Where(f => f.Uploaded))
|
||||||
{
|
{
|
||||||
var file = FilePathUtil.GetFileInfoForHash(_cacheDir, fileCache.Hash);
|
|
||||||
bool fileDeleted = false;
|
bool fileDeleted = false;
|
||||||
|
|
||||||
|
var file = FilePathUtil.GetFileInfoForHash(_cacheDir, fileCache.Hash);
|
||||||
if (file == null && _isMainServer)
|
if (file == null && _isMainServer)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("File does not exist anymore: {fileName}", fileCache.Hash);
|
_logger.LogInformation("File does not exist anymore: {fileName}", fileCache.Hash);
|
||||||
@@ -214,6 +221,11 @@ public class FileCleanupService : IHostedService
|
|||||||
ct.ThrowIfCancellationRequested();
|
ct.ThrowIfCancellationRequested();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
allFiles.AddRange(filesChunk);
|
||||||
|
filesChunk = await dbContext.Files.Skip(filesToTake * iterations).Take(filesToTake).ToListAsync(cancellationToken: ct).ConfigureAwait(false);
|
||||||
|
iterations++;
|
||||||
|
}
|
||||||
|
|
||||||
// clean up files that are on disk but not in DB for some reason
|
// clean up files that are on disk but not in DB for some reason
|
||||||
CleanUpOrphanedFiles(allFiles, allFilesInDir, ct);
|
CleanUpOrphanedFiles(allFiles, allFilesInDir, ct);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,8 @@ public sealed class BlockFileDataStream : Stream
|
|||||||
|
|
||||||
protected override void Dispose(bool disposing)
|
protected override void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
base.Dispose(disposing);
|
if (disposing)
|
||||||
|
{
|
||||||
foreach (var substream in _substreams)
|
foreach (var substream in _substreams)
|
||||||
{
|
{
|
||||||
// probably unnecessary but better safe than sorry
|
// probably unnecessary but better safe than sorry
|
||||||
@@ -48,6 +49,9 @@ public sealed class BlockFileDataStream : Stream
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
base.Dispose(disposing);
|
||||||
|
}
|
||||||
|
|
||||||
public override void Flush()
|
public override void Flush()
|
||||||
{
|
{
|
||||||
throw new NotSupportedException();
|
throw new NotSupportedException();
|
||||||
|
|||||||
Reference in New Issue
Block a user