From 14ea27292fc9417fd1d8fbdbba5040c8987e6869 Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Sun, 8 Mar 2026 23:18:42 +0100 Subject: [PATCH] use journal mode WAL for expand db use --- cronjob/WALCheckpointJob.go | 20 ++++++++++++++++++++ cronjob/cronJob.go | 2 ++ database/db.go | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 cronjob/WALCheckpointJob.go diff --git a/cronjob/WALCheckpointJob.go b/cronjob/WALCheckpointJob.go new file mode 100644 index 0000000..f6baa2e --- /dev/null +++ b/cronjob/WALCheckpointJob.go @@ -0,0 +1,20 @@ +package cronjob + +import ( + "github.com/alireza0/s-ui/database" + "github.com/alireza0/s-ui/logger" +) + +type WALCheckpointJob struct{} + +func NewWALCheckpointJob() *WALCheckpointJob { + return &WALCheckpointJob{} +} + +func (s *WALCheckpointJob) Run() { + db := database.GetDB() + _, err := db.Raw("PRAGMA wal_checkpoint(FULL)").Rows() + if err != nil { + logger.Error("Error checkpointing WAL: ", err.Error()) + } +} diff --git a/cronjob/cronJob.go b/cronjob/cronJob.go index be6f880..6f1f24b 100644 --- a/cronjob/cronJob.go +++ b/cronjob/cronJob.go @@ -29,6 +29,8 @@ func (c *CronJob) Start(loc *time.Location, trafficAge int) error { } // Start core if it is not running c.cron.AddJob("@every 5s", NewCheckCoreJob()) + // database WAL checkpoint + c.cron.AddJob("@every 10m", NewWALCheckpointJob()) }() return nil diff --git a/database/db.go b/database/db.go index c09f116..3810bbd 100644 --- a/database/db.go +++ b/database/db.go @@ -54,7 +54,7 @@ func OpenDB(dbPath string) error { if strings.Contains(dbPath, "?") { sep = "&" } - dsn := dbPath + sep + "_busy_timeout=10000" + dsn := dbPath + sep + "_busy_timeout=10000&_journal_mode=WAL" db, err = gorm.Open(sqlite.Open(dsn), c) if config.IsDebug() {