Skip to content

Commit

Permalink
Improved deletion sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
HeDo88TH committed Nov 27, 2024
1 parent 848c428 commit bcf0468
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions Registry.Web/Services/Managers/ObjectsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,29 @@ public async Task Delete(string orgSlug, string dsSlug, string path)

var objs = (await ddb.SearchAsync(path, true)).ToArray();

foreach (var obj in objs.Where(item => item.Type != EntryType.Directory))
// Let's delete from DDB first
try
{
_logger.LogInformation("Removing from DDB");
await ddb.RemoveAsync(path);
}
catch (Exception ex)
{
_logger.LogInformation("Deleting '{ObjPath}'", obj.Path);
_logger.LogWarning("Cannot delete {ObjPath} from DDB: {Reason}", path, ex.Message);
throw new InvalidOperationException("Cannot delete object from database", ex);
}

var filesToDelete = objs.Where(item => item.Type != EntryType.Directory).ToArray();

_logger.LogInformation("Deleting {FilesCount} files", filesToDelete.Length);

// Then we delete from the file system
foreach (var obj in filesToDelete)
{
var objLocalPath = ddb.GetLocalPath(obj.Path);

_logger.LogInformation("Deleting {ObjPath} in physical path {PhysicalPath}", obj.Path, objLocalPath);

try
{
if (!_fs.Exists(objLocalPath))
Expand All @@ -370,27 +387,20 @@ public async Task Delete(string orgSlug, string dsSlug, string path)

_fs.Delete(objLocalPath);

_logger.LogInformation("Local file deleted, now clearing cache with hash {Hash}", obj.Hash);

_cacheManager.Clear(MagicStrings.ThumbnailCacheSeed, obj.Hash);
_cacheManager.Clear(MagicStrings.TileCacheSeed, obj.Hash);

_logger.LogInformation("Cache cleared");
}
catch (Exception ex)
{
// We basically ignore this error, it's not critical. We should perform a cleanup later
_logger.LogWarning("Cannot delete local file '{ObjPath}': {Reason}", objLocalPath, ex.Message);
_logger.LogWarning("Cannot delete local file {ObjPath}: {Reason}", objLocalPath, ex.Message);
}
}

try
{
_logger.LogInformation("Removing from DDB");
await ddb.RemoveAsync(path);
}
catch (Exception ex)
{
_logger.LogWarning("Cannot delete '{ObjPath}' from DDB: {Reason}", path, ex.Message);
throw new InvalidOperationException("Cannot delete object from database", ex);
}

_logger.LogInformation("Deletion complete");
}

Expand Down

0 comments on commit bcf0468

Please sign in to comment.