refactor: harden sqlite runtime and packaged startup flow

- refine config loading with --config and current-directory .env

- improve sqlite busy handling and runtime DB tuning

- restore telegram payment notification and wallet input flow

- add behavior-based config and callback recovery tests
This commit is contained in:
alphago9
2026-03-31 13:54:10 +08:00
parent 08f0c93e11
commit 682aa3907a
23 changed files with 975 additions and 133 deletions
+1 -1
View File
@@ -29,7 +29,7 @@ func MysqlInit() error {
// panic(err)
return err
}
if config.AppDebug {
if config.SQLDebug {
Mdb = Mdb.Debug()
}
sqlDB, err := Mdb.DB()
+1 -1
View File
@@ -40,7 +40,7 @@ func PostgreSQLInit() error {
return err
}
if config.AppDebug {
if config.SQLDebug {
Mdb = Mdb.Debug()
}
sqlDB, err := Mdb.DB()
+7 -16
View File
@@ -1,6 +1,7 @@
package dao
import (
"os"
"path/filepath"
"github.com/assimon/luuu/config"
@@ -16,9 +17,10 @@ import (
// SqliteInit 数据库初始化
func SqliteInit() error {
var err error
dbFilename := "./conf/.db"
if dbfile := viper.GetString("sqlite_database_filename"); len(dbfile) > 0 {
dbFilename = filepath.Base(dbfile)
dbFilename := config.GetPrimarySqlitePath()
if err = os.MkdirAll(filepath.Dir(dbFilename), 0o755); err != nil {
color.Red.Printf("[store_db] sqlite mkdir err=%s\n", err)
return err
}
color.Green.Printf("[store_db] sqlite filename: %s\n", dbFilename)
Mdb, err = openDB(dbFilename, &gorm.Config{
@@ -33,22 +35,11 @@ func SqliteInit() error {
// panic(err)
return err
}
if config.AppDebug {
if config.SQLDebug {
Mdb = Mdb.Debug()
}
sqlDB, err := Mdb.DB()
if err != nil {
color.Red.Printf("[store_db] sqlite get DB,err=%s\n", err)
// panic(err)
return err
}
// sqlDB.SetMaxIdleConns(viper.GetInt("sqlite_max_idle_conns"))
// sqlDB.SetMaxOpenConns(viper.GetInt("sqlite_max_open_conns"))
// sqlDB.SetConnMaxLifetime(time.Hour * time.Duration(viper.GetInt("sqlite_max_life_time")))
err = sqlDB.Ping()
if err != nil {
if _, err = configureSQLite(Mdb, 1); err != nil {
color.Red.Printf("[store_db] sqlite connDB err:%s", err.Error())
// panic(err)
return err
}
log.Sugar.Debug("[store_db] sqlite connDB success")
+1 -19
View File
@@ -27,12 +27,6 @@ func RuntimeInit() error {
return err
}
sqlDB, err := RuntimeDB.DB()
if err != nil {
color.Red.Printf("[runtime_db] sqlite get DB,err=%s\n", err)
return err
}
concurrency := config.GetQueueConcurrency()
if concurrency < 2 {
concurrency = 2
@@ -40,19 +34,7 @@ func RuntimeInit() error {
if concurrency > 16 {
concurrency = 16
}
sqlDB.SetMaxOpenConns(concurrency)
sqlDB.SetMaxIdleConns(1)
if err = RuntimeDB.Exec("PRAGMA journal_mode=WAL;").Error; err != nil {
return err
}
if err = RuntimeDB.Exec("PRAGMA synchronous=NORMAL;").Error; err != nil {
return err
}
if err = RuntimeDB.Exec("PRAGMA busy_timeout=5000;").Error; err != nil {
return err
}
if err = sqlDB.Ping(); err != nil {
if _, err = configureSQLite(RuntimeDB, concurrency); err != nil {
color.Red.Printf("[runtime_db] sqlite connDB err:%s", err.Error())
return err
}
+35
View File
@@ -0,0 +1,35 @@
package dao
import (
"database/sql"
"gorm.io/gorm"
)
func configureSQLite(db *gorm.DB, maxOpenConns int) (*sql.DB, error) {
sqlDB, err := db.DB()
if err != nil {
return nil, err
}
if maxOpenConns <= 0 {
maxOpenConns = 1
}
sqlDB.SetMaxOpenConns(maxOpenConns)
sqlDB.SetMaxIdleConns(1)
if err := db.Exec("PRAGMA journal_mode=WAL;").Error; err != nil {
return nil, err
}
if err := db.Exec("PRAGMA synchronous=NORMAL;").Error; err != nil {
return nil, err
}
if err := db.Exec("PRAGMA busy_timeout=5000;").Error; err != nil {
return nil, err
}
if err := sqlDB.Ping(); err != nil {
return nil, err
}
return sqlDB, nil
}