Plugins: Move store init to dskit service (#111823)
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/dskit/services"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/repo"
|
||||
@@ -18,6 +19,8 @@ import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
const ServiceName = "plugin.backgroundinstaller"
|
||||
|
||||
var (
|
||||
installRequestCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: "plugins",
|
||||
@@ -36,6 +39,7 @@ var (
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
services.NamedService
|
||||
cfg *setting.Cfg
|
||||
log log.Logger
|
||||
pluginInstaller plugins.Installer
|
||||
@@ -43,6 +47,7 @@ type Service struct {
|
||||
pluginRepo repo.Service
|
||||
features featuremgmt.FeatureToggles
|
||||
updateChecker pluginchecker.PluginUpdateChecker
|
||||
installComplete chan struct{} // closed when all plugins are installed (used for testing)
|
||||
}
|
||||
|
||||
func ProvideService(
|
||||
@@ -60,21 +65,18 @@ func ProvideService(
|
||||
})
|
||||
|
||||
s := &Service{
|
||||
log: log.New("plugin.backgroundinstaller"),
|
||||
log: log.New(ServiceName),
|
||||
cfg: cfg,
|
||||
pluginInstaller: pluginInstaller,
|
||||
pluginStore: pluginStore,
|
||||
pluginRepo: pluginRepo,
|
||||
features: features,
|
||||
updateChecker: updateChecker,
|
||||
installComplete: make(chan struct{}),
|
||||
}
|
||||
if len(cfg.PreinstallPluginsSync) > 0 {
|
||||
// Block initialization process until plugins are installed
|
||||
err := s.installPluginsWithTimeout(cfg.PreinstallPluginsSync)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
s.NamedService = services.NewBasicService(s.starting, s.running, nil).WithName(ServiceName)
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
@@ -83,24 +85,6 @@ func (s *Service) IsDisabled() bool {
|
||||
return len(s.cfg.PreinstallPluginsAsync) == 0
|
||||
}
|
||||
|
||||
func (s *Service) installPluginsWithTimeout(pluginsToInstall []setting.InstallPlugin) error {
|
||||
// Installation process does not timeout by default nor reuses the context
|
||||
// passed to the request so we need to handle the timeout here.
|
||||
// We could make this timeout configurable in the future.
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
||||
defer cancel()
|
||||
done := make(chan struct{ err error })
|
||||
go func() {
|
||||
done <- struct{ err error }{err: s.installPlugins(ctx, pluginsToInstall, true)}
|
||||
}()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return fmt.Errorf("failed to install plugins: %w", ctx.Err())
|
||||
case d := <-done:
|
||||
return d.err
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) shouldUpdate(ctx context.Context, pluginID, currentVersion string, pluginURL string) bool {
|
||||
// If the plugin is installed from a URL, we cannot check for updates as we do not have the version information
|
||||
// from the repository. Therefore, we assume that the plugin should be updated if the URL is provided.
|
||||
@@ -166,11 +150,34 @@ func (s *Service) installPlugins(ctx context.Context, pluginsToInstall []setting
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) Run(ctx context.Context) error {
|
||||
err := s.installPlugins(ctx, s.cfg.PreinstallPluginsAsync, false)
|
||||
if err != nil {
|
||||
// Unexpected error, asynchronous installation should not return errors
|
||||
s.log.Error("Failed to install plugins", "error", err)
|
||||
func (s *Service) starting(ctx context.Context) error {
|
||||
if len(s.cfg.PreinstallPluginsSync) > 0 {
|
||||
s.log.Info("Installing plugins", "plugins", s.cfg.PreinstallPluginsSync)
|
||||
if err := s.installPlugins(ctx, s.cfg.PreinstallPluginsSync, true); err != nil {
|
||||
s.log.Error("Failed to install plugins", "error", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
s.log.Info("Plugins installed", "plugins", s.cfg.PreinstallPluginsSync)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) running(ctx context.Context) error {
|
||||
if len(s.cfg.PreinstallPluginsAsync) > 0 {
|
||||
s.log.Info("Installing plugins", "plugins", s.cfg.PreinstallPluginsAsync)
|
||||
if err := s.installPlugins(ctx, s.cfg.PreinstallPluginsAsync, false); err != nil {
|
||||
s.log.Error("Failed to install plugins", "error", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
close(s.installComplete)
|
||||
<-ctx.Done()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) Run(ctx context.Context) error {
|
||||
if err := s.StartAsync(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.AwaitTerminated(ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user