66 "slices"
77 "strings"
88 "sync"
9+ "sync/atomic"
910 "time"
1011
1112 "github.com/content-services/content-sources-backend/pkg/clients/pulp_client"
@@ -116,7 +117,8 @@ func enqueueSnapshotsCleanup(ctx context.Context, olderThanDays int, batchSize i
116117 return fmt .Errorf ("error getting repository configurations: %v" , err )
117118 }
118119 if batchSize > 0 {
119- repoConfigs = repoConfigs [:batchSize ] // Limit to batch size
120+ limit := min (batchSize , len (repoConfigs ))
121+ repoConfigs = repoConfigs [:limit ]
120122 }
121123 log .Info ().Msgf ("Snapshot cleanup: processing %d repositories with outdated snapshots" , len (repoConfigs ))
122124
@@ -143,12 +145,15 @@ func enqueueSnapshotCleanupForRepoConfig(ctx context.Context, taskClient client.
143145 slices .SortFunc (snaps , func (s1 , s2 models.Snapshot ) int {
144146 return s1 .CreatedAt .Compare (s2 .CreatedAt )
145147 })
148+
149+ keepIndex := len (snaps ) - 1
150+ cutoffTime := time .Now ().Add (- time .Duration (olderThanDays ) * 24 * time .Hour )
146151 toBeDeletedSnapUUIDs := make ([]string , 0 , len (snaps ))
147152 for i , snap := range snaps {
148- if i == len ( snaps ) - 1 && len (toBeDeletedSnapUUIDs ) == len (snaps )- 1 {
153+ if i == keepIndex && len (toBeDeletedSnapUUIDs ) == len (snaps )- 1 {
149154 break
150155 }
151- if snap .CreatedAt .Before (time . Now (). Add ( - time . Duration ( olderThanDays ) * 24 * time . Hour ) ) {
156+ if snap .CreatedAt .Before (cutoffTime ) {
152157 toBeDeletedSnapUUIDs = append (toBeDeletedSnapUUIDs , snap .UUID )
153158 }
154159 }
@@ -194,15 +199,23 @@ func enqueueSnapshotCleanupForRepoConfig(ctx context.Context, taskClient client.
194199}
195200
196201func pulpOrphanCleanup (ctx context.Context , db * gorm.DB , batchSize int ) error {
197- var err error
198202 daoReg := dao .GetDaoRegistry (db )
199203
200204 domains , err := daoReg .Domain .List (ctx )
201205 if err != nil {
202206 log .Panic ().Err (err ).Msg ("orphan cleanup error: error listing orgs" )
203207 }
204208
205- for i := 0 ; i < len (domains ); i += batchSize {
209+ return runPulpOrphanCleanupForDomains (ctx , domains , batchSize , pulp_client .GetPulpClientWithDomain )
210+ }
211+
212+ // runPulpOrphanCleanupForDomains runs orphan cleanup in batches and stops if any PollTask fails
213+ // (PollTask already retries transient GetTask errors).
214+ func runPulpOrphanCleanupForDomains (ctx context.Context , domains []models.Domain , batchSize int , getPulpClient func (domainName string ) pulp_client.PulpClient ) error {
215+ var abort atomic.Bool
216+ var firstErr error
217+
218+ for i := 0 ; i < len (domains ) && ! abort .Load (); i += batchSize {
206219 end := i + batchSize
207220 if end > len (domains ) {
208221 end = len (domains )
@@ -218,24 +231,33 @@ func pulpOrphanCleanup(ctx context.Context, db *gorm.DB, batchSize int) error {
218231 wg .Add (1 )
219232 go func () {
220233 defer wg .Done ()
221-
222- pulpClient := pulp_client .GetPulpClientWithDomain (domainName )
234+ if abort .Load () {
235+ return
236+ }
237+ pulpClient := getPulpClient (domainName )
223238 cleanupTask , err := pulpClient .OrphanCleanup (ctx )
224239 if err != nil {
225240 logger .Error ().Err (err ).Msgf ("error starting orphan cleanup" )
226241 return
227242 }
228243 logger .Info ().Str ("task_href" , cleanupTask ).Msgf ("running orphan cleanup for org: %v" , org )
229244
230- _ , err = pulp_client . GetGlobalPulpClient () .PollTask (ctx , cleanupTask )
245+ _ , err = pulpClient .PollTask (ctx , cleanupTask )
231246 if err != nil {
232- logger .Error ().Err (err ).Msgf ("error polling pulp task for orphan cleanup" )
247+ logger .Error ().Err (err ).
248+ Msg ("error polling pulp task for orphan cleanup; remaining domains will be skipped" )
249+ if abort .CompareAndSwap (false , true ) {
250+ firstErr = err
251+ }
233252 return
234253 }
235254 }()
236255 }
237256 wg .Wait ()
238257 }
258+ if abort .Load () {
259+ return fmt .Errorf ("pulp orphan cleanup aborted after failed task poll: %w" , firstErr )
260+ }
239261 return nil
240262}
241263
0 commit comments