diff --git a/cmd/admin.go b/cmd/admin.go index 5dab2b25..3421e5dd 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -19,6 +19,7 @@ var AdminCmd = &cobra.Command{ Short: "Show admin user's info and some operations about admin user's password", Run: func(cmd *cobra.Command, args []string) { Init() + defer Release() admin, err := op.GetAdmin() if err != nil { utils.Log.Errorf("failed get admin user: %+v", err) @@ -57,6 +58,7 @@ var ShowTokenCmd = &cobra.Command{ Short: "Show admin token", Run: func(cmd *cobra.Command, args []string) { Init() + defer Release() token := setting.GetStr(conf.Token) utils.Log.Infof("Admin token: %s", token) }, @@ -64,6 +66,7 @@ var ShowTokenCmd = &cobra.Command{ func setAdminPassword(pwd string) { Init() + defer Release() admin, err := op.GetAdmin() if err != nil { utils.Log.Errorf("failed get admin user: %+v", err) diff --git a/cmd/cancel2FA.go b/cmd/cancel2FA.go index 888b0f06..08fafee8 100644 --- a/cmd/cancel2FA.go +++ b/cmd/cancel2FA.go @@ -15,6 +15,7 @@ var Cancel2FACmd = &cobra.Command{ Short: "Delete 2FA of admin user", Run: func(cmd *cobra.Command, args []string) { Init() + defer Release() admin, err := op.GetAdmin() if err != nil { utils.Log.Errorf("failed to get admin user: %+v", err) diff --git a/cmd/common.go b/cmd/common.go index c60d8731..b4a7081c 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -7,6 +7,7 @@ import ( "github.com/alist-org/alist/v3/internal/bootstrap" "github.com/alist-org/alist/v3/internal/bootstrap/data" + "github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/pkg/utils" log "github.com/sirupsen/logrus" ) @@ -19,6 +20,10 @@ func Init() { bootstrap.InitIndex() } +func Release() { + db.Close() +} + var pid = -1 var pidFile string diff --git a/cmd/server.go b/cmd/server.go index 461389b1..94a60c72 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -100,7 +100,7 @@ the address is defined in config file`, signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) <-quit utils.Log.Println("Shutdown server...") - + Release() ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) defer cancel() var wg sync.WaitGroup diff --git a/cmd/storage.go b/cmd/storage.go index 23b834c8..eabb5b40 100644 --- a/cmd/storage.go +++ b/cmd/storage.go @@ -31,6 +31,7 @@ var disableStorageCmd = &cobra.Command{ } mountPath := args[0] Init() + defer Release() storage, err := db.GetStorageByMountPath(mountPath) if err != nil { utils.Log.Errorf("failed to query storage: %+v", err) @@ -89,6 +90,7 @@ var listStorageCmd = &cobra.Command{ Short: "List all storages", Run: func(cmd *cobra.Command, args []string) { Init() + defer Release() storages, _, err := db.GetStorages(1, -1) if err != nil { utils.Log.Errorf("failed to query storages: %+v", err) diff --git a/internal/db/db.go b/internal/db/db.go index c6a46107..cd3905ff 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -31,3 +31,17 @@ func AutoMigrate(dst ...interface{}) error { func GetDb() *gorm.DB { return db } + +func Close() { + log.Info("closing db") + sqlDB, err := db.DB() + if err != nil { + log.Errorf("failed to get db: %s", err.Error()) + return + } + err = sqlDB.Close() + if err != nil { + log.Errorf("failed to close db: %s", err.Error()) + return + } +}