Skip to content

Commit 32fddbd

Browse files
committed
Added debug mode which will list files for deletion in logs
1 parent cad5f14 commit 32fddbd

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,6 +2659,8 @@ LEVEL = Info
26592659
;; Cleanup expired packages/data then targets the files within all maven snapshots versions
26602660
;RETAIN_MAVEN_SNAPSHOT_BUILDS = -1
26612661
;; Maximum size of a npm upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
2662+
; Enable debug logging for Maven cleanup. Enabling debug will stop snapshot version artifacts from being deleted but will log the files which were meant for deletion.
2663+
; DEBUG_MAVEN_CLEANUP = true
26622664
;LIMIT_SIZE_NPM = -1
26632665
;; Maximum size of a NuGet upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
26642666
;LIMIT_SIZE_NUGET = -1

modules/setting/packages.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var (
4343

4444
DefaultRPMSignEnabled bool
4545
RetainMavenSnapshotBuilds int
46+
DebugMavenCleanup bool
4647
}{
4748
Enabled: true,
4849
LimitTotalOwnerCount: -1,
@@ -91,6 +92,7 @@ func loadPackagesFrom(rootCfg ConfigProvider) (err error) {
9192
Packages.LimitSizeVagrant = mustBytes(sec, "LIMIT_SIZE_VAGRANT")
9293
Packages.DefaultRPMSignEnabled = sec.Key("DEFAULT_RPM_SIGN_ENABLED").MustBool(false)
9394
Packages.RetainMavenSnapshotBuilds = sec.Key("RETAIN_MAVEN_SNAPSHOT_BUILDS").MustInt(Packages.RetainMavenSnapshotBuilds)
95+
Packages.DebugMavenCleanup = sec.Key("DEBUG_MAVEN_CLEANUP").MustBool(true)
9496
return nil
9597
}
9698

services/packages/maven/cleanup.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import (
1717
// CleanupSnapshotVersions removes outdated files for SNAPHOT versions for all Maven packages.
1818
func CleanupSnapshotVersions(ctx context.Context) error {
1919
retainBuilds := setting.Packages.RetainMavenSnapshotBuilds
20-
log.Debug("Starting CleanupSnapshotVersion with retainBuilds: %d", retainBuilds)
20+
debugSession := setting.Packages.DebugMavenCleanup
21+
log.Debug("Maven Cleanup: starting with retainBuilds: %d, debugSession: %t", retainBuilds, debugSession)
2122

2223
if retainBuilds < 1 {
2324
log.Warn("Maven Cleanup: skipped as value for retainBuilds less than 1: %d. Minimum 1 build should be retained", retainBuilds)
@@ -49,7 +50,7 @@ func CleanupSnapshotVersions(ctx context.Context) error {
4950
}
5051
}
5152

52-
if err := cleanSnapshotFiles(ctx, version.ID, retainBuilds); err != nil {
53+
if err := cleanSnapshotFiles(ctx, version.ID, retainBuilds, debugSession); err != nil {
5354
formattedErr := fmt.Errorf("version '%s' (ID: %d, Group ID: %s, Artifact ID: %s): %w",
5455
version.Version, version.ID, groupId, artifactId, err)
5556

@@ -80,8 +81,8 @@ func isSnapshotVersion(version string) bool {
8081
return strings.HasSuffix(version, "-SNAPSHOT")
8182
}
8283

83-
func cleanSnapshotFiles(ctx context.Context, versionID int64, retainBuilds int) error {
84-
log.Debug("Starting cleanSnapshotFiles for versionID: %d with retainBuilds: %d", versionID, retainBuilds)
84+
func cleanSnapshotFiles(ctx context.Context, versionID int64, retainBuilds int, debugSession bool) error {
85+
log.Debug("Maven Cleanup: starting cleanSnapshotFiles for versionID: %d with retainBuilds: %d, debugSession: %t", versionID, retainBuilds, debugSession)
8586

8687
metadataFile, err := packages.GetFileForVersionByName(ctx, versionID, "maven-metadata.xml", packages.EmptyFileKey)
8788
if err != nil {
@@ -99,9 +100,24 @@ func cleanSnapshotFiles(ctx context.Context, versionID int64, retainBuilds int)
99100
return nil
100101
}
101102

102-
filesToRemove, _, err := packages.GetFilesBelowBuildNumber(ctx, versionID, thresholdBuildNumber, classifiers...)
103+
filesToRemove, skippedFiles, err := packages.GetFilesBelowBuildNumber(ctx, versionID, thresholdBuildNumber, classifiers...)
103104
if err != nil {
104-
return fmt.Errorf("failed to retrieve files for version ID %d: %w", versionID, err)
105+
return fmt.Errorf("cleanSnapshotFiles: failed to retrieve files for version: %w", err)
106+
}
107+
108+
if debugSession {
109+
var fileNamesToRemove, skippedFileNames []string
110+
111+
for _, file := range filesToRemove {
112+
fileNamesToRemove = append(fileNamesToRemove, file.Name)
113+
}
114+
115+
for _, file := range skippedFiles {
116+
skippedFileNames = append(skippedFileNames, file.Name)
117+
}
118+
119+
log.Debug("Maven Cleanup: debug session active. Files to remove: %v, Skipped files: %v", fileNamesToRemove, skippedFileNames)
120+
return nil
105121
}
106122

107123
for _, file := range filesToRemove {

0 commit comments

Comments
 (0)