Short URL: Cleanup unvisited/stale short URLs (#28867)

* cleanup stale short urls

* refactor test case names

* service injection

* fix query

* add docs

* remove comma
This commit is contained in:
Will Browne
2020-11-09 18:08:16 +01:00
committed by GitHub
parent 71fffcb17c
commit a7ea8de47e
6 changed files with 68 additions and 5 deletions
+15
View File
@@ -7,6 +7,8 @@ import (
"path"
"time"
"github.com/grafana/grafana/pkg/services/shorturls"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/serverlock"
@@ -20,6 +22,7 @@ type CleanUpService struct {
log log.Logger
Cfg *setting.Cfg `inject:""`
ServerLockService *serverlock.ServerLockService `inject:""`
ShortURLService *shorturls.ShortURLService `inject:""`
}
func init() {
@@ -46,6 +49,7 @@ func (srv *CleanUpService) Run(ctx context.Context) error {
srv.deleteExpiredDashboardVersions()
srv.cleanUpOldAnnotations(ctxWithTimeout)
srv.expireOldUserInvites()
srv.deleteStaleShortURLs()
err := srv.ServerLockService.LockAndExecute(ctx, "delete old login attempts",
time.Minute*10, func() {
srv.deleteOldLoginAttempts()
@@ -151,3 +155,14 @@ func (srv *CleanUpService) expireOldUserInvites() {
srv.log.Debug("Expired user invites", "rows affected", cmd.NumExpired)
}
}
func (srv *CleanUpService) deleteStaleShortURLs() {
cmd := models.DeleteShortUrlCommand{
OlderThan: time.Now().Add(-time.Hour * 24 * 7),
}
if err := srv.ShortURLService.DeleteStaleShortURLs(context.Background(), &cmd); err != nil {
srv.log.Error("Problem deleting stale short urls", "error", err.Error())
} else {
srv.log.Debug("Deleted short urls", "rows affected", cmd.NumDeleted)
}
}