diff --git a/api/cron_views_cleanup.go b/api/cron_views_cleanup.go new file mode 100644 index 0000000..8aa2a49 --- /dev/null +++ b/api/cron_views_cleanup.go @@ -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 +} diff --git a/api/main.go b/api/main.go index 2aaeef6..9bffe64 100644 --- a/api/main.go +++ b/api/main.go @@ -12,6 +12,7 @@ func main() { exitIfError(sigintCleanupSetup()) exitIfError(versionCheckStart()) exitIfError(domainExportCleanupBegin()) + exitIfError(viewsCleanupBegin()) exitIfError(routesServe()) }