From 94829d9b83abfa5b2b5aa6c8dfe6cae210612b3c Mon Sep 17 00:00:00 2001 From: Adhityaa Chandrasekar Date: Thu, 31 Jan 2019 02:19:12 -0500 Subject: [PATCH] api: add cron job to clean up views table --- api/cron_views_cleanup.go | 25 +++++++++++++++++++++++++ api/main.go | 1 + 2 files changed, 26 insertions(+) create mode 100644 api/cron_views_cleanup.go 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()) }