From 2654f8b429be023526429cffb09e1bda9128b76c Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Thu, 3 Oct 2024 22:06:26 -0700 Subject: [PATCH] store: purge old jobs and logs >30d old Signed-off-by: Stephen Gutekanst --- internal/wrench/store.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/internal/wrench/store.go b/internal/wrench/store.go index 2891257..079c472 100644 --- a/internal/wrench/store.go +++ b/internal/wrench/store.go @@ -405,7 +405,25 @@ type JobsFilter struct { Limit int } +var lastPurge time.Time + +func (s *Store) purgeOldData(ctx context.Context) error { + if !lastPurge.IsZero() && time.Since(lastPurge) < 24*time.Hour { + return nil + } + lastPurge = time.Now() + _, err := s.db.ExecContext(ctx, ` + DELETE FROM runner_jobs WHERE created_at < datetime('now', '-30 days'); + DELETE FROM logs WHERE timestamp < datetime('now', '-30 days'); + `) + return err +} + func (s *Store) Jobs(ctx context.Context, filters ...JobsFilter) ([]api.Job, error) { + if err := s.purgeOldData(ctx); err != nil { + return nil, errors.Wrap(err, "purgeOldData") + } + var conds []*sqlf.Query limit := sqlf.Sprintf("") for _, where := range filters {