osbuild-service-maintenance: Split out db cleanup

This commit is contained in:
Sanne Raymaekers 2022-04-12 18:05:47 +02:00 committed by Ondřej Budai
parent 9bff4a4f0f
commit eeb2238b12
2 changed files with 48 additions and 33 deletions

View file

@ -0,0 +1,46 @@
package main
import (
"fmt"
"time"
"github.com/sirupsen/logrus"
"github.com/osbuild/osbuild-composer/internal/jobqueue/dbjobqueue"
)
func DBCleanup(dbURL string, dryRun bool, cutoff time.Time) error {
archs := []string{"x86_64"}
jobType := "osbuild"
jobs, err := dbjobqueue.New(dbURL)
if err != nil {
return err
}
var jobTypes []string
for _, a := range archs {
jobTypes = append(jobTypes, fmt.Sprintf("%s:%s", jobType, a))
}
jobsByType, err := jobs.JobsUptoByType(jobTypes, cutoff)
if err != nil {
return fmt.Errorf("Error querying jobs: %v", err)
}
for k, v := range jobsByType {
logrus.Infof("Deleting jobs and their dependencies of type %v", k)
if dryRun {
logrus.Infof("Dry run, skipping deletion of jobs: %v", v)
continue
}
for _, jobId := range v {
err = jobs.DeleteJobIncludingDependencies(jobId)
if err != nil {
return fmt.Errorf("Error deleting job: %v", jobId)
}
}
}
return nil
}

View file

@ -7,15 +7,11 @@ import (
"time"
"github.com/sirupsen/logrus"
"github.com/osbuild/osbuild-composer/internal/jobqueue/dbjobqueue"
)
func main() {
logrus.SetReportCaller(true)
archs := []string{"x86_64"}
jobType := "osbuild"
// 14 days
cutoff := time.Now().Add(-(time.Hour * 24 * 14))
logrus.Infof("Cutoff date: %v", cutoff)
@ -90,7 +86,6 @@ func main() {
logrus.Info("🦀🦀🦀 DB maintenance not enabled, skipping 🦀🦀🦀")
return
}
dbURL := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s",
conf.PGUser,
conf.PGPassword,
@ -99,35 +94,9 @@ func main() {
conf.PGDatabase,
conf.PGSSLMode,
)
jobs, err := dbjobqueue.New(dbURL)
err = DBCleanup(dbURL, conf.DryRun, cutoff)
if err != nil {
panic(err)
}
var jobTypes []string
for _, a := range archs {
jobTypes = append(jobTypes, fmt.Sprintf("%s:%s", jobType, a))
}
jobsByType, err := jobs.JobsUptoByType(jobTypes, cutoff)
if err != nil {
logrus.Errorf("Error querying jobs: %v", err)
return
}
for k, v := range jobsByType {
logrus.Infof("Deleting jobs and their dependencies of type %v", k)
if conf.DryRun {
logrus.Infof("Dry run, skipping deletion of jobs: %v", v)
continue
}
for _, jobId := range v {
err = jobs.DeleteJobIncludingDependencies(jobId)
if err != nil {
logrus.Errorf("Error deleting job: %v", jobId)
}
}
logrus.Fatalf("Error during DBCleanup: %v", err)
}
logrus.Info("🦀🦀🦀 dbqueue cleanup done 🦀🦀🦀")
}