-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurgedata.go
49 lines (41 loc) · 990 Bytes
/
purgedata.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
package main
import (
"io/ioutil"
"log"
"os"
"path"
"time"
)
const purgeInterval = (15 * time.Minute)
func remove(path string) {
err := os.Remove(path)
if err != nil {
log.Printf("Error when removing %s: %v\n", path, err)
}
}
func isHiddenFile(name string) bool {
return len(name) > 0 && name[0] == '.'
}
func purgeOldEntries(directory string, limit time.Time) {
infos, err := ioutil.ReadDir(directory)
if err != nil {
log.Printf("Error when purging directory %s: %v\n", directory, err)
return
}
for _, entry := range infos {
if !entry.IsDir() && !isHiddenFile(entry.Name()) && entry.ModTime().Before(limit) {
path := path.Join(directory, entry.Name())
remove(path)
}
}
}
func purgeWorker(directory string, purgeOlder time.Duration) {
for {
limit := time.Now().Add(-purgeOlder)
purgeOldEntries(directory, limit)
time.Sleep(purgeInterval)
}
}
func initPurge(directory string, purgeOlder time.Duration) {
go purgeWorker(directory, purgeOlder)
}