-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.go
executable file
·80 lines (65 loc) · 1.65 KB
/
daemon.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
package sqlitehench
import (
"io/fs"
"os"
"time"
)
// Shrinking of SQLite databases runs in the background.
// Everytime a db-file is acceesed for writing, it is added to
// a list rather than scanning a target folder for SQLite
// db files (adds to I/O operations). Databases in the list
// will be monitored for shrinking.
func (d *DBAccess) shrinkAllDB() {
for {
for i := 0; i < len(d.ShrinkWatchList); i++ {
if len(d.ShrinkWatchList) == 0 || i >= len(d.ShrinkWatchList) {
break
}
if !fileOrDirExists(d.ShrinkWatchList[i]) {
d.removeItemFromShrinkWatchList(i)
break
}
if len(d.ShrinkWatchList) == 0 || i >= len(d.ShrinkWatchList) {
break
}
// Shrink the database.
d.ShrinkDB(d.ShrinkWatchList[i])
if i%10 == 0 {
time.Sleep(time.Second)
}
}
time.Sleep(19 * time.Second)
}
}
// maintWatchList examins the file paths in the watch list.
// It removes the ones that have not been acccessed for sometime.
func (d *DBAccess) maintWatchList() {
var err error
var fi fs.FileInfo
for {
for i := 0; i < len(d.ShrinkWatchList); i++ {
if len(d.ShrinkWatchList) == 0 {
break
}
if !d.DatabaseExists(d.ShrinkWatchList[i]) {
d.removeItemFromShrinkWatchList(i)
break
}
if len(d.ShrinkWatchList) == 0 || i >= len(d.ShrinkWatchList) {
break
}
if fi, err = os.Stat(d.ShrinkWatchList[i]); err != nil {
// The db file could be corrupted or been removed.
d.removeItemFromShrinkWatchList(i)
break
}
t := time.Since(fi.ModTime())
if t.Hours() < 1.25 {
// remove from watchlist.
d.removeItemFromShrinkWatchList(i)
break
}
}
time.Sleep(37 * time.Second)
}
}