fix(cv-import): bound source retention and guard worker recovery

This commit is contained in:
cesnimda
2026-10-26 14:41:53 +01:00
parent e8f76a9dea
commit 9e1726c61d
14 changed files with 115 additions and 9 deletions
@@ -66,6 +66,20 @@ public sealed class CvImportRetentionHostedService(IServiceScopeFactory scopes,
removed++;
}
await db.SaveChangesAsync(token);
var registered = (await db.CvUploadArtifacts.IgnoreQueryFilters().AsNoTracking().Select(a => a.StoragePath).ToListAsync(token))
.Where(p => !string.IsNullOrWhiteSpace(p)).Select(Path.GetFullPath).ToHashSet(OperatingSystem.IsWindows() ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal);
foreach (var directory in Directory.EnumerateDirectories(paths.CvArtifactsRoot).Take(1000))
{
if ((File.GetAttributes(directory) & FileAttributes.ReparsePoint) != 0) continue;
foreach (var file in Directory.EnumerateFiles(directory).Take(1000))
{
if (registered.Contains(Path.GetFullPath(file)) || !System.Text.RegularExpressions.Regex.IsMatch(Path.GetFileName(file), @"^\d{14}-[a-f0-9]{32}\.(pdf|docx)$", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
|| File.GetLastWriteTimeUtc(file) > now.AddDays(-7).UtcDateTime || (File.GetAttributes(file) & FileAttributes.ReparsePoint) != 0) continue;
try { File.Delete(file); removed++; }
catch (IOException) { }
catch (UnauthorizedAccessException) { }
}
}
return removed;
}
}