mirror of
https://github.com/fankes/beszel.git
synced 2025-10-20 02:09:28 +08:00
use txDao in deleteOldRecords for deletion only
This commit is contained in:
14
hub/main.go
14
hub/main.go
@@ -22,7 +22,6 @@ 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,15 +104,12 @@ 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() {
|
||||||
app.Dao().RunInTransaction(func(txDao *daos.Dao) error {
|
|
||||||
collections := []string{"system_stats", "container_stats"}
|
collections := []string{"system_stats", "container_stats"}
|
||||||
deleteOldRecords(txDao, collections, "1m", time.Hour)
|
deleteOldRecords(collections, "1m", time.Hour)
|
||||||
deleteOldRecords(txDao, collections, "10m", 12*time.Hour)
|
deleteOldRecords(collections, "10m", 12*time.Hour)
|
||||||
deleteOldRecords(txDao, collections, "20m", 24*time.Hour)
|
deleteOldRecords(collections, "20m", 24*time.Hour)
|
||||||
deleteOldRecords(txDao, collections, "120m", 7*24*time.Hour)
|
deleteOldRecords(collections, "120m", 7*24*time.Hour)
|
||||||
deleteOldRecords(txDao, collections, "480m", 30*24*time.Hour)
|
deleteOldRecords(collections, "480m", 30*24*time.Hour)
|
||||||
return nil
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
scheduler.Start()
|
scheduler.Start()
|
||||||
return nil
|
return nil
|
||||||
|
@@ -141,7 +141,7 @@ func twoDecimals(value float64) float64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Delete records of specified collections and type that are older than timeLimit */
|
/* Delete records of specified collections and type that are older than timeLimit */
|
||||||
func deleteOldRecords(txDao *daos.Dao, collections []string, recordType string, timeLimit time.Duration) {
|
func deleteOldRecords(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")
|
||||||
|
|
||||||
// db query
|
// db query
|
||||||
@@ -150,12 +150,18 @@ func deleteOldRecords(txDao *daos.Dao, collections []string, recordType string,
|
|||||||
|
|
||||||
var records []*models.Record
|
var records []*models.Record
|
||||||
for _, collection := range collections {
|
for _, collection := range collections {
|
||||||
if collectionRecords, err := txDao.FindRecordsByExpr(collection, expType, expCreated); err == nil {
|
if collectionRecords, err := app.Dao().FindRecordsByExpr(collection, expType, expCreated); err == nil {
|
||||||
records = append(records, collectionRecords...)
|
records = append(records, collectionRecords...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.Dao().RunInTransaction(func(txDao *daos.Dao) error {
|
||||||
for _, record := range records {
|
for _, record := range records {
|
||||||
txDao.DeleteRecord(record)
|
err := txDao.DeleteRecord(record)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user