From 508f7b84f2e4c8a8dbb02d90e859a25881818a9a Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Thu, 3 Jun 2021 11:00:17 +1000 Subject: [PATCH] Fix plugin cache initialisation (#1475) --- pkg/api/resolver_mutation_plugin.go | 2 +- pkg/manager/manager.go | 21 +++++++------------ pkg/plugin/plugins.go | 32 +++++++++++++---------------- 3 files changed, 22 insertions(+), 33 deletions(-) diff --git a/pkg/api/resolver_mutation_plugin.go b/pkg/api/resolver_mutation_plugin.go index 3e65366c2..4ad20c856 100644 --- a/pkg/api/resolver_mutation_plugin.go +++ b/pkg/api/resolver_mutation_plugin.go @@ -40,7 +40,7 @@ func (r *mutationResolver) RunPluginTask(ctx context.Context, pluginID string, t } func (r *mutationResolver) ReloadPlugins(ctx context.Context) (bool, error) { - err := manager.GetInstance().PluginCache.ReloadPlugins() + err := manager.GetInstance().PluginCache.LoadPlugins() if err != nil { logger.Errorf("Error reading plugin configs: %s", err.Error()) } diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index a47512eb5..82fefc6f7 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -69,6 +69,7 @@ func Initialize() *singleton { Config: cfg, JobManager: job.NewManager(), DownloadStore: NewDownloadStore(), + PluginCache: plugin.NewCache(cfg), TxnManager: sqlite.NewTransactionManager(), @@ -170,17 +171,6 @@ func initLog() { logger.Init(config.GetLogFile(), config.GetLogOut(), config.GetLogLevel()) } -func initPluginCache() *plugin.Cache { - config := config.GetInstance() - ret, err := plugin.NewCache(config.GetPluginsPath()) - - if err != nil { - logger.Errorf("Error reading plugin configs: %s", err.Error()) - } - - return ret -} - // PostInit initialises the paths, caches and txnManager after the initial // configuration has been set. Should only be called if the configuration // is valid. @@ -188,11 +178,14 @@ func (s *singleton) PostInit() error { s.Config.SetInitialConfig() s.Paths = paths.NewPaths(s.Config.GetGeneratedPath()) - s.PluginCache = initPluginCache() - s.ScraperCache = instance.initScraperCache() - s.RefreshConfig() + if err := s.PluginCache.LoadPlugins(); err != nil { + logger.Errorf("Error reading plugin configs: %s", err.Error()) + } + + s.ScraperCache = instance.initScraperCache() + // clear the downloads and tmp directories // #1021 - only clear these directories if the generated folder is non-empty if s.Config.GetGeneratedPath() != "" { diff --git a/pkg/plugin/plugins.go b/pkg/plugin/plugins.go index 33e6acb72..1eb218f1c 100644 --- a/pkg/plugin/plugins.go +++ b/pkg/plugin/plugins.go @@ -14,44 +14,40 @@ import ( "path/filepath" "github.com/stashapp/stash/pkg/logger" + "github.com/stashapp/stash/pkg/manager/config" "github.com/stashapp/stash/pkg/models" "github.com/stashapp/stash/pkg/plugin/common" ) // Cache stores plugin details. type Cache struct { - path string + config *config.Instance plugins []Config gqlHandler http.HandlerFunc } -// NewCache returns a new Cache loading plugin configurations -// from the provided plugin path. It returns an new instance and an error -// if the plugin directory could not be loaded. +// NewCache returns a new Cache. // -// Plugins configurations are loaded from yml files in the provided plugin -// directory and any subdirectories. -func NewCache(pluginPath string) (*Cache, error) { - plugins, err := loadPlugins(pluginPath) - if err != nil { - return nil, err - } - +// Plugins configurations are loaded from yml files in the plugin +// directory in the config and any subdirectories. +// +// Does not load plugins. Plugins will need to be +// loaded explicitly using ReloadPlugins. +func NewCache(config *config.Instance) *Cache { return &Cache{ - path: pluginPath, - plugins: plugins, - }, nil + config: config, + } } func (c *Cache) RegisterGQLHandler(handler http.HandlerFunc) { c.gqlHandler = handler } -// ReloadPlugins clears the plugin cache and reloads from the plugin path. +// LoadPlugins clears the plugin cache and loads from the plugin path. // In the event of an error during loading, the cache will be left empty. -func (c *Cache) ReloadPlugins() error { +func (c *Cache) LoadPlugins() error { c.plugins = nil - plugins, err := loadPlugins(c.path) + plugins, err := loadPlugins(c.config.GetPluginsPath()) if err != nil { return err }