feat: batch reload all storages (close #2762 pr #2775)

pull/2785/head
BoYanZh 2022-12-21 19:21:18 +08:00 committed by GitHub
parent 3a41b929c9
commit 3af23f6792
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -1,8 +1,10 @@
package handles
import (
"context"
"strconv"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
@ -116,3 +118,35 @@ func GetStorage(c *gin.Context) {
}
common.SuccessResp(c, storage)
}
func LoadAllStorages(c *gin.Context) {
storages, err := db.GetEnabledStorages()
if err != nil {
log.Errorf("failed get enabled storages: %+v", err)
common.ErrorResp(c, err, 500, true)
return
}
conf.StoragesLoaded = false
go func(storages []model.Storage) {
for _, storage := range storages {
storageDriver, err := op.GetStorageByMountPath(storage.MountPath)
if err != nil {
log.Errorf("failed get storage driver: %+v", err)
continue
}
// drop the storage in the driver
if err := storageDriver.Drop(context.Background()); err != nil {
log.Errorf("failed drop storage: %+v", err)
continue
}
if err := op.LoadStorage(context.Background(), storage); err != nil {
log.Errorf("failed get enabled storages: %+v", err)
continue
}
log.Infof("success load storage: [%s], driver: [%s]",
storage.MountPath, storage.Driver)
}
conf.StoragesLoaded = true
}(storages)
common.SuccessResp(c)
}

View File

@ -71,6 +71,7 @@ func admin(g *gin.RouterGroup) {
storage.POST("/delete", handles.DeleteStorage)
storage.POST("/enable", handles.EnableStorage)
storage.POST("/disable", handles.DisableStorage)
storage.POST("/load_all", handles.LoadAllStorages)
driver := g.Group("/driver")
driver.GET("/list", handles.ListDriverInfo)