Added package cleanup
This commit is contained in:
@@ -13,29 +13,48 @@ public class Worker(
|
||||
{
|
||||
private async Task DeletePackages(List<GiteaPackage> packages, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (configuration["DRY_RUN"]?.ToLower() == "true")
|
||||
var dryRun = configuration["DRY_RUN"]?.ToLower() == "true";
|
||||
if (dryRun)
|
||||
{
|
||||
logger.LogInformation("Dry run enabled, not deleting packages");
|
||||
logger.LogInformation("Dry run enabled, not deleting {Count} packages", packages.Count);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var giteaPackage in packages)
|
||||
{
|
||||
await giteaPackageService.DeletePackage(giteaPackage, cancellationToken);
|
||||
try
|
||||
{
|
||||
await giteaPackageService.DeletePackage(giteaPackage, cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to delete package {PackageName} version {Version}",
|
||||
giteaPackage.Name, giteaPackage.Version);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var packages = await packageService.GetFilteredPackages();
|
||||
Console.WriteLine($"Found {packages.Count()} packages");
|
||||
|
||||
var packagesToDelete = packageService.FilterPackagesToDelete(packages);
|
||||
Console.WriteLine($"Found {packagesToDelete.Count()} packages to delete");
|
||||
|
||||
await DeletePackages(packagesToDelete, cancellationToken);
|
||||
|
||||
logger.LogInformation("Cleanup finished, stopping application");
|
||||
appLifetime.StopApplication();
|
||||
try
|
||||
{
|
||||
var packages = await packageService.GetFilteredPackages();
|
||||
logger.LogInformation("Found {Count} packages", packages.Count);
|
||||
|
||||
var packagesToDelete = packageService.FilterPackagesToDelete(packages);
|
||||
logger.LogInformation("Found {Count} packages to delete", packagesToDelete.Count);
|
||||
|
||||
await DeletePackages(packagesToDelete, cancellationToken);
|
||||
|
||||
logger.LogInformation("Cleanup finished successfully");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Cleanup failed with an error");
|
||||
}
|
||||
finally
|
||||
{
|
||||
appLifetime.StopApplication();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user