Fix plugin cache initialisation (#1475)

This commit is contained in:
WithoutPants 2021-06-03 11:00:17 +10:00 committed by GitHub
parent c98cc73f33
commit 508f7b84f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 33 deletions

View File

@ -40,7 +40,7 @@ func (r *mutationResolver) RunPluginTask(ctx context.Context, pluginID string, t
} }
func (r *mutationResolver) ReloadPlugins(ctx context.Context) (bool, error) { func (r *mutationResolver) ReloadPlugins(ctx context.Context) (bool, error) {
err := manager.GetInstance().PluginCache.ReloadPlugins() err := manager.GetInstance().PluginCache.LoadPlugins()
if err != nil { if err != nil {
logger.Errorf("Error reading plugin configs: %s", err.Error()) logger.Errorf("Error reading plugin configs: %s", err.Error())
} }

View File

@ -69,6 +69,7 @@ func Initialize() *singleton {
Config: cfg, Config: cfg,
JobManager: job.NewManager(), JobManager: job.NewManager(),
DownloadStore: NewDownloadStore(), DownloadStore: NewDownloadStore(),
PluginCache: plugin.NewCache(cfg),
TxnManager: sqlite.NewTransactionManager(), TxnManager: sqlite.NewTransactionManager(),
@ -170,17 +171,6 @@ func initLog() {
logger.Init(config.GetLogFile(), config.GetLogOut(), config.GetLogLevel()) 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 // PostInit initialises the paths, caches and txnManager after the initial
// configuration has been set. Should only be called if the configuration // configuration has been set. Should only be called if the configuration
// is valid. // is valid.
@ -188,11 +178,14 @@ func (s *singleton) PostInit() error {
s.Config.SetInitialConfig() s.Config.SetInitialConfig()
s.Paths = paths.NewPaths(s.Config.GetGeneratedPath()) s.Paths = paths.NewPaths(s.Config.GetGeneratedPath())
s.PluginCache = initPluginCache()
s.ScraperCache = instance.initScraperCache()
s.RefreshConfig() 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 // clear the downloads and tmp directories
// #1021 - only clear these directories if the generated folder is non-empty // #1021 - only clear these directories if the generated folder is non-empty
if s.Config.GetGeneratedPath() != "" { if s.Config.GetGeneratedPath() != "" {

View File

@ -14,44 +14,40 @@ import (
"path/filepath" "path/filepath"
"github.com/stashapp/stash/pkg/logger" "github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/manager/config"
"github.com/stashapp/stash/pkg/models" "github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/plugin/common" "github.com/stashapp/stash/pkg/plugin/common"
) )
// Cache stores plugin details. // Cache stores plugin details.
type Cache struct { type Cache struct {
path string config *config.Instance
plugins []Config plugins []Config
gqlHandler http.HandlerFunc gqlHandler http.HandlerFunc
} }
// NewCache returns a new Cache loading plugin configurations // NewCache returns a new Cache.
// from the provided plugin path. It returns an new instance and an error
// if the plugin directory could not be loaded.
// //
// Plugins configurations are loaded from yml files in the provided plugin // Plugins configurations are loaded from yml files in the plugin
// directory and any subdirectories. // directory in the config and any subdirectories.
func NewCache(pluginPath string) (*Cache, error) { //
plugins, err := loadPlugins(pluginPath) // Does not load plugins. Plugins will need to be
if err != nil { // loaded explicitly using ReloadPlugins.
return nil, err func NewCache(config *config.Instance) *Cache {
}
return &Cache{ return &Cache{
path: pluginPath, config: config,
plugins: plugins, }
}, nil
} }
func (c *Cache) RegisterGQLHandler(handler http.HandlerFunc) { func (c *Cache) RegisterGQLHandler(handler http.HandlerFunc) {
c.gqlHandler = handler 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. // 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 c.plugins = nil
plugins, err := loadPlugins(c.path) plugins, err := loadPlugins(c.config.GetPluginsPath())
if err != nil { if err != nil {
return err return err
} }