Compare commits

..

2 Commits

Author SHA1 Message Date
c8cfa26760 Added output manually
All checks were successful
Build on push / prepare (push) Successful in 6s
Build on push / build (push) Successful in 15s
2026-02-15 10:36:03 +01:00
4ff5680310 Changed logging
All checks were successful
Build on push / prepare (push) Successful in 7s
Build on push / build (push) Successful in 15s
2026-02-15 10:29:52 +01:00
3 changed files with 17 additions and 46 deletions

View File

@@ -55,4 +55,5 @@ runs:
GITHUB_OUTPUT: ${{ env.GITHUB_OUTPUT }} GITHUB_OUTPUT: ${{ env.GITHUB_OUTPUT }}
run: | run: |
echo "Starting cleanup..." echo "Starting cleanup..."
./package-cleanup-linux-x64 ./package-cleanup-linux-x64 > output.txt
cat output.txt

View File

@@ -15,5 +15,9 @@ builder.Services
.AddHostedService<Worker>() .AddHostedService<Worker>()
.AddHttpClient<IGiteaPackageService, GiteaPackageService>(); .AddHttpClient<IGiteaPackageService, GiteaPackageService>();
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.SetMinimumLevel(LogLevel.Debug);
var host = builder.Build(); var host = builder.Build();
await host.RunAsync(); await host.RunAsync();

View File

@@ -11,50 +11,26 @@ public class Worker(
IGiteaPackageService giteaPackageService IGiteaPackageService giteaPackageService
) : BackgroundService ) : BackgroundService
{ {
private async Task<int> DeletePackages(List<GiteaPackage> packages, CancellationToken cancellationToken = default) private async Task DeletePackages(List<GiteaPackage> packages, CancellationToken cancellationToken = default)
{ {
var dryRun = configuration["DRY_RUN"]?.ToLower() == "true"; var dryRun = configuration["DRY_RUN"]?.ToLower() == "true";
if (dryRun) if (dryRun)
{ {
logger.LogInformation("Dry run enabled, not deleting {Count} packages", packages.Count); logger.LogInformation("Dry run enabled, not deleting {Count} packages", packages.Count);
return 0;
} }
var deletedCount = 0;
foreach (var giteaPackage in packages) foreach (var giteaPackage in packages)
{ {
try try
{ {
await giteaPackageService.DeletePackage(giteaPackage, cancellationToken); await giteaPackageService.DeletePackage(giteaPackage, cancellationToken);
deletedCount++;
} }
catch (Exception ex) catch (Exception ex)
{ {
logger.LogError(ex, "Failed to delete package {PackageName} version {Version}", logger.LogError(ex, "Failed to delete package {PackageName} version {Version}",
giteaPackage.Name, giteaPackage.Version); giteaPackage.Name, giteaPackage.Version);
} }
} }
return deletedCount;
}
private void WriteGitHubOutput(string name, string value)
{
var githubOutput = configuration["GITHUB_OUTPUT"];
if (string.IsNullOrEmpty(githubOutput))
{
logger.LogDebug("GITHUB_OUTPUT not set, skipping output: {Name}={Value}", name, value);
return;
}
try
{
File.AppendAllText(githubOutput, $"{name}={value}{Environment.NewLine}");
logger.LogInformation("Wrote to GITHUB_OUTPUT: {Name}={Value}", name, value);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to write to GITHUB_OUTPUT file");
}
} }
protected override async Task ExecuteAsync(CancellationToken cancellationToken) protected override async Task ExecuteAsync(CancellationToken cancellationToken)
@@ -68,43 +44,33 @@ public class Worker(
if (names.Length == 0) if (names.Length == 0)
{ {
logger.LogWarning("No package names configured"); logger.LogWarning("No package names configured");
WriteGitHubOutput("deleted_packages", "0");
WriteGitHubOutput("processed_names", "0");
appLifetime.StopApplication(); appLifetime.StopApplication();
return; return;
} }
var totalDeleted = 0;
// Process each name separately: collect -> filter -> delete // Process each name separately: collect -> filter -> delete
foreach (var name in names) foreach (var name in names)
{ {
logger.LogInformation("Processing packages for name '{Name}'", name); logger.LogInformation("Processing packages for name '{Name}'", name);
var packages = (await giteaPackageService.GetPackagesByNameAsync(name, cancellationToken)).ToList(); var packages = (await giteaPackageService.GetPackagesByNameAsync(name, cancellationToken)).ToList();
logger.LogInformation("Found {Count} packages for name '{Name}'", packages.Count, name); logger.LogInformation("Found {Count} packages for name '{Name}'", packages.Count, name);
var packagesToDelete = packageService.FilterPackagesToDelete(packages); var packagesToDelete = packageService.FilterPackagesToDelete(packages);
logger.LogInformation("Found {Count} packages to delete for name '{Name}'", packagesToDelete.Count, name); logger.LogInformation("Found {Count} packages to delete for name '{Name}'", packagesToDelete.Count,
name);
var deletedCount = await DeletePackages(packagesToDelete, cancellationToken);
totalDeleted += deletedCount; await DeletePackages(packagesToDelete, cancellationToken);
logger.LogInformation("Deleted {Count} packages for name '{Name}'", deletedCount, name); logger.LogInformation("Deleted {Count} packages for name '{Name}'", packagesToDelete.Count, name);
logger.LogInformation("Cleanup finished for name '{Name}'", name); logger.LogInformation("Cleanup finished for name '{Name}'", name);
} }
logger.LogInformation("All package names processed successfully"); logger.LogInformation("All package names processed successfully");
// Write outputs to GITHUB_OUTPUT
WriteGitHubOutput("deleted_packages", totalDeleted.ToString());
WriteGitHubOutput("processed_names", names.Length.ToString());
} }
catch (Exception ex) catch (Exception ex)
{ {
logger.LogError(ex, "Cleanup failed with an error"); logger.LogError(ex, "Cleanup failed with an error");
WriteGitHubOutput("deleted_packages", "0");
WriteGitHubOutput("processed_names", "0");
} }
finally finally
{ {