osbuild-service-maintenance: Honor dryrun in db cleanup

This commit is contained in:
Sanne Raymaekers 2022-07-30 13:22:12 +02:00
parent 2eaad3701d
commit 69d4429e8f
2 changed files with 24 additions and 0 deletions

View file

@ -18,6 +18,9 @@ const (
ORDER BY expires_at
LIMIT 1000
)`
sqlExpiredJobCount = `
SELECT COUNT(*) FROM jobs
WHERE expires_at < NOW()`
sqlVacuumAnalyze = `
VACUUM ANALYZE`
sqlVacuumStats = `
@ -55,6 +58,15 @@ func (d *db) DeleteJobs() (int64, error) {
return tag.RowsAffected(), nil
}
func (d *db) ExpiredJobCount() (int64, error) {
var count int64
err := d.Conn.QueryRow(context.Background(), sqlExpiredJobCount).Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func (d *db) VacuumAnalyze() error {
_, err := d.Conn.Exec(context.Background(), sqlVacuumAnalyze)
if err != nil {
@ -123,6 +135,15 @@ func DBCleanup(dbURL string, dryRun bool, cutoff time.Time) error {
var rows int64
for {
if dryRun {
rows, err = db.ExpiredJobCount()
if err != nil {
logrus.Warningf("Error querying expired jobs: %v", err)
}
logrus.Infof("Dryrun, expired job count: %d", rows)
break
}
rows, err = db.DeleteJobs()
if err != nil {
logrus.Errorf("Error deleting jobs: %v, %d rows affected", rows, err)

View file

@ -72,6 +72,9 @@ func testDeleteJob(t *testing.T, d db, q *dbjobqueue.DBJobQueue) {
require.Equal(t, int64(0), rows)
setExpired(t, d, id)
rows, err = d.ExpiredJobCount()
require.NoError(t, err)
require.Equal(t, int64(1), rows)
rows, err = d.DeleteJobs()
require.NoError(t, err)