application lifecycle event support

This commit is contained in:
Marcus Efraimsson
2018-10-26 13:57:31 +02:00
parent cfb061ddab
commit 9edaa3fa8c
5 changed files with 82 additions and 12 deletions

View File

@@ -15,13 +15,17 @@ import (
"github.com/grafana/grafana/pkg/api" "github.com/grafana/grafana/pkg/api"
"github.com/grafana/grafana/pkg/api/routing" "github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/lifecycle"
"github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/registry"
"golang.org/x/sync/errgroup"
"github.com/grafana/grafana/pkg/api"
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/login"
"github.com/grafana/grafana/pkg/services/cache" "github.com/grafana/grafana/pkg/services/cache"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/social"
// self registering services // self registering services
_ "github.com/grafana/grafana/pkg/extensions" _ "github.com/grafana/grafana/pkg/extensions"
_ "github.com/grafana/grafana/pkg/metrics" _ "github.com/grafana/grafana/pkg/metrics"
@@ -35,8 +39,7 @@ import (
_ "github.com/grafana/grafana/pkg/services/rendering" _ "github.com/grafana/grafana/pkg/services/rendering"
_ "github.com/grafana/grafana/pkg/services/search" _ "github.com/grafana/grafana/pkg/services/search"
_ "github.com/grafana/grafana/pkg/services/sqlstore" _ "github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting" // self registering services
"github.com/grafana/grafana/pkg/social" // self registering services
_ "github.com/grafana/grafana/pkg/tracing" _ "github.com/grafana/grafana/pkg/tracing"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
@@ -71,8 +74,7 @@ func (g *GrafanaServerImpl) Run() error {
g.loadConfiguration() g.loadConfiguration()
g.writePIDFile() g.writePIDFile()
login.Init() lifecycle.Notify(lifecycle.ApplicationStarting)
social.NewOAuthService()
serviceGraph := inject.Graph{} serviceGraph := inject.Graph{}
serviceGraph.Provide(&inject.Object{Value: bus.GetBus()}) serviceGraph.Provide(&inject.Object{Value: bus.GetBus()})
@@ -145,7 +147,7 @@ func (g *GrafanaServerImpl) Run() error {
} }
sendSystemdNotification("READY=1") sendSystemdNotification("READY=1")
lifecycle.Notify(lifecycle.ApplicationStarted)
return g.childRoutines.Wait() return g.childRoutines.Wait()
} }

View File

@@ -0,0 +1,22 @@
package lifecycle
type Event int
const (
ApplicationStarting Event = iota
ApplicationStarted
)
type EventHandlerFunc func()
var listeners = map[int][]EventHandlerFunc{}
func AddListener(evt Event, fn EventHandlerFunc) {
listeners[int(evt)] = append(listeners[int(evt)], fn)
}
func Notify(evt Event) {
for _, handler := range listeners[int(evt)] {
handler()
}
}

View File

@@ -0,0 +1,35 @@
package lifecycle
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestLifecycle(t *testing.T) {
Convey("TestLifecycle", t, func() {
Convey("Given listeners", func() {
applicationStartingCounter := 0
AddListener(ApplicationStarting, func() {
applicationStartingCounter++
})
applicationStartedCounter := 0
AddListener(ApplicationStarted, func() {
applicationStartedCounter++
})
Convey("When notify application starting should call listener", func() {
Notify(ApplicationStarting)
So(applicationStartingCounter, ShouldEqual, 1)
So(applicationStartedCounter, ShouldEqual, 0)
})
Convey("When notify application started should call listener", func() {
Notify(ApplicationStarted)
So(applicationStartingCounter, ShouldEqual, 0)
So(applicationStartedCounter, ShouldEqual, 1)
})
})
})
}

View File

@@ -2,7 +2,9 @@ package login
import ( import (
"errors" "errors"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/lifecycle"
m "github.com/grafana/grafana/pkg/models" m "github.com/grafana/grafana/pkg/models"
) )
@@ -18,9 +20,11 @@ var (
ErrGettingUserQuota = errors.New("Error getting user quota") ErrGettingUserQuota = errors.New("Error getting user quota")
) )
func Init() { func init() {
lifecycle.AddListener(lifecycle.ApplicationStarting, func() {
bus.AddHandler("auth", AuthenticateUser) bus.AddHandler("auth", AuthenticateUser)
loadLdapConfig() loadLdapConfig()
})
} }
func AuthenticateUser(query *m.LoginUserQuery) error { func AuthenticateUser(query *m.LoginUserQuery) error {

View File

@@ -8,11 +8,18 @@ import (
"golang.org/x/oauth2" "golang.org/x/oauth2"
"github.com/grafana/grafana/pkg/lifecycle"
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/util"
) )
func init() {
lifecycle.AddListener(lifecycle.ApplicationStarting, func() {
initOAuthService()
})
}
type BasicUserInfo struct { type BasicUserInfo struct {
Id string Id string
Name string Name string
@@ -56,7 +63,7 @@ var (
allOauthes = []string{"github", "gitlab", "google", "generic_oauth", "grafananet", grafanaCom} allOauthes = []string{"github", "gitlab", "google", "generic_oauth", "grafananet", grafanaCom}
) )
func NewOAuthService() { func initOAuthService() {
setting.OAuthService = &setting.OAuther{} setting.OAuthService = &setting.OAuther{}
setting.OAuthService.OAuthInfos = make(map[string]*setting.OAuthInfo) setting.OAuthService.OAuthInfos = make(map[string]*setting.OAuthInfo)