From de7e07963d5baaac7a8fca3d6797796e0152bd6a Mon Sep 17 00:00:00 2001 From: Henry Dollman Date: Wed, 31 Jul 2024 16:26:41 -0400 Subject: [PATCH] improve efficiency of hourly cleanup operation --- hub/main.go | 20 ++++++++++---------- hub/records.go | 26 ++++++++++++++++---------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/hub/main.go b/hub/main.go index b9df259..45a0de8 100644 --- a/hub/main.go +++ b/hub/main.go @@ -22,6 +22,7 @@ import ( "github.com/pocketbase/pocketbase" "github.com/pocketbase/pocketbase/apis" "github.com/pocketbase/pocketbase/core" + "github.com/pocketbase/pocketbase/daos" "github.com/pocketbase/pocketbase/models" "github.com/pocketbase/pocketbase/plugins/migratecmd" "github.com/pocketbase/pocketbase/tools/cron" @@ -105,16 +106,15 @@ func main() { // cron job to delete old records scheduler := cron.New() scheduler.MustAdd("delete old records", "8 * * * *", func() { - deleteOldRecords("system_stats", "1m", time.Hour) - deleteOldRecords("container_stats", "1m", time.Hour) - deleteOldRecords("system_stats", "10m", 12*time.Hour) - deleteOldRecords("container_stats", "10m", 12*time.Hour) - deleteOldRecords("system_stats", "20m", 24*time.Hour) - deleteOldRecords("container_stats", "20m", 24*time.Hour) - deleteOldRecords("system_stats", "120m", 7*24*time.Hour) - deleteOldRecords("container_stats", "120m", 7*24*time.Hour) - deleteOldRecords("system_stats", "480m", 30*24*time.Hour) - deleteOldRecords("container_stats", "480m", 30*24*time.Hour) + app.Dao().RunInTransaction(func(txDao *daos.Dao) error { + collections := []string{"system_stats", "container_stats"} + deleteOldRecords(txDao, collections, "1m", time.Hour) + deleteOldRecords(txDao, collections, "10m", 12*time.Hour) + deleteOldRecords(txDao, collections, "20m", 24*time.Hour) + deleteOldRecords(txDao, collections, "120m", 7*24*time.Hour) + deleteOldRecords(txDao, collections, "480m", 30*24*time.Hour) + return nil + }) }) scheduler.Start() return nil diff --git a/hub/records.go b/hub/records.go index 351f810..748961e 100644 --- a/hub/records.go +++ b/hub/records.go @@ -3,12 +3,12 @@ package main import ( "encoding/json" "fmt" - "log" "math" "reflect" "time" "github.com/pocketbase/dbx" + "github.com/pocketbase/pocketbase/daos" "github.com/pocketbase/pocketbase/models" "github.com/pocketbase/pocketbase/tools/types" ) @@ -138,16 +138,22 @@ func twoDecimals(value float64) float64 { return math.Round(value*100) / 100 } -/* Delete records of specified collection and type that are older than timeLimit */ -func deleteOldRecords(collection string, recordType string, timeLimit time.Duration) { +/* Delete records of specified collections and type that are older than timeLimit */ +func deleteOldRecords(txDao *daos.Dao, collections []string, recordType string, timeLimit time.Duration) { timeLimitStamp := time.Now().UTC().Add(-timeLimit).Format("2006-01-02 15:04:05") - records, _ := app.Dao().FindRecordsByExpr(collection, - dbx.NewExp("type = {:type}", dbx.Params{"type": recordType}), - dbx.NewExp("created < {:created}", dbx.Params{"created": timeLimitStamp}), - ) - for _, record := range records { - if err := app.Dao().DeleteRecord(record); err != nil { - log.Fatal(err) + + // db query + expType := dbx.NewExp("type = {:type}", dbx.Params{"type": recordType}) + expCreated := dbx.NewExp("created < {:created}", dbx.Params{"created": timeLimitStamp}) + + var records []*models.Record + for _, collection := range collections { + if collectionRecords, err := txDao.FindRecordsByExpr(collection, expType, expCreated); err == nil { + records = append(records, collectionRecords...) } } + + for _, record := range records { + txDao.DeleteRecord(record) + } }