Skip to content

Commit

Permalink
Introduce Database.PeriodicCleanup()
Browse files Browse the repository at this point in the history
  • Loading branch information
jrauh01 committed Jul 15, 2024
1 parent c469361 commit 1d51623
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions pkg/database/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-kubernetes/pkg/com"
"github.com/icinga/icinga-kubernetes/pkg/periodic"
"golang.org/x/sync/errgroup"
"time"
)

Expand Down Expand Up @@ -74,3 +76,31 @@ func (db *Database) CleanupOlderThan(
type cleanupWhere struct {
Time types.UnixMilli
}

func (db *Database) PeriodicCleanup(ctx context.Context, stmt CleanupStmt) error {
g, ctxCleanup := errgroup.WithContext(ctx)

errs := make(chan error, 1)
defer close(errs)

periodic.Start(ctx, time.Hour, func(tick periodic.Tick) {
olderThan := tick.Time.AddDate(0, -1, 0)

_, err := db.CleanupOlderThan(
ctx, stmt, 5000, olderThan,
)

if err != nil {
select {
case errs <- err:
case <-ctx.Done():
}

return
}
}, periodic.Immediate()).Stop()

com.ErrgroupReceive(ctxCleanup, g, errs)

return g.Wait()
}

0 comments on commit 1d51623

Please sign in to comment.