Catch exceptions in expired deletion service

This commit is contained in:
Kieran 2023-04-09 14:26:51 +01:00
parent de975be92f
commit 1c370dfd24
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941

View File

@ -22,28 +22,36 @@ public sealed class DeleteExpiredFiles : BackgroundService
{ {
while (!stoppingToken.IsCancellationRequested) while (!stoppingToken.IsCancellationRequested)
{ {
using var scope = _scopeFactory.CreateScope(); try
var metadata = scope.ServiceProvider.GetRequiredService<IFileMetadataStore>();
var fileInfoManager = scope.ServiceProvider.GetRequiredService<FileInfoManager>();
var fileStoreFactory = scope.ServiceProvider.GetRequiredService<FileStoreFactory>();
var files = await metadata.ListFiles<SecretFileMeta>(new(0, int.MaxValue));
await foreach (var f in files.Results.WithCancellation(stoppingToken))
{ {
try using var scope = _scopeFactory.CreateScope();
{ var metadata = scope.ServiceProvider.GetRequiredService<IFileMetadataStore>();
if (f.Expires < DateTime.Now) var fileInfoManager = scope.ServiceProvider.GetRequiredService<FileInfoManager>();
{ var fileStoreFactory = scope.ServiceProvider.GetRequiredService<FileStoreFactory>();
await fileStoreFactory.DeleteFile(f.Id);
await fileInfoManager.Delete(f.Id);
_logger.LogInformation("Deleted file: {Id}", f.Id); var files = await metadata.ListFiles<SecretFileMeta>(new(0, int.MaxValue));
await foreach (var f in files.Results.WithCancellation(stoppingToken))
{
try
{
if (f.Expires < DateTime.Now)
{
await fileStoreFactory.DeleteFile(f.Id);
await fileInfoManager.Delete(f.Id);
_logger.LogInformation("Deleted file: {Id}", f.Id);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to delete file: {Id}", f.Id);
} }
} }
catch (Exception ex)
{ }
_logger.LogError(ex, "Failed to delete file: {Id}", f.Id); catch (Exception ex)
} {
_logger.LogError(ex, "Failed to run delete expired file services");
} }
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken); await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);