Chore: Add context for dashboards (#39844)

* Add context for dashboards

* Remove GetDashboardCtx

* Remove ctx.TODO
This commit is contained in:
idafurjes
2021-10-05 13:26:24 +02:00
committed by GitHub
parent 697a90699b
commit 2759b16ef5
18 changed files with 112 additions and 113 deletions
@@ -19,7 +19,7 @@ type DashboardProvisioner interface {
PollChanges(ctx context.Context)
GetProvisionerResolvedPath(name string) string
GetAllowUIUpdatesFromConfig(name string) bool
CleanUpOrphanedDashboards()
CleanUpOrphanedDashboards(ctx context.Context)
}
// DashboardProvisionerFactory creates DashboardProvisioners based on input
@@ -77,14 +77,14 @@ func (provider *Provisioner) Provision(ctx context.Context) error {
}
// CleanUpOrphanedDashboards deletes provisioned dashboards missing a linked reader.
func (provider *Provisioner) CleanUpOrphanedDashboards() {
func (provider *Provisioner) CleanUpOrphanedDashboards(ctx context.Context) {
currentReaders := make([]string, len(provider.fileReaders))
for index, reader := range provider.fileReaders {
currentReaders[index] = reader.Cfg.Name
}
if err := bus.Dispatch(&models.DeleteOrphanedProvisionedDashboardsCommand{ReaderNames: currentReaders}); err != nil {
if err := bus.DispatchCtx(ctx, &models.DeleteOrphanedProvisionedDashboardsCommand{ReaderNames: currentReaders}); err != nil {
provider.log.Warn("Failed to delete orphaned provisioned dashboards", "err", err)
}
}
@@ -62,4 +62,4 @@ func (dpm *ProvisionerMock) GetAllowUIUpdatesFromConfig(name string) bool {
}
// CleanUpOrphanedDashboards not implemented for mocks
func (dpm *ProvisionerMock) CleanUpOrphanedDashboards() {}
func (dpm *ProvisionerMock) CleanUpOrphanedDashboards(ctx context.Context) {}
+5 -5
View File
@@ -38,7 +38,7 @@ type ProvisioningService interface {
ProvisionDatasources() error
ProvisionPlugins() error
ProvisionNotifications() error
ProvisionDashboards() error
ProvisionDashboards(ctx context.Context) error
GetDashboardProvisionerResolvedPath(name string) string
GetAllowUIUpdatesFromConfig(name string) bool
}
@@ -104,7 +104,7 @@ func (ps *ProvisioningServiceImpl) RunInitProvisioners() error {
}
func (ps *ProvisioningServiceImpl) Run(ctx context.Context) error {
err := ps.ProvisionDashboards()
err := ps.ProvisionDashboards(ctx)
if err != nil {
ps.log.Error("Failed to provision dashboard", "error", err)
return err
@@ -150,7 +150,7 @@ func (ps *ProvisioningServiceImpl) ProvisionNotifications() error {
return errutil.Wrap("Alert notification provisioning error", err)
}
func (ps *ProvisioningServiceImpl) ProvisionDashboards() error {
func (ps *ProvisioningServiceImpl) ProvisionDashboards(ctx context.Context) error {
dashboardPath := filepath.Join(ps.Cfg.ProvisioningPath, "dashboards")
dashProvisioner, err := ps.newDashboardProvisioner(dashboardPath, ps.SQLStore)
if err != nil {
@@ -161,9 +161,9 @@ func (ps *ProvisioningServiceImpl) ProvisionDashboards() error {
defer ps.mutex.Unlock()
ps.cancelPolling()
dashProvisioner.CleanUpOrphanedDashboards()
dashProvisioner.CleanUpOrphanedDashboards(ctx)
err = dashProvisioner.Provision(context.TODO())
err = dashProvisioner.Provision(ctx)
if err != nil {
// If we fail to provision with the new provisioner, the mutex will unlock and the polling will restart with the
// old provisioner as we did not switch them yet.
@@ -63,7 +63,7 @@ func (mock *ProvisioningServiceMock) ProvisionNotifications() error {
return nil
}
func (mock *ProvisioningServiceMock) ProvisionDashboards() error {
func (mock *ProvisioningServiceMock) ProvisionDashboards(ctx context.Context) error {
mock.Calls.ProvisionDashboards = append(mock.Calls.ProvisionDashboards, nil)
if mock.ProvisionDashboardsFunc != nil {
return mock.ProvisionDashboardsFunc()
@@ -15,14 +15,14 @@ import (
func TestProvisioningServiceImpl(t *testing.T) {
t.Run("Restart dashboard provisioning and stop service", func(t *testing.T) {
serviceTest := setup()
err := serviceTest.service.ProvisionDashboards()
err := serviceTest.service.ProvisionDashboards(context.Background())
assert.Nil(t, err)
serviceTest.startService()
serviceTest.waitForPollChanges()
assert.Equal(t, 1, len(serviceTest.mock.Calls.PollChanges), "PollChanges should have been called")
err = serviceTest.service.ProvisionDashboards()
err = serviceTest.service.ProvisionDashboards(context.Background())
assert.Nil(t, err)
serviceTest.waitForPollChanges()
@@ -42,7 +42,7 @@ func TestProvisioningServiceImpl(t *testing.T) {
t.Run("Failed reloading does not stop polling with old provisioned", func(t *testing.T) {
serviceTest := setup()
err := serviceTest.service.ProvisionDashboards()
err := serviceTest.service.ProvisionDashboards(context.Background())
assert.Nil(t, err)
serviceTest.startService()
serviceTest.waitForPollChanges()
@@ -51,7 +51,7 @@ func TestProvisioningServiceImpl(t *testing.T) {
serviceTest.mock.ProvisionFunc = func(ctx context.Context) error {
return errors.New("Test error")
}
err = serviceTest.service.ProvisionDashboards()
err = serviceTest.service.ProvisionDashboards(context.Background())
assert.NotNil(t, err)
serviceTest.waitForPollChanges()