@@ -58,65 +58,79 @@ func ExecuteCleanupRules(outerCtx context.Context) error {
5858 }
5959
6060 anyVersionDeleted := false
61+ limit := 200
6162 for _ , p := range packages {
62- pvs , _ , err := packages_model .SearchVersions (ctx , & packages_model.PackageSearchOptions {
63- PackageID : p .ID ,
64- IsInternal : optional .Some (false ),
65- Sort : packages_model .SortCreatedDesc ,
66- Paginator : db .NewAbsoluteListOptions (pcr .KeepCount , 200 ),
67- })
68- if err != nil {
69- return fmt .Errorf ("CleanupRule [%d]: SearchVersions failed: %w" , pcr .ID , err )
70- }
71- versionDeleted := false
72- for _ , pv := range pvs {
73- if pcr .Type == packages_model .TypeContainer {
74- if skip , err := container_service .ShouldBeSkipped (ctx , pcr , p , pv ); err != nil {
75- return fmt .Errorf ("CleanupRule [%d]: container.ShouldBeSkipped failed: %w" , pcr .ID , err )
76- } else if skip {
77- log .Debug ("Rule[%d]: keep '%s/%s' (container)" , pcr .ID , p .Name , pv .Version )
78- continue
79- }
63+ lastVersionID := int64 (0 )
64+ for {
65+ pvs , _ , err := packages_model .SearchVersions (ctx , & packages_model.PackageSearchOptions {
66+ PackageID : p .ID ,
67+ IsInternal : optional .Some (false ),
68+ Sort : packages_model .SortCreatedDesc ,
69+ Paginator : db .NewAbsoluteListOptions (func () int {
70+ if lastVersionID > 0 {
71+ return 0
72+ }
73+ return pcr .KeepCount
74+ }(), limit ),
75+ LtVersionID : lastVersionID ,
76+ })
77+ if err != nil {
78+ return fmt .Errorf ("CleanupRule [%d]: SearchVersions failed: %w" , pcr .ID , err )
8079 }
80+ versionDeleted := false
81+ for _ , pv := range pvs {
82+ lastVersionID = pv .ID
83+ if pcr .Type == packages_model .TypeContainer {
84+ if skip , err := container_service .ShouldBeSkipped (ctx , pcr , p , pv ); err != nil {
85+ return fmt .Errorf ("CleanupRule [%d]: container.ShouldBeSkipped failed: %w" , pcr .ID , err )
86+ } else if skip {
87+ log .Debug ("Rule[%d]: keep '%s/%s' (container)" , pcr .ID , p .Name , pv .Version )
88+ continue
89+ }
90+ }
8191
82- toMatch := pv .LowerVersion
83- if pcr .MatchFullName {
84- toMatch = p .LowerName + "/" + pv .LowerVersion
85- }
92+ toMatch := pv .LowerVersion
93+ if pcr .MatchFullName {
94+ toMatch = p .LowerName + "/" + pv .LowerVersion
95+ }
8696
87- if pcr .KeepPatternMatcher != nil && pcr .KeepPatternMatcher .MatchString (toMatch ) {
88- log .Debug ("Rule[%d]: keep '%s/%s' (keep pattern)" , pcr .ID , p .Name , pv .Version )
89- continue
90- }
91- if pv .CreatedUnix .AsLocalTime ().After (olderThan ) {
92- log .Debug ("Rule[%d]: keep '%s/%s' (remove days)" , pcr .ID , p .Name , pv .Version )
93- continue
94- }
95- if pcr .RemovePatternMatcher != nil && ! pcr .RemovePatternMatcher .MatchString (toMatch ) {
96- log .Debug ("Rule[%d]: keep '%s/%s' (remove pattern)" , pcr .ID , p .Name , pv .Version )
97- continue
98- }
97+ if pcr .KeepPatternMatcher != nil && pcr .KeepPatternMatcher .MatchString (toMatch ) {
98+ log .Debug ("Rule[%d]: keep '%s/%s' (keep pattern)" , pcr .ID , p .Name , pv .Version )
99+ continue
100+ }
101+ if pv .CreatedUnix .AsLocalTime ().After (olderThan ) {
102+ log .Debug ("Rule[%d]: keep '%s/%s' (remove days) %v " , pcr .ID , p .Name , pv .Version , pv . CreatedUnix . FormatDate () )
103+ continue
104+ }
105+ if pcr .RemovePatternMatcher != nil && ! pcr .RemovePatternMatcher .MatchString (toMatch ) {
106+ log .Debug ("Rule[%d]: keep '%s/%s' (remove pattern)" , pcr .ID , p .Name , pv .Version )
107+ continue
108+ }
99109
100- log .Debug ("Rule[%d]: remove '%s/%s'" , pcr .ID , p .Name , pv .Version )
110+ log .Debug ("Rule[%d]: remove '%s/%s'" , pcr .ID , p .Name , pv .Version )
101111
102- if err := packages_service .DeletePackageVersionAndReferences (ctx , pv ); err != nil {
103- return fmt .Errorf ("CleanupRule [%d]: DeletePackageVersionAndReferences failed: %w" , pcr .ID , err )
104- }
112+ if err := packages_service .DeletePackageVersionAndReferences (ctx , pv ); err != nil {
113+ return fmt .Errorf ("CleanupRule [%d]: DeletePackageVersionAndReferences failed: %w" , pcr .ID , err )
114+ }
105115
106- versionDeleted = true
107- anyVersionDeleted = true
108- }
116+ versionDeleted = true
117+ anyVersionDeleted = true
118+ }
109119
110- if versionDeleted {
111- if pcr .Type == packages_model .TypeCargo {
112- owner , err := user_model .GetUserByID (ctx , pcr .OwnerID )
113- if err != nil {
114- return fmt .Errorf ("GetUserByID failed: %w" , err )
115- }
116- if err := cargo_service .UpdatePackageIndexIfExists (ctx , owner , owner , p .ID ); err != nil {
117- return fmt .Errorf ("CleanupRule [%d]: cargo.UpdatePackageIndexIfExists failed: %w" , pcr .ID , err )
120+ if versionDeleted {
121+ if pcr .Type == packages_model .TypeCargo {
122+ owner , err := user_model .GetUserByID (ctx , pcr .OwnerID )
123+ if err != nil {
124+ return fmt .Errorf ("GetUserByID failed: %w" , err )
125+ }
126+ if err := cargo_service .UpdatePackageIndexIfExists (ctx , owner , owner , p .ID ); err != nil {
127+ return fmt .Errorf ("CleanupRule [%d]: cargo.UpdatePackageIndexIfExists failed: %w" , pcr .ID , err )
128+ }
118129 }
119130 }
131+ if len (pvs ) < limit {
132+ break
133+ }
120134 }
121135 }
122136
0 commit comments