mirror of
https://github.com/fankes/beszel.git
synced 2025-10-19 01:39:34 +08:00
improve efficiency of hourly cleanup operation
This commit is contained in:
20
hub/main.go
20
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
|
||||
|
@@ -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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user