Plugins: Remove registry dependency from process manager (#73241)
simplify
This commit is contained in:
@@ -3,8 +3,10 @@ package store
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/loader"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/registry"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/sources"
|
||||
@@ -27,6 +29,12 @@ func ProvideService(pluginRegistry registry.Service, pluginSources sources.Regis
|
||||
return New(pluginRegistry), nil
|
||||
}
|
||||
|
||||
func (s *Service) Run(ctx context.Context) error {
|
||||
<-ctx.Done()
|
||||
s.shutdown(ctx)
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
func New(pluginRegistry registry.Service) *Service {
|
||||
return &Service{
|
||||
pluginRegistry: pluginRegistry,
|
||||
@@ -120,3 +128,19 @@ func (s *Service) Routes(ctx context.Context) []*plugins.StaticRoute {
|
||||
}
|
||||
return staticRoutes
|
||||
}
|
||||
|
||||
func (s *Service) shutdown(ctx context.Context) {
|
||||
var wg sync.WaitGroup
|
||||
for _, p := range s.pluginRegistry.Plugins(ctx) {
|
||||
wg.Add(1)
|
||||
go func(p backendplugin.Plugin, ctx context.Context) {
|
||||
defer wg.Done()
|
||||
p.Logger().Debug("Stopping plugin")
|
||||
if err := p.Stop(ctx); err != nil {
|
||||
p.Logger().Error("Failed to stop plugin", "error", err)
|
||||
}
|
||||
p.Logger().Debug("Plugin stopped")
|
||||
}(p, ctx)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
@@ -2,12 +2,14 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/log"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/fakes"
|
||||
)
|
||||
|
||||
@@ -177,6 +179,37 @@ func TestStore_SecretsManager(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcessManager_shutdown(t *testing.T) {
|
||||
p := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-datasource", Type: plugins.TypeDataSource}} // Backend: true
|
||||
backend := &fakes.FakeBackendPlugin{}
|
||||
p.RegisterClient(backend)
|
||||
p.SetLogger(log.NewTestLogger())
|
||||
|
||||
ps := New(&fakes.FakePluginRegistry{
|
||||
Store: map[string]*plugins.Plugin{
|
||||
p.ID: p,
|
||||
},
|
||||
})
|
||||
|
||||
pCtx := context.Background()
|
||||
cCtx, cancel := context.WithCancel(pCtx)
|
||||
var wgRun sync.WaitGroup
|
||||
wgRun.Add(1)
|
||||
var runErr error
|
||||
go func() {
|
||||
runErr = ps.Run(cCtx)
|
||||
wgRun.Done()
|
||||
}()
|
||||
|
||||
t.Run("When context is cancelled the plugin is stopped", func(t *testing.T) {
|
||||
cancel()
|
||||
wgRun.Wait()
|
||||
require.ErrorIs(t, runErr, context.Canceled)
|
||||
require.True(t, p.Exited())
|
||||
require.Equal(t, 1, backend.StopCount)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStore_availablePlugins(t *testing.T) {
|
||||
t.Run("Decommissioned plugins are excluded from availablePlugins", func(t *testing.T) {
|
||||
p1 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-datasource"}}
|
||||
|
||||
Reference in New Issue
Block a user