-
Notifications
You must be signed in to change notification settings - Fork 20
/
rollup.go
163 lines (145 loc) · 3.79 KB
/
rollup.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package funnel
import (
"compress/gzip"
"io"
"io/ioutil"
"os"
"path"
"sort"
"strconv"
"strings"
"time"
"github.com/fvbommel/sortorder"
)
// Renames a file with the current timestamp
func renameFileTimestamp(cfg *Config) (string, error) {
newFileName := time.Now().UTC().Format("2006-01-02_15-04-05.00000") + ".log"
err := os.Rename(
path.Join(cfg.DirName, cfg.ActiveFileName),
path.Join(cfg.DirName, newFileName),
)
return newFileName, err
}
// Renames files serially by increasing suffix by 1
func renameFileSerial(cfg *Config) (string, error) {
// Read all the files from log dir
files, err := ioutil.ReadDir(cfg.DirName)
if err != nil {
return "", err
}
// Extracting the file names
var fileNames []string
for _, file := range files {
file.ModTime()
fileNames = append(fileNames, file.Name())
}
// Sorting the files in natural order
sort.Sort(sortorder.Natural(fileNames))
// Reverse traversing the slice
for i := len(fileNames) - 1; i >= 0; i-- {
fileName := fileNames[i]
// Continuing if its the current file
if fileName == cfg.ActiveFileName {
continue
}
// Check if the log file is prefixed with the correct active file name
if strings.HasPrefix(fileName, cfg.ActiveFileName) {
suffix := ".gz"
// Get the index from the file name
num := strings.TrimPrefix(fileName, cfg.ActiveFileName+".")
// Trim the suffix if ends with .gz
if strings.HasSuffix(fileName, suffix) {
num = strings.TrimSuffix(num, suffix)
}
intNum, err := strconv.Atoi(num)
if err != nil {
continue
}
// Now increase it by 1 and rename
intNum++
finalName := cfg.ActiveFileName + "." + strconv.Itoa(intNum)
// If ends with gz, add the gz suffix
if strings.HasSuffix(fileName, suffix) {
finalName += suffix
}
err = os.Rename(
path.Join(cfg.DirName, fileName),
path.Join(cfg.DirName, finalName),
)
if err != nil {
return "", err
}
}
}
// Rename active file to file.1
err = os.Rename(
path.Join(cfg.DirName, cfg.ActiveFileName),
path.Join(cfg.DirName, cfg.ActiveFileName+".1"),
)
if err != nil {
return "", err
}
return cfg.ActiveFileName + ".1", nil
}
func gzipFile(sourcePath string) error {
reader, err := os.Open(sourcePath)
if err != nil {
return err
}
// Remove the old file once done
defer os.Remove(sourcePath)
target := sourcePath + ".gz"
// Open new gzip stream
writer, err := os.Create(target)
if err != nil {
return err
}
defer writer.Close()
archiver := gzip.NewWriter(writer)
archiver.Name = path.Base(sourcePath)
defer archiver.Close()
// Write to the gzip stream
_, err = io.Copy(archiver, reader)
return err
}
// ByModTime implements sorting for files by mod time from recent to old
type ByModTime []os.FileInfo
func (a ByModTime) Len() int { return len(a) }
func (a ByModTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByModTime) Less(i, j int) bool { return a[i].ModTime().Unix() > a[j].ModTime().Unix() }
func deleteOldFiles(cfg *Config) error {
// Read all the files from log dir
files, err := ioutil.ReadDir(cfg.DirName)
if err != nil {
return err
}
// sort files by mod time
sort.Sort(ByModTime(files))
t := time.Now().Unix()
t -= cfg.MaxAge
// iterate the list, oldest first
for i := len(files) - 1; i >= 0; i-- {
file := files[i]
// Never remove the active file
if file.Name() == cfg.ActiveFileName {
continue
}
modTime := file.ModTime().Unix()
// start removing from top if timestamp older than given
if modTime < t {
err := os.Remove(path.Join(cfg.DirName, file.Name()))
if err != nil {
return err
}
continue
}
// then check if remaining count is more than max, then keep deleting
if i+1 > cfg.MaxCount {
err := os.Remove(path.Join(cfg.DirName, file.Name()))
if err != nil {
return err
}
}
}
return nil
}