grafana/pkg/services/cleanup/cleanup.go
Carl Bergquist 28f7b6dad1 Enable Grafana extensions at build time. (#11752)
* extensions: import and build

* bus: use predefined error

* enterprise: build script for enterprise packages

* poc: auto registering services and dependency injection

(cherry picked from commit b5b1ef875f905473af41e49f8071cb9028edc845)

* poc: backend services registry progress

(cherry picked from commit 97be69725881241bfbf1e7adf0e66801d6b0af3d)

* poc: minor update

(cherry picked from commit 03d7a6888b81403f458b94305792e075568f0794)

* ioc: introduce manuel ioc

* enterprise: adds setting for enterprise

* build: test and build specific ee commit

* cleanup: test testing code

* removes example hello service
2018-04-27 13:41:58 +02:00

108 lines
2.7 KiB
Go

package cleanup
import (
"context"
"io/ioutil"
"os"
"path"
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/setting"
)
type CleanUpService struct {
log log.Logger
}
func init() {
registry.RegisterService(&CleanUpService{})
}
func (service *CleanUpService) Init() error {
service.log = log.New("cleanup")
return nil
}
func (service *CleanUpService) Run(ctx context.Context) error {
service.cleanUpTmpFiles()
ticker := time.NewTicker(time.Minute * 10)
for {
select {
case <-ticker.C:
service.cleanUpTmpFiles()
service.deleteExpiredSnapshots()
service.deleteExpiredDashboardVersions()
service.deleteOldLoginAttempts()
case <-ctx.Done():
return ctx.Err()
}
}
}
func (service *CleanUpService) cleanUpTmpFiles() {
if _, err := os.Stat(setting.ImagesDir); os.IsNotExist(err) {
return
}
files, err := ioutil.ReadDir(setting.ImagesDir)
if err != nil {
service.log.Error("Problem reading image dir", "error", err)
return
}
var toDelete []os.FileInfo
for _, file := range files {
if file.ModTime().AddDate(0, 0, 1).Before(time.Now()) {
toDelete = append(toDelete, file)
}
}
for _, file := range toDelete {
fullPath := path.Join(setting.ImagesDir, file.Name())
err := os.Remove(fullPath)
if err != nil {
service.log.Error("Failed to delete temp file", "file", file.Name(), "error", err)
}
}
service.log.Debug("Found old rendered image to delete", "deleted", len(toDelete), "keept", len(files))
}
func (service *CleanUpService) deleteExpiredSnapshots() {
cmd := m.DeleteExpiredSnapshotsCommand{}
if err := bus.Dispatch(&cmd); err != nil {
service.log.Error("Failed to delete expired snapshots", "error", err.Error())
} else {
service.log.Debug("Deleted expired snapshots", "rows affected", cmd.DeletedRows)
}
}
func (service *CleanUpService) deleteExpiredDashboardVersions() {
cmd := m.DeleteExpiredVersionsCommand{}
if err := bus.Dispatch(&cmd); err != nil {
service.log.Error("Failed to delete expired dashboard versions", "error", err.Error())
} else {
service.log.Debug("Deleted old/expired dashboard versions", "rows affected", cmd.DeletedRows)
}
}
func (service *CleanUpService) deleteOldLoginAttempts() {
if setting.DisableBruteForceLoginProtection {
return
}
cmd := m.DeleteOldLoginAttemptsCommand{
OlderThan: time.Now().Add(time.Minute * -10),
}
if err := bus.Dispatch(&cmd); err != nil {
service.log.Error("Problem deleting expired login attempts", "error", err.Error())
} else {
service.log.Debug("Deleted expired login attempts", "rows affected", cmd.DeletedRows)
}
}