api: add cron job to clean up views table

This commit is contained in:
Adhityaa Chandrasekar 2019-01-31 02:19:12 -05:00
parent 7be22b091f
commit 94829d9b83
2 changed files with 26 additions and 0 deletions

25
api/cron_views_cleanup.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"time"
)
func viewsCleanupBegin() error {
go func() {
for {
statement := `
DELETE FROM views
WHERE viewDate < $1;
`
_, err := db.Exec(statement, time.Now().UTC().AddDate(0, 0, -45))
if err != nil {
logger.Errorf("error cleaning up views: %v", err)
return
}
time.Sleep(24 * time.Hour)
}
}()
return nil
}

View File

@ -12,6 +12,7 @@ func main() {
exitIfError(sigintCleanupSetup()) exitIfError(sigintCleanupSetup())
exitIfError(versionCheckStart()) exitIfError(versionCheckStart())
exitIfError(domainExportCleanupBegin()) exitIfError(domainExportCleanupBegin())
exitIfError(viewsCleanupBegin())
exitIfError(routesServe()) exitIfError(routesServe())
} }