Skip to content

Commit

Permalink
Refresh package managers after setup (#4397)
Browse files Browse the repository at this point in the history
* Refresh package managers after setup
* Fix default plugins/scrapers paths
  • Loading branch information
DingDongSoLong4 authored Dec 26, 2023
1 parent 9bd3640 commit 6ee7e61
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
5 changes: 5 additions & 0 deletions internal/api/resolver_query_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"errors"
"fmt"
"sort"
"strings"

Expand All @@ -26,6 +27,10 @@ func getPackageManager(typeArg PackageType) (*pkg.Manager, error) {
return nil, ErrInvalidPackageType
}

if pm == nil {
return nil, fmt.Errorf("%s package manager not initialized", typeArg)
}

return pm, nil
}

Expand Down
28 changes: 4 additions & 24 deletions internal/manager/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
Expand All @@ -21,7 +20,6 @@ import (
"github.com/stashapp/stash/pkg/job"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models/paths"
"github.com/stashapp/stash/pkg/pkg"
"github.com/stashapp/stash/pkg/plugin"
"github.com/stashapp/stash/pkg/scene"
"github.com/stashapp/stash/pkg/scraper"
Expand Down Expand Up @@ -102,9 +100,6 @@ func Initialize(cfg *config.Config, l *log.Logger) (*Manager, error) {
scanSubs: &subscriptionManager{},
}

mgr.RefreshPluginSourceManager()
mgr.RefreshScraperSourceManager()

if !cfg.IsNewSystem() {
logger.Infof("using config file: %s", cfg.GetConfigFile())

Expand Down Expand Up @@ -135,25 +130,6 @@ func Initialize(cfg *config.Config, l *log.Logger) (*Manager, error) {
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 {
switch {
case t >= time.Minute: // 1m23s or 2h45m12s
Expand Down Expand Up @@ -208,7 +184,11 @@ func (s *Manager) postInit(ctx context.Context) error {
s.PluginCache.RegisterSessionStore(s.SessionStore)

s.RefreshPluginCache()
s.RefreshPluginSourceManager()

s.RefreshScraperCache()
s.RefreshScraperSourceManager()

s.RefreshStreamManager()
s.RefreshDLNA()

Expand Down
33 changes: 27 additions & 6 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"
"time"

"github.com/stashapp/stash/internal/dlna"
"github.com/stashapp/stash/internal/log"
Expand Down Expand Up @@ -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() {
s.ScraperPackageManager = initialisePackageManager(s.Config.GetScrapersPath(), s.Config.GetScraperPackagePathGetter())
s.ScraperPackageManager = createPackageManager(s.Config.GetScrapersPath(), s.Config.GetScraperPackagePathGetter())
}

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) {
Expand Down Expand Up @@ -179,10 +200,6 @@ func (s *Manager) Setup(ctx context.Context, input SetupInput) error {
setSetupDefaults(&input)
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
// don't do anything if config is already set in the environment
if !config.FileEnvSet() {
Expand All @@ -207,6 +224,10 @@ func (s *Manager) Setup(ctx context.Context, input SetupInput) error {
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
if !cfg.HasOverride(config.Generated) {
if exists, _ := fsutil.DirExists(input.GeneratedLocation); !exists {
Expand Down

0 comments on commit 6ee7e61

Please sign in to comment.