Alerting: Delete expired images from the database (#53236)

This commit adds a DeleteExpiredService that deletes expired images from the database. It is run in the periodic collector service.
This commit is contained in:
George Robinson
2022-08-09 15:28:36 +01:00
committed by GitHub
parent adbb789877
commit 196b781c70
10 changed files with 354 additions and 218 deletions
+38 -24
View File
@@ -8,44 +8,46 @@ import (
"path"
"time"
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
"github.com/grafana/grafana/pkg/services/queryhistory"
"github.com/grafana/grafana/pkg/services/shorturls"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/serverlock"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/annotations"
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
"github.com/grafana/grafana/pkg/services/ngalert/image"
"github.com/grafana/grafana/pkg/services/queryhistory"
"github.com/grafana/grafana/pkg/services/shorturls"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/setting"
)
func ProvideService(cfg *setting.Cfg, serverLockService *serverlock.ServerLockService,
shortURLService shorturls.Service, store sqlstore.Store, queryHistoryService queryhistory.Service,
dashboardVersionService dashver.Service, dashSnapSvc dashboardsnapshots.Service) *CleanUpService {
shortURLService shorturls.Service, sqlstore *sqlstore.SQLStore, queryHistoryService queryhistory.Service,
dashboardVersionService dashver.Service, dashSnapSvc dashboardsnapshots.Service, deleteExpiredImageService *image.DeleteExpiredService) *CleanUpService {
s := &CleanUpService{
Cfg: cfg,
ServerLockService: serverLockService,
ShortURLService: shortURLService,
QueryHistoryService: queryHistoryService,
store: store,
log: log.New("cleanup"),
dashboardVersionService: dashboardVersionService,
dashboardSnapshotService: dashSnapSvc,
Cfg: cfg,
ServerLockService: serverLockService,
ShortURLService: shortURLService,
QueryHistoryService: queryHistoryService,
store: sqlstore,
log: log.New("cleanup"),
dashboardVersionService: dashboardVersionService,
dashboardSnapshotService: dashSnapSvc,
deleteExpiredImageService: deleteExpiredImageService,
}
return s
}
type CleanUpService struct {
log log.Logger
store sqlstore.Store
Cfg *setting.Cfg
ServerLockService *serverlock.ServerLockService
ShortURLService shorturls.Service
QueryHistoryService queryhistory.Service
dashboardVersionService dashver.Service
dashboardSnapshotService dashboardsnapshots.Service
log log.Logger
store sqlstore.Store
Cfg *setting.Cfg
ServerLockService *serverlock.ServerLockService
ShortURLService shorturls.Service
QueryHistoryService queryhistory.Service
dashboardVersionService dashver.Service
dashboardSnapshotService dashboardsnapshots.Service
deleteExpiredImageService *image.DeleteExpiredService
}
func (srv *CleanUpService) Run(ctx context.Context) error {
@@ -61,6 +63,7 @@ func (srv *CleanUpService) Run(ctx context.Context) error {
srv.cleanUpTmpFiles()
srv.deleteExpiredSnapshots(ctx)
srv.deleteExpiredDashboardVersions(ctx)
srv.deleteExpiredImages(ctx)
srv.cleanUpOldAnnotations(ctxWithTimeout)
srv.expireOldUserInvites(ctx)
srv.deleteStaleShortURLs(ctx)
@@ -156,6 +159,17 @@ func (srv *CleanUpService) deleteExpiredDashboardVersions(ctx context.Context) {
}
}
func (srv *CleanUpService) deleteExpiredImages(ctx context.Context) {
if !srv.Cfg.UnifiedAlerting.IsEnabled() {
return
}
if rowsAffected, err := srv.deleteExpiredImageService.DeleteExpired(ctx); err != nil {
srv.log.Error("Failed to delete expired images", "error", err.Error())
} else {
srv.log.Debug("Deleted expired images", "rows affected", rowsAffected)
}
}
func (srv *CleanUpService) deleteOldLoginAttempts(ctx context.Context) {
if srv.Cfg.DisableBruteForceLoginProtection {
return