#11607 Cleanup time of temporary files is now configurable
This commit is contained in:
@@ -57,8 +57,10 @@ func (srv *CleanUpService) cleanUpTmpFiles() {
|
||||
}
|
||||
|
||||
var toDelete []os.FileInfo
|
||||
var now = time.Now()
|
||||
|
||||
for _, file := range files {
|
||||
if file.ModTime().AddDate(0, 0, 1).Before(time.Now()) {
|
||||
if srv.shouldCleanupTempFile(file.ModTime(), now) {
|
||||
toDelete = append(toDelete, file)
|
||||
}
|
||||
}
|
||||
@@ -74,6 +76,14 @@ func (srv *CleanUpService) cleanUpTmpFiles() {
|
||||
srv.log.Debug("Found old rendered image to delete", "deleted", len(toDelete), "keept", len(files))
|
||||
}
|
||||
|
||||
func (srv *CleanUpService) shouldCleanupTempFile(filemtime time.Time, now time.Time) bool {
|
||||
if srv.Cfg.TempDataLifetime == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return filemtime.Add(srv.Cfg.TempDataLifetime).Before(now)
|
||||
}
|
||||
|
||||
func (srv *CleanUpService) deleteExpiredSnapshots() {
|
||||
cmd := m.DeleteExpiredSnapshotsCommand{}
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package cleanup
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCleanUpTmpFiles(t *testing.T) {
|
||||
Convey("Cleanup service tests", t, func() {
|
||||
cfg := setting.Cfg{}
|
||||
cfg.TempDataLifetime, _ = time.ParseDuration("24h")
|
||||
service := CleanUpService{
|
||||
Cfg: &cfg,
|
||||
}
|
||||
now := time.Now()
|
||||
secondAgo := now.Add(-time.Second)
|
||||
dayAgo := now.Add(-time.Second * 3600 * 24 * 7)
|
||||
weekAgo := now.Add(-time.Second * 3600 * 24 * 7)
|
||||
|
||||
Convey("Should not cleanup recent files", func() {
|
||||
So(service.shouldCleanupTempFile(secondAgo, now), ShouldBeFalse);
|
||||
})
|
||||
|
||||
Convey("Should cleanup older files", func() {
|
||||
So(service.shouldCleanupTempFile(dayAgo, now), ShouldBeTrue);
|
||||
})
|
||||
|
||||
Convey("After increasing temporary files lifetime, older files should be kept", func() {
|
||||
cfg.TempDataLifetime, _ = time.ParseDuration("1000h")
|
||||
So(service.shouldCleanupTempFile(weekAgo, now), ShouldBeFalse);
|
||||
})
|
||||
|
||||
Convey("If lifetime is 0, files should never be cleaned up", func() {
|
||||
cfg.TempDataLifetime = 0
|
||||
So(service.shouldCleanupTempFile(weekAgo, now), ShouldBeFalse);
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user