Plugins: Move store init to dskit service (#111823)
This commit is contained in:
@@ -3,9 +3,21 @@ package adapter
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||
"github.com/grafana/grafana/pkg/modules"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/plugininstaller"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore"
|
||||
"github.com/grafana/grafana/pkg/services/provisioning"
|
||||
)
|
||||
|
||||
const (
|
||||
// PluginStore is the module name for the plugin store service.
|
||||
PluginStore = pluginstore.ServiceName
|
||||
|
||||
// PluginInstaller is the module name for the plugin installer service.
|
||||
PluginInstaller = plugininstaller.ServiceName
|
||||
|
||||
// Provisioning is the module name for the provisioning service.
|
||||
Provisioning = provisioning.ServiceName
|
||||
|
||||
// Tracing is the module name for the tracing service.
|
||||
Tracing = tracing.ServiceName
|
||||
|
||||
@@ -29,7 +41,10 @@ func dependencyMap() map[string][]string {
|
||||
return map[string][]string{
|
||||
Tracing: {},
|
||||
GrafanaAPIServer: {Tracing},
|
||||
Core: {GrafanaAPIServer},
|
||||
PluginStore: {GrafanaAPIServer},
|
||||
PluginInstaller: {PluginStore},
|
||||
Provisioning: {PluginStore, PluginInstaller},
|
||||
Core: {GrafanaAPIServer, PluginStore, PluginInstaller, Provisioning},
|
||||
BackgroundServices: {Core},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
stopTimeout = 30 * time.Second
|
||||
stopTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
type ManagerAdapter struct {
|
||||
@@ -63,6 +63,7 @@ func (m *ManagerAdapter) starting(ctx context.Context) error {
|
||||
// skip disabled services
|
||||
if s, ok := bgSvc.(registry.CanBeDisabled); ok && s.IsDisabled() {
|
||||
logger.Debug("Skipping disabled service", "service", namedService.ServiceName())
|
||||
manager.RegisterInvisibleModule(namedService.ServiceName(), nil)
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -18,9 +18,10 @@ var _ services.NamedService = &serviceAdapter{}
|
||||
// The adapter uses dskit's BasicService with a custom RunningFn:
|
||||
// - Starting phase: No-op, transitions immediately to Running
|
||||
// - Running phase: Delegates to the wrapped service's Run method
|
||||
// - Stopping phase: No-op, transitions immediately to Terminated/Failed
|
||||
// - Stopping phase: Closes the stop channel to signal the service to stop
|
||||
type serviceAdapter struct {
|
||||
*services.BasicService
|
||||
services.NamedService
|
||||
stopCh chan struct{}
|
||||
name string
|
||||
service registry.BackgroundService
|
||||
}
|
||||
@@ -36,8 +37,9 @@ func asNamedService(service registry.BackgroundService) *serviceAdapter {
|
||||
a := &serviceAdapter{
|
||||
name: name,
|
||||
service: service,
|
||||
stopCh: make(chan struct{}),
|
||||
}
|
||||
a.BasicService = services.NewBasicService(nil, a.run, nil).WithName(name)
|
||||
a.NamedService = services.NewBasicService(nil, a.running, a.stopping).WithName(name)
|
||||
return a
|
||||
}
|
||||
|
||||
@@ -46,13 +48,24 @@ func asNamedService(service registry.BackgroundService) *serviceAdapter {
|
||||
// background service's Run method. If the background service completes without
|
||||
// error, the adapter waits for context cancellation (service stop) before
|
||||
// transitioning to Stopping state, ensuring proper dskit service lifecycle.
|
||||
func (a *serviceAdapter) run(ctx context.Context) error {
|
||||
err := a.service.Run(ctx)
|
||||
func (a *serviceAdapter) running(ctx context.Context) error {
|
||||
serviceCtx, serviceCancel := context.WithCancel(ctx)
|
||||
go func() {
|
||||
<-a.stopCh
|
||||
serviceCancel()
|
||||
}()
|
||||
|
||||
err := a.service.Run(serviceCtx)
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
return err
|
||||
}
|
||||
// wait for context cancellation to transition to Stopping state.
|
||||
// this prevents the service from causing it's dependents to stop prematurely.
|
||||
<-ctx.Done()
|
||||
<-serviceCtx.Done()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *serviceAdapter) stopping(_ error) error {
|
||||
close(a.stopCh)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ func TestAsNamedService(t *testing.T) {
|
||||
adapter := asNamedService(mockSvc)
|
||||
|
||||
require.NotNil(t, adapter)
|
||||
require.NotNil(t, adapter.BasicService)
|
||||
require.NotNil(t, adapter.NamedService)
|
||||
require.Equal(t, mockSvc, adapter.service)
|
||||
|
||||
expectedName := reflect.TypeOf(mockSvc).String()
|
||||
|
||||
Reference in New Issue
Block a user