-
Notifications
You must be signed in to change notification settings - Fork 3
/
purge.go
40 lines (34 loc) · 974 Bytes
/
purge.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
package main
import (
"errors"
"fmt"
"log"
)
func runPurge() error {
if *maxCheckAge < 1 {
return errors.New("max-age can not be zero")
}
failed := false
// purge checks older than N days
res, err := dbConn.Exec("DELETE FROM nameserver_checks WHERE created_at < DATE_SUB(NOW(), INTERVAL ? DAY)", maxCheckAge)
if err != nil {
log.Println(err)
failed = true
} else {
affected, _ := res.RowsAffected()
fmt.Printf("Purged %d checks\n", affected)
}
// purge invalid nameservers checked within the last 7 days that has been invalid for N days
res, err = dbConn.Exec("DELETE FROM nameservers WHERE state='invalid' AND checked_at > DATE_SUB(NOW(), INTERVAL 7 DAY) AND state_changed_at < DATE_SUB(NOW(), INTERVAL ? DAY)", maxCheckAge)
if err != nil {
log.Println(err)
failed = true
} else {
affected, _ := res.RowsAffected()
fmt.Printf("Purged %d nameservers\n", affected)
}
if failed {
return errors.New("purging failed")
}
return nil
}