-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilehelper.go
59 lines (50 loc) · 1.84 KB
/
filehelper.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
package main
import (
"fmt"
"io/ioutil"
"os"
"time"
)
// we need a small amount of state to check the last time logs were checked as in
// the event our program crashes or is rescheduled, we may need to look back
// further than the look back interval. We will cap the maximum look back at 6
// hours to ensure we don't abuse the cloudflare API.
func getLastProcessedTime(lookBack int, fileName string) (time.Time, error) {
currentTime := time.Now().Add(time.Duration(-lookBack))
var lastProcessedTime time.Time
fileContents, err := ioutil.ReadFile(fileName)
if err != nil {
if os.IsNotExist(err) {
// This must be the first run ever, we'll scrape 6 hours and then start
lastProcessedTime = currentTime.Add(time.Duration(-maxLookBack) * time.Minute)
return lastProcessedTime, nil
}
return lastProcessedTime, fmt.Errorf("error reading file: %v", err)
}
if len(fileContents) == 0 {
// This must be the first run ever, we'll scrape 6 hours and then start
lastProcessedTime = currentTime.Add(time.Duration(-maxLookBack) * time.Minute)
return lastProcessedTime, nil
}
lastProcessedTime, err = time.Parse(time.RFC3339, string(fileContents))
if err != nil {
return lastProcessedTime, fmt.Errorf("error parsing last processed time: %v", err)
}
if lastProcessedTime.Before(currentTime.Add(-maxLookBack * time.Hour)) {
lastProcessedTime = currentTime.Add(-maxLookBack * time.Hour)
}
return lastProcessedTime, nil
}
// Store the last processed time to file in RFC3339 format
func storeLastProcessedTimeToDisk(lastProcessedTime time.Time, fileName string) error {
file, err := os.Create(fileName)
if err != nil {
return fmt.Errorf("error creating file: %v", err)
}
defer file.Close()
_, err = file.WriteString(lastProcessedTime.Format(time.RFC3339))
if err != nil {
return fmt.Errorf("error writing to file: %v", err)
}
return nil
}