Skip to content
This repository was archived by the owner on Jul 12, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions pkg/controller/cleanup/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,23 @@ func New(ctx context.Context, config *config.CleanupConfig, db *database.Databas
}, nil
}

func (c *Controller) shouldCleanup(ctx context.Context) error {
func (c *Controller) shouldCleanup(ctx context.Context) (bool, error) {
cStat, err := c.db.CreateCleanup(database.CleanupName)
if err != nil {
return fmt.Errorf("failed to create cleanup: %w", err)
return false, fmt.Errorf("failed to create cleanup: %w", err)
}

if cStat.NotBefore.After(time.Now().UTC()) {
return fmt.Errorf("skipping cleanup, no cleanup before %v", cStat.NotBefore)
return false, nil
}

// Attempt to advance the generation.
if _, err = c.db.ClaimCleanup(cStat, c.config.CleanupPeriod); err != nil {
stats.RecordWithTags(ctx, []tag.Mutator{observability.ResultNotOK()}, mClaimRequests.M(1))
return fmt.Errorf("failed to claim cleanup: %w", err)
return false, fmt.Errorf("failed to claim cleanup: %w", err)
}
stats.RecordWithTags(ctx, []tag.Mutator{observability.ResultOK()}, mClaimRequests.M(1))
return nil
return true, nil
}

func (c *Controller) HandleCleanup() http.Handler {
Expand All @@ -79,14 +79,21 @@ func (c *Controller) HandleCleanup() http.Handler {

var result, item tag.Mutator

if err := c.shouldCleanup(ctx); err != nil {
ok, err := c.shouldCleanup(ctx)
if err != nil {
logger.Errorw("failed to run shouldCleanup", "error", err)
c.h.RenderJSON(w, http.StatusInternalServerError, &CleanupResult{
OK: false,
Errors: []error{err},
})
return
}
if !ok {
c.h.RenderJSON(w, http.StatusTooManyRequests, &CleanupResult{
OK: false,
Errors: []error{fmt.Errorf("too early")},
})
}

// Construct a multi-error. If one of the purges fails, we still want to
// attempt the other purges.
Expand Down