feat(service):lab4 sweep stale pending attachments
This commit is contained in:
@@ -70,6 +70,11 @@ func (r *Reconciler) tick(ctx context.Context) {
|
||||
return
|
||||
}
|
||||
defer release()
|
||||
|
||||
pendingDeleted := r.sweepPending(ctx)
|
||||
if pendingDeleted > 0 {
|
||||
log.Printf("reconciler: sweep done pending=%d", pendingDeleted)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reconciler) acquireLock(ctx context.Context) (bool, func(), error) {
|
||||
@@ -98,3 +103,26 @@ func (r *Reconciler) acquireLock(ctx context.Context) (bool, func(), error) {
|
||||
}
|
||||
return true, release, nil
|
||||
}
|
||||
|
||||
func (r *Reconciler) sweepPending(ctx context.Context) int {
|
||||
cutoff := time.Now().UTC().Add(-r.gracePeriod)
|
||||
stale, err := r.attachments.ListStalePending(ctx, cutoff, r.batchSize)
|
||||
if err != nil {
|
||||
log.Printf("reconciler: list stale pending: %v", err)
|
||||
return 0
|
||||
}
|
||||
|
||||
deleted := 0
|
||||
for _, a := range stale {
|
||||
if err := r.storage.Delete(ctx, a.ObjectKey); err != nil {
|
||||
log.Printf("reconciler: delete object %s: %v", a.ObjectKey, err)
|
||||
continue
|
||||
}
|
||||
if err := r.attachments.DeletePending(ctx, a.ID); err != nil {
|
||||
log.Printf("reconciler: delete pending row %s: %v", a.ID, err)
|
||||
continue
|
||||
}
|
||||
deleted++
|
||||
}
|
||||
return deleted
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user