From 92ae2f7c83e8e6de25bbce6e03d4870b19993490 Mon Sep 17 00:00:00 2001 From: Sanne Raymaekers Date: Mon, 6 Jun 2022 14:17:46 +0200 Subject: [PATCH] osbuild-service-maintenance: Delete/update results in chunks The results of the manifest jobs can be very big, and operating on 30-40k rows at once can starve or crash a smaller rds instance. --- cmd/osbuild-service-maintenance/db.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/cmd/osbuild-service-maintenance/db.go b/cmd/osbuild-service-maintenance/db.go index f7867b95a..8943b7bb4 100644 --- a/cmd/osbuild-service-maintenance/db.go +++ b/cmd/osbuild-service-maintenance/db.go @@ -28,12 +28,21 @@ func DBCleanup(dbURL string, dryRun bool, cutoff time.Time) error { logrus.Info("Dry run, skipping deletion of jobs") continue } - rows, err := jobs.DeleteJobResult(v) - if err != nil { - logrus.Errorf("Error deleting results for jobs: %v, %d rows affected", rows, err) - continue + + // Delete results in chunks to avoid starving the rds instance + for i := 0; i < len(v); i += 100 { + max := i + 100 + if max > len(v) { + max = len(v) + } + + rows, err := jobs.DeleteJobResult(v[i:max]) + if err != nil { + logrus.Errorf("Error deleting results for jobs: %v, %d rows affected", rows, err) + continue + } + logrus.Infof("Deleted results from %d jobs out of %d job ids", rows, len(v)) } - logrus.Infof("Deleted results from %d jobs out of %d job ids", rows, len(v)) } return nil