2018-04-27 06:41:58 -05:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-07-01 09:01:43 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
2018-04-27 06:41:58 -05:00
|
|
|
)
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
// BackgroundServiceRegistry provides background services.
|
|
|
|
type BackgroundServiceRegistry interface {
|
|
|
|
GetServices() []BackgroundService
|
2018-04-27 06:41:58 -05:00
|
|
|
}
|
|
|
|
|
2018-06-29 04:45:32 -05:00
|
|
|
// CanBeDisabled allows the services to decide if it should
|
|
|
|
// be started or not by itself. This is useful for services
|
|
|
|
// that might not always be started, ex alerting.
|
|
|
|
// This will be called after `Init()`.
|
2018-04-27 06:41:58 -05:00
|
|
|
type CanBeDisabled interface {
|
2018-06-29 04:45:32 -05:00
|
|
|
// IsDisabled should return a bool saying if it can be started or not.
|
2018-04-27 06:41:58 -05:00
|
|
|
IsDisabled() bool
|
|
|
|
}
|
|
|
|
|
2018-06-29 04:58:54 -05:00
|
|
|
// BackgroundService should be implemented for services that have
|
|
|
|
// long running tasks in the background.
|
2018-04-27 06:41:58 -05:00
|
|
|
type BackgroundService interface {
|
2018-06-29 04:58:54 -05:00
|
|
|
// Run starts the background process of the service after `Init` have been called
|
|
|
|
// on all services. The `context.Context` passed into the function should be used
|
|
|
|
// to subscribe to ctx.Done() so the service can be notified when Grafana shuts down.
|
|
|
|
Run(ctx context.Context) error
|
2018-05-18 04:10:10 -05:00
|
|
|
}
|
|
|
|
|
2022-04-28 04:06:49 -05:00
|
|
|
// UsageStatsProvidersRegistry provides services sharing their usage stats
|
|
|
|
type UsageStatsProvidersRegistry interface {
|
|
|
|
GetServices() []ProvidesUsageStats
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProvidesUsageStats is an interface for services that share their usage stats
|
|
|
|
type ProvidesUsageStats interface {
|
|
|
|
// GetUsageStats is called on a schedule by the UsageStatsService
|
|
|
|
// Any errors occurring during usage stats collection should be collected and logged within the provider.
|
|
|
|
GetUsageStats(ctx context.Context) map[string]interface{}
|
|
|
|
}
|
|
|
|
|
2018-07-01 09:01:43 -05:00
|
|
|
// DatabaseMigrator allows the caller to add migrations to
|
|
|
|
// the migrator passed as argument
|
|
|
|
type DatabaseMigrator interface {
|
|
|
|
// AddMigrations allows the service to add migrations to
|
|
|
|
// the database migrator.
|
|
|
|
AddMigration(mg *migrator.Migrator)
|
|
|
|
}
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
// IsDisabled returns whether a background service is disabled.
|
|
|
|
func IsDisabled(srv BackgroundService) bool {
|
2018-04-27 06:41:58 -05:00
|
|
|
canBeDisabled, ok := srv.(CanBeDisabled)
|
|
|
|
return ok && canBeDisabled.IsDisabled()
|
|
|
|
}
|