-
Notifications
You must be signed in to change notification settings - Fork 20
/
lurker.go
129 lines (119 loc) · 3.24 KB
/
lurker.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
//"net/http"
//"os"
//"path"
//"strings"
//"encoding/json"
"github.com/espebra/filebin2/dbl"
"github.com/espebra/filebin2/s3"
"time"
)
type Lurker struct {
dao *dbl.DAO
s3 *s3.S3AO
interval time.Duration
retention uint64
}
func (l *Lurker) Init(interval int, retention uint64) (err error) {
l.interval = time.Second * time.Duration(interval)
l.retention = retention
return nil
}
func (l *Lurker) Run() {
fmt.Printf("Starting Lurker process (interval: %s)\n", l.interval.String())
ticker := time.NewTicker(l.interval)
done := make(chan bool)
go func() {
for {
select {
case <-done:
return
case _ = <-ticker.C:
t0 := time.Now()
l.DeletePendingFiles()
l.DeletePendingBins()
l.CleanTransactions()
l.CleanClients()
fmt.Printf("Lurker completed run in %.3fs\n", time.Since(t0).Seconds())
}
}
}()
}
func (l *Lurker) DeletePendingFiles() {
files, err := l.dao.File().GetPendingDelete()
if err != nil {
fmt.Printf("Unable to GetPendingDelete(): %s\n", err.Error())
return
}
if len(files) > 0 {
fmt.Printf("Found %d files pending removal.\n", len(files))
for _, file := range files {
//fmt.Printf(" > Bin %s, filename %s\n", file.Bin, file.Filename)
if err := l.s3.RemoveObject(file.Bin, file.Filename); err != nil {
fmt.Printf("Unable to delete file %s from bin %s from S3.\n", file.Filename, file.Bin)
return
}
file.InStorage = false
if err := l.dao.File().Update(&file); err != nil {
fmt.Printf("Unable to update filename %s (id %d) in bin %s: %s\n", file.Filename, file.Id, file.Bin, err.Error())
return
}
}
}
}
func (l *Lurker) DeletePendingBins() {
bins, err := l.dao.Bin().GetPendingDelete()
if err != nil {
fmt.Printf("Unable to GetPendingDelete(): %s\n", err.Error())
return
}
if len(bins) > 0 {
fmt.Printf("Found %d bins pending removal.\n", len(bins))
for _, bin := range bins {
//fmt.Printf(" > Bin %s\n", bin.Id)
files, err := l.dao.File().GetByBin(bin.Id, true)
if err != nil {
fmt.Printf("Unable to GetByBin: %s\n", err.Error())
return
}
for _, file := range files {
if err := l.s3.RemoveObject(file.Bin, file.Filename); err != nil {
fmt.Printf("Unable to delete file %s from bin %s from S3.\n", file.Filename, file.Bin)
return
}
fmt.Printf("Removing file %s from bin %s\n", file.Filename, bin.Id)
file.InStorage = false
if err := l.dao.File().Update(&file); err != nil {
fmt.Printf("Unable to update filename %s (id %d) in bin %s: %s\n", file.Filename, file.Id, file.Bin, err.Error())
return
}
}
if err := l.dao.Bin().Update(&bin); err != nil {
fmt.Printf("Unable to update bin %s: %s\n", bin.Id, err.Error())
return
}
}
}
}
func (l *Lurker) CleanTransactions() {
count, err := l.dao.Transaction().Cleanup(l.retention)
if err != nil {
fmt.Printf("Unable to Transactions().Cleanup(): %s\n", err.Error())
return
}
if count > 0 {
fmt.Printf("Removed %d log transactions.\n", count)
}
}
func (l *Lurker) CleanClients() {
count, err := l.dao.Client().Cleanup(l.retention)
if err != nil {
fmt.Printf("Unable to Client().Cleanup(): %s\n", err.Error())
return
}
if count > 0 {
fmt.Printf("Removed %d client entries.\n", count)
}
}