Skip to content

Commit

Permalink
fix: perform config item cleanup in batch
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed May 30, 2024
1 parent 4d66aff commit 4fe885b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions jobs/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ var CleanupConfigItems = &job.Job{
ctx.Tracef("removed %d config parent relationships", tx.RowsAffected)
}

var iter int
deleteBatchSize := ctx.Properties().Int("config.retention.delete_batch_size", 500)
configDeleteQuery := fmt.Sprintf(`
WITH ordered_rows AS (
SELECT id
Expand All @@ -126,11 +128,13 @@ var CleanupConfigItems = &job.Job{
deleted_at < NOW() - interval '1 SECONDS' * ? AND
id NOT IN (%s)
ORDER BY length(path) DESC
LIMIT ?
)
DELETE FROM config_items
WHERE id IN (SELECT id FROM ordered_rows)`, linkedConfigsQuery)
for {
tx := ctx.Context.DB().Exec(configDeleteQuery, seconds)
iter++
tx := ctx.Context.DB().Exec(configDeleteQuery, seconds, deleteBatchSize)
if tx.Error != nil {
return fmt.Errorf("failed to delete config items: %w", tx.Error)
}
Expand All @@ -139,8 +143,8 @@ var CleanupConfigItems = &job.Job{
break
}

ctx.Tracef("removed %d config items", tx.RowsAffected)
ctx.History.SuccessCount = int(tx.RowsAffected)
ctx.Logger.V(2).Infof("hard deleted %d config items [iter=%d, batchsize=%d]", iter, deleteBatchSize, tx.RowsAffected)
ctx.History.SuccessCount += int(tx.RowsAffected)
}

return nil
Expand Down

0 comments on commit 4fe885b

Please sign in to comment.