improve efficiency of hourly cleanup operation

This commit is contained in:
Henry Dollman
2024-07-31 16:26:41 -04:00
parent ac6f50c40c
commit de7e07963d
2 changed files with 26 additions and 20 deletions

View File

@@ -22,6 +22,7 @@ import (
"github.com/pocketbase/pocketbase" "github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis" "github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core" "github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/models" "github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/plugins/migratecmd" "github.com/pocketbase/pocketbase/plugins/migratecmd"
"github.com/pocketbase/pocketbase/tools/cron" "github.com/pocketbase/pocketbase/tools/cron"
@@ -105,16 +106,15 @@ func main() {
// cron job to delete old records // cron job to delete old records
scheduler := cron.New() scheduler := cron.New()
scheduler.MustAdd("delete old records", "8 * * * *", func() { scheduler.MustAdd("delete old records", "8 * * * *", func() {
deleteOldRecords("system_stats", "1m", time.Hour) app.Dao().RunInTransaction(func(txDao *daos.Dao) error {
deleteOldRecords("container_stats", "1m", time.Hour) collections := []string{"system_stats", "container_stats"}
deleteOldRecords("system_stats", "10m", 12*time.Hour) deleteOldRecords(txDao, collections, "1m", time.Hour)
deleteOldRecords("container_stats", "10m", 12*time.Hour) deleteOldRecords(txDao, collections, "10m", 12*time.Hour)
deleteOldRecords("system_stats", "20m", 24*time.Hour) deleteOldRecords(txDao, collections, "20m", 24*time.Hour)
deleteOldRecords("container_stats", "20m", 24*time.Hour) deleteOldRecords(txDao, collections, "120m", 7*24*time.Hour)
deleteOldRecords("system_stats", "120m", 7*24*time.Hour) deleteOldRecords(txDao, collections, "480m", 30*24*time.Hour)
deleteOldRecords("container_stats", "120m", 7*24*time.Hour) return nil
deleteOldRecords("system_stats", "480m", 30*24*time.Hour) })
deleteOldRecords("container_stats", "480m", 30*24*time.Hour)
}) })
scheduler.Start() scheduler.Start()
return nil return nil

View File

@@ -3,12 +3,12 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"math" "math"
"reflect" "reflect"
"time" "time"
"github.com/pocketbase/dbx" "github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/models" "github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/tools/types" "github.com/pocketbase/pocketbase/tools/types"
) )
@@ -138,16 +138,22 @@ func twoDecimals(value float64) float64 {
return math.Round(value*100) / 100 return math.Round(value*100) / 100
} }
/* Delete records of specified collection and type that are older than timeLimit */ /* Delete records of specified collections and type that are older than timeLimit */
func deleteOldRecords(collection string, recordType string, timeLimit time.Duration) { 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") 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}), // db query
dbx.NewExp("created < {:created}", dbx.Params{"created": timeLimitStamp}), expType := dbx.NewExp("type = {:type}", dbx.Params{"type": recordType})
) expCreated := dbx.NewExp("created < {:created}", dbx.Params{"created": timeLimitStamp})
for _, record := range records {
if err := app.Dao().DeleteRecord(record); err != nil { var records []*models.Record
log.Fatal(err) 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)
}
} }