Refresh package managers after setup (#4397)

* Refresh package managers after setup
* Fix default plugins/scrapers paths
This commit is contained in:
DingDongSoLong4 2023-12-27 01:57:10 +02:00 committed by GitHub
parent 9bd36408ee
commit 6ee7e6112b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 30 deletions

View File

@ -3,6 +3,7 @@ package api
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"sort" "sort"
"strings" "strings"
@ -26,6 +27,10 @@ func getPackageManager(typeArg PackageType) (*pkg.Manager, error) {
return nil, ErrInvalidPackageType return nil, ErrInvalidPackageType
} }
if pm == nil {
return nil, fmt.Errorf("%s package manager not initialized", typeArg)
}
return pm, nil return pm, nil
} }

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -21,7 +20,6 @@ import (
"github.com/stashapp/stash/pkg/job" "github.com/stashapp/stash/pkg/job"
"github.com/stashapp/stash/pkg/logger" "github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models/paths" "github.com/stashapp/stash/pkg/models/paths"
"github.com/stashapp/stash/pkg/pkg"
"github.com/stashapp/stash/pkg/plugin" "github.com/stashapp/stash/pkg/plugin"
"github.com/stashapp/stash/pkg/scene" "github.com/stashapp/stash/pkg/scene"
"github.com/stashapp/stash/pkg/scraper" "github.com/stashapp/stash/pkg/scraper"
@ -102,9 +100,6 @@ func Initialize(cfg *config.Config, l *log.Logger) (*Manager, error) {
scanSubs: &subscriptionManager{}, scanSubs: &subscriptionManager{},
} }
mgr.RefreshPluginSourceManager()
mgr.RefreshScraperSourceManager()
if !cfg.IsNewSystem() { if !cfg.IsNewSystem() {
logger.Infof("using config file: %s", cfg.GetConfigFile()) logger.Infof("using config file: %s", cfg.GetConfigFile())
@ -135,25 +130,6 @@ func Initialize(cfg *config.Config, l *log.Logger) (*Manager, error) {
return mgr, nil return mgr, nil
} }
func initialisePackageManager(localPath string, srcPathGetter pkg.SourcePathGetter) *pkg.Manager {
const timeout = 10 * time.Second
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
Timeout: timeout,
}
return &pkg.Manager{
Local: &pkg.Store{
BaseDir: localPath,
ManifestFile: pkg.ManifestFile,
},
PackagePathGetter: srcPathGetter,
Client: httpClient,
}
}
func formatDuration(t time.Duration) string { func formatDuration(t time.Duration) string {
switch { switch {
case t >= time.Minute: // 1m23s or 2h45m12s case t >= time.Minute: // 1m23s or 2h45m12s
@ -208,7 +184,11 @@ func (s *Manager) postInit(ctx context.Context) error {
s.PluginCache.RegisterSessionStore(s.SessionStore) s.PluginCache.RegisterSessionStore(s.SessionStore)
s.RefreshPluginCache() s.RefreshPluginCache()
s.RefreshPluginSourceManager()
s.RefreshScraperCache() s.RefreshScraperCache()
s.RefreshScraperSourceManager()
s.RefreshStreamManager() s.RefreshStreamManager()
s.RefreshDLNA() s.RefreshDLNA()

View File

@ -4,9 +4,11 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"time"
"github.com/stashapp/stash/internal/dlna" "github.com/stashapp/stash/internal/dlna"
"github.com/stashapp/stash/internal/log" "github.com/stashapp/stash/internal/log"
@ -145,12 +147,31 @@ func (s *Manager) RefreshDLNA() {
} }
} }
func createPackageManager(localPath string, srcPathGetter pkg.SourcePathGetter) *pkg.Manager {
const timeout = 10 * time.Second
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
Timeout: timeout,
}
return &pkg.Manager{
Local: &pkg.Store{
BaseDir: localPath,
ManifestFile: pkg.ManifestFile,
},
PackagePathGetter: srcPathGetter,
Client: httpClient,
}
}
func (s *Manager) RefreshScraperSourceManager() { func (s *Manager) RefreshScraperSourceManager() {
s.ScraperPackageManager = initialisePackageManager(s.Config.GetScrapersPath(), s.Config.GetScraperPackagePathGetter()) s.ScraperPackageManager = createPackageManager(s.Config.GetScrapersPath(), s.Config.GetScraperPackagePathGetter())
} }
func (s *Manager) RefreshPluginSourceManager() { func (s *Manager) RefreshPluginSourceManager() {
s.PluginPackageManager = initialisePackageManager(s.Config.GetPluginsPath(), s.Config.GetPluginPackagePathGetter()) s.PluginPackageManager = createPackageManager(s.Config.GetPluginsPath(), s.Config.GetPluginPackagePathGetter())
} }
func setSetupDefaults(input *SetupInput) { func setSetupDefaults(input *SetupInput) {
@ -179,10 +200,6 @@ func (s *Manager) Setup(ctx context.Context, input SetupInput) error {
setSetupDefaults(&input) setSetupDefaults(&input)
cfg := s.Config cfg := s.Config
if err := cfg.SetInitialConfig(); err != nil {
return fmt.Errorf("error setting initial configuration: %v", err)
}
// create the config directory if it does not exist // create the config directory if it does not exist
// don't do anything if config is already set in the environment // don't do anything if config is already set in the environment
if !config.FileEnvSet() { if !config.FileEnvSet() {
@ -207,6 +224,10 @@ func (s *Manager) Setup(ctx context.Context, input SetupInput) error {
s.Config.SetConfigFile(configFile) s.Config.SetConfigFile(configFile)
} }
if err := cfg.SetInitialConfig(); err != nil {
return fmt.Errorf("error setting initial configuration: %v", err)
}
// create the generated directory if it does not exist // create the generated directory if it does not exist
if !cfg.HasOverride(config.Generated) { if !cfg.HasOverride(config.Generated) {
if exists, _ := fsutil.DirExists(input.GeneratedLocation); !exists { if exists, _ := fsutil.DirExists(input.GeneratedLocation); !exists {