linx-server/cleanup/cleanup.go

43 lines
838 B
Go
Raw Permalink Normal View History

package cleanup
import (
"log"
"time"
"github.com/andreimarcu/linx-server/backends/localfs"
"github.com/andreimarcu/linx-server/expiry"
)
func Cleanup(filesDir string, metaDir string, noLogs bool) {
2019-01-25 08:33:11 +01:00
fileBackend := localfs.NewLocalfsBackend(metaDir, filesDir)
2019-01-25 08:33:11 +01:00
files, err := fileBackend.List()
if err != nil {
panic(err)
}
for _, filename := range files {
2019-01-25 08:33:11 +01:00
metadata, err := fileBackend.Head(filename)
if err != nil {
if !noLogs {
log.Printf("Failed to find metadata for %s", filename)
}
}
if expiry.IsTsExpired(metadata.Expiry) {
if !noLogs {
log.Printf("Delete %s", filename)
}
fileBackend.Delete(filename)
}
}
}
func PeriodicCleanup(minutes time.Duration, filesDir string, metaDir string, noLogs bool) {
c := time.Tick(minutes)
for range c {
Cleanup(filesDir, metaDir, noLogs)
}
}