-
Notifications
You must be signed in to change notification settings - Fork 0
/
prune.go
70 lines (62 loc) · 1.48 KB
/
prune.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
"github.com/go-co-op/gocron"
)
func findOldFiles(dir string) (files []os.FileInfo, err error) {
tmpfiles, err := ioutil.ReadDir(dir)
if err != nil {
return
}
for _, file := range tmpfiles {
if file.Mode().IsRegular() {
if time.Since(file.ModTime()) > time.Duration(args.maxCacheAge)*time.Hour {
if args.debug {
log.Printf("Found old file %s with mod time of %d", file.Name(), file.ModTime().Unix())
}
files = append(files, file)
}
}
}
return
}
func deleteFiles(path string, files []os.FileInfo) {
log.Printf("Deleting %d old files\n", len(files))
for _, file := range files {
fullPath := filepath.Join(path, file.Name())
if args.debug {
log.Printf("Deleting %s", fullPath)
}
err := os.Remove(fullPath)
if err != nil {
log.Printf("Error deleting %s: %s\n", fullPath, err)
}
tentaSize.Sub(float64(file.Size()))
tentaFiles.Dec()
}
}
func pruneFiles() {
log.Printf("Pruning old files in %s\n", args.dataDir)
files, err := findOldFiles(args.dataDir)
if err != nil {
return
}
deleteFiles(args.dataDir, files)
}
func StartCron() {
if args.maxCacheAge > 0 {
log.Println("Starting CRON pruner")
cron := gocron.NewScheduler(time.UTC)
_, err := cron.Cron(args.cronSchedule).Do(pruneFiles)
if err != nil {
log.Fatalf("Error creating prune cronjob: %s", err.Error())
}
cron.StartAsync()
} else {
log.Println("Cache age set to 0. Skipping CRON pruner")
}
}