Chore: Move backgroundsvcs and usagestatssvcs to registry (#72692)

This commit is contained in:
Todd Treece
2023-08-02 09:25:55 -04:00
committed by GitHub
parent 7ec9a0c9da
commit 29fef40f26
9 changed files with 7 additions and 7 deletions
@@ -1,105 +0,0 @@
package backgroundsvcs
import (
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/infra/remotecache"
"github.com/grafana/grafana/pkg/infra/tracing"
uss "github.com/grafana/grafana/pkg/infra/usagestats/service"
"github.com/grafana/grafana/pkg/infra/usagestats/statscollector"
"github.com/grafana/grafana/pkg/plugins/manager/process"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/services/auth"
"github.com/grafana/grafana/pkg/services/cleanup"
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
"github.com/grafana/grafana/pkg/services/grpcserver"
"github.com/grafana/grafana/pkg/services/guardian"
ldapapi "github.com/grafana/grafana/pkg/services/ldap/api"
"github.com/grafana/grafana/pkg/services/live"
"github.com/grafana/grafana/pkg/services/live/pushhttp"
"github.com/grafana/grafana/pkg/services/login/authinfoservice"
"github.com/grafana/grafana/pkg/services/loginattempt/loginattemptimpl"
"github.com/grafana/grafana/pkg/services/ngalert"
"github.com/grafana/grafana/pkg/services/notifications"
plugindashboardsservice "github.com/grafana/grafana/pkg/services/plugindashboards/service"
"github.com/grafana/grafana/pkg/services/pluginsintegration/angulardetectorsprovider"
"github.com/grafana/grafana/pkg/services/pluginsintegration/keyretriever/dynamic"
"github.com/grafana/grafana/pkg/services/provisioning"
publicdashboardsmetric "github.com/grafana/grafana/pkg/services/publicdashboards/metric"
"github.com/grafana/grafana/pkg/services/rendering"
"github.com/grafana/grafana/pkg/services/searchV2"
secretsManager "github.com/grafana/grafana/pkg/services/secrets/manager"
"github.com/grafana/grafana/pkg/services/serviceaccounts"
samanager "github.com/grafana/grafana/pkg/services/serviceaccounts/manager"
"github.com/grafana/grafana/pkg/services/store"
"github.com/grafana/grafana/pkg/services/store/entity"
"github.com/grafana/grafana/pkg/services/store/sanitizer"
"github.com/grafana/grafana/pkg/services/supportbundles/supportbundlesimpl"
"github.com/grafana/grafana/pkg/services/updatechecker"
)
func ProvideBackgroundServiceRegistry(
ng *ngalert.AlertNG, cleanup *cleanup.CleanUpService, live *live.GrafanaLive,
pushGateway *pushhttp.Gateway, notifications *notifications.NotificationService, processManager *process.Manager,
rendering *rendering.RenderingService, tokenService auth.UserTokenBackgroundService, tracing tracing.Tracer,
provisioning *provisioning.ProvisioningServiceImpl, alerting *alerting.AlertEngine, usageStats *uss.UsageStats,
statsCollector *statscollector.Service, grafanaUpdateChecker *updatechecker.GrafanaService,
pluginsUpdateChecker *updatechecker.PluginsService, metrics *metrics.InternalMetricsService,
secretsService *secretsManager.SecretsService, remoteCache *remotecache.RemoteCache, StorageService store.StorageService, searchService searchV2.SearchService, entityEventsService store.EntityEventsService,
saService *samanager.ServiceAccountsService, authInfoService *authinfoservice.Implementation,
grpcServerProvider grpcserver.Provider, loginAttemptService *loginattemptimpl.Service,
bundleService *supportbundlesimpl.Service,
publicDashboardsMetric *publicdashboardsmetric.Service,
keyRetriever *dynamic.KeyRetriever,
dynamicAngularDetectorsProvider *angulardetectorsprovider.Dynamic,
// Need to make sure these are initialized, is there a better place to put them?
_ dashboardsnapshots.Service, _ *alerting.AlertNotificationService,
_ serviceaccounts.Service, _ *guardian.Provider,
_ *plugindashboardsservice.DashboardUpdater, _ *sanitizer.Provider,
_ *grpcserver.HealthService, _ entity.EntityStoreServer, _ *grpcserver.ReflectionService, _ *ldapapi.Service,
) *BackgroundServiceRegistry {
return NewBackgroundServiceRegistry(
ng,
cleanup,
live,
pushGateway,
notifications,
rendering,
tokenService,
provisioning,
alerting,
grafanaUpdateChecker,
pluginsUpdateChecker,
metrics,
usageStats,
statsCollector,
tracing,
remoteCache,
secretsService,
StorageService,
searchService,
entityEventsService,
grpcServerProvider,
saService,
authInfoService,
processManager,
loginAttemptService,
bundleService,
publicDashboardsMetric,
keyRetriever,
dynamicAngularDetectorsProvider,
)
}
// BackgroundServiceRegistry provides background services.
type BackgroundServiceRegistry struct {
services []registry.BackgroundService
}
func NewBackgroundServiceRegistry(s ...registry.BackgroundService) *BackgroundServiceRegistry {
return &BackgroundServiceRegistry{services: s}
}
func (r *BackgroundServiceRegistry) GetServices() []registry.BackgroundService {
return r.services
}
@@ -1,13 +0,0 @@
package backgroundsvcs
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
)
func TestBackgroundServiceRegistry_GetServices(t *testing.T) {
s := NewBackgroundServiceRegistry(newTestService("A", nil, false), newTestService("B", errors.New("boom"), false))
require.Len(t, s.GetServices(), 2)
}
-64
View File
@@ -1,64 +0,0 @@
package backgroundsvcs
import (
"context"
"errors"
"fmt"
"reflect"
"golang.org/x/sync/errgroup"
"github.com/grafana/dskit/services"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/modules"
"github.com/grafana/grafana/pkg/registry"
)
// BackgroundServiceRunner provides a runner for background services.
type BackgroundServiceRunner struct {
*services.BasicService
registry registry.BackgroundServiceRegistry
log log.Logger
}
func ProvideBackgroundServiceRunner(registry registry.BackgroundServiceRegistry) *BackgroundServiceRunner {
r := &BackgroundServiceRunner{registry: registry, log: log.New("background-services-runner")}
r.BasicService = services.NewBasicService(nil, r.run, nil).WithName(modules.BackgroundServices)
return r
}
func (r *BackgroundServiceRunner) run(ctx context.Context) error {
childRoutines, childCtx := errgroup.WithContext(ctx)
// Start background services.
for _, svc := range r.registry.GetServices() {
if registry.IsDisabled(svc) {
continue
}
service := svc
serviceName := reflect.TypeOf(service).String()
childRoutines.Go(func() error {
select {
case <-childCtx.Done():
return childCtx.Err()
default:
}
r.log.Debug("Starting background service", "service", serviceName)
err := service.Run(childCtx)
// Do not return context.Canceled error since errgroup.Group only
// returns the first error to the caller - thus we can miss a more
// interesting error.
if err != nil && !errors.Is(err, context.Canceled) {
r.log.Error("Stopped background service", "service", serviceName, "reason", err)
return fmt.Errorf("%s run error: %w", serviceName, err)
}
r.log.Debug("Stopped background service", "service", serviceName, "reason", err)
return nil
})
}
r.log.Debug("Waiting on services...")
return childRoutines.Wait()
}
-52
View File
@@ -1,52 +0,0 @@
package backgroundsvcs
import (
"context"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestBackgroundServiceRunner_Run_Error(t *testing.T) {
testErr := errors.New("boom")
registry := NewBackgroundServiceRegistry(newTestService("A", nil, false), newTestService("B", testErr, false))
r := ProvideBackgroundServiceRunner(registry)
err := r.run(context.Background())
require.ErrorIs(t, err, testErr)
}
type testBackgroundService struct {
name string
started chan struct{}
runErr error
isDisabled bool
}
func newTestService(name string, runErr error, disabled bool) *testBackgroundService {
return &testBackgroundService{
name: name,
started: make(chan struct{}),
runErr: runErr,
isDisabled: disabled,
}
}
func (s *testBackgroundService) Run(ctx context.Context) error {
if s.isDisabled {
return fmt.Errorf("shouldn't run disabled service")
}
if s.runErr != nil {
return s.runErr
}
close(s.started)
<-ctx.Done()
return ctx.Err()
}
func (s *testBackgroundService) IsDisabled() bool {
return s.isDisabled
}
@@ -1,29 +0,0 @@
package usagestatssvcs
import (
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/user"
)
func ProvideUsageStatsProvidersRegistry(
accesscontrol accesscontrol.Service,
user user.Service,
) *UsageStatsProvidersRegistry {
return NewUsageStatsProvidersRegistry(
accesscontrol,
user,
)
}
type UsageStatsProvidersRegistry struct {
Services []registry.ProvidesUsageStats
}
func NewUsageStatsProvidersRegistry(services ...registry.ProvidesUsageStats) *UsageStatsProvidersRegistry {
return &UsageStatsProvidersRegistry{services}
}
func (r *UsageStatsProvidersRegistry) GetServices() []registry.ProvidesUsageStats {
return r.Services
}
+2 -2
View File
@@ -33,8 +33,8 @@ import (
"github.com/grafana/grafana/pkg/modules"
moduleRegistry "github.com/grafana/grafana/pkg/modules/registry"
pluginDashboards "github.com/grafana/grafana/pkg/plugins/manager/dashboards"
"github.com/grafana/grafana/pkg/registry/backgroundsvcs"
"github.com/grafana/grafana/pkg/registry/corekind"
"github.com/grafana/grafana/pkg/server/backgroundsvcs"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/accesscontrol/acimpl"
"github.com/grafana/grafana/pkg/services/accesscontrol/ossaccesscontrol"
@@ -65,7 +65,7 @@ import (
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/folder"
"github.com/grafana/grafana/pkg/services/folder/folderimpl"
"github.com/grafana/grafana/pkg/services/grafana-apiserver"
grafanaapiserver "github.com/grafana/grafana/pkg/services/grafana-apiserver"
"github.com/grafana/grafana/pkg/services/grpcserver"
grpccontext "github.com/grafana/grafana/pkg/services/grpcserver/context"
"github.com/grafana/grafana/pkg/services/grpcserver/interceptors"
+2 -2
View File
@@ -8,8 +8,8 @@ import (
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/server/backgroundsvcs"
"github.com/grafana/grafana/pkg/server/usagestatssvcs"
"github.com/grafana/grafana/pkg/registry/backgroundsvcs"
"github.com/grafana/grafana/pkg/registry/usagestatssvcs"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/accesscontrol/acimpl"
"github.com/grafana/grafana/pkg/services/accesscontrol/ossaccesscontrol"