Commit 629eab0b by bergquist

bus: dont mix ctx/classic handlers

parent e2275701
......@@ -103,7 +103,7 @@ func GetDashboard(c *m.ReqContext) Response {
}
isDashboardProvisioned := &m.IsDashboardProvisionedQuery{DashboardId: dash.Id}
err = bus.DispatchCtx(c.Req.Context(), isDashboardProvisioned)
err = bus.Dispatch(isDashboardProvisioned)
if err != nil {
return Error(500, "Error while checking if dashboard is provisioned", err)
}
......
......@@ -76,25 +76,13 @@ func (b *InProcBus) SetTransactionManager(tm TransactionManager) {
func (b *InProcBus) DispatchCtx(ctx context.Context, msg Msg) error {
var msgName = reflect.TypeOf(msg).Elem().Name()
// we prefer to use the handler that support context.Context
var handler = b.handlersWithCtx[msgName]
var withCtx = true
// fallback to use classic handlers
if handler == nil {
withCtx = false
handler = b.handlers[msgName]
}
if handler == nil {
return ErrHandlerNotFound
}
var params = []reflect.Value{}
if withCtx {
params = append(params, reflect.ValueOf(ctx))
}
params = append(params, reflect.ValueOf(ctx))
params = append(params, reflect.ValueOf(msg))
ret := reflect.ValueOf(handler).Call(params)
......
......@@ -33,22 +33,23 @@ func TestDispatchCtxCanUseNormalHandlers(t *testing.T) {
t.Errorf("expected bus to return HandlerNotFound is no handler is registered")
}
bus.AddHandler(handler)
bus.AddHandlerCtx(handlerWithCtx)
t.Run("when a normal handler is registered", func(t *testing.T) {
bus.AddHandler(handler)
bus.DispatchCtx(context.Background(), &testQuery{})
bus.Dispatch(&testQuery{})
if handlerCallCount != 1 {
t.Errorf("Expected normal handler to be called once")
t.Errorf("Expected normal handler to be called 1 time. was called %d", handlerCallCount)
}
})
t.Run("when a ctx handler is registered", func(t *testing.T) {
bus.AddHandlerCtx(handlerWithCtx)
bus.DispatchCtx(context.Background(), &testQuery{})
t.Run("when a ctx handler is registered", func(t *testing.T) {
bus.DispatchCtx(context.Background(), &testQuery{})
if handlerWithCtxCallCount != 1 {
t.Errorf("Expected ctx handler to be called once")
}
})
if handlerWithCtxCallCount != 1 {
t.Errorf("Expected ctx handler to be called 1 time. was called %d", handlerWithCtxCallCount)
}
})
}
......
......@@ -13,8 +13,7 @@ func init() {
bus.AddHandler("sql", GetDataSourceStats)
bus.AddHandler("sql", GetDataSourceAccessStats)
bus.AddHandler("sql", GetAdminStats)
bus.AddHandler("sql", GetSystemUserCountStats)
bus.AddHandlerCtx("sql", GetSystemUserCountStatsCtx)
bus.AddHandlerCtx("sql", GetSystemUserCountStats)
}
var activeUserTimeLimit = time.Hour * 24 * 30
......@@ -135,7 +134,7 @@ func GetAdminStats(query *m.GetAdminStatsQuery) error {
return err
}
func GetSystemUserCountStatsCtx(ctx context.Context, query *m.GetSystemUserCountStatsQuery) error {
func GetSystemUserCountStats(ctx context.Context, query *m.GetSystemUserCountStatsQuery) error {
return withDbSession(ctx, func(sess *DBSession) error {
var rawSql = `SELECT COUNT(id) AS Count FROM ` + dialect.Quote("user")
......@@ -150,16 +149,3 @@ func GetSystemUserCountStatsCtx(ctx context.Context, query *m.GetSystemUserCount
return err
})
}
func GetSystemUserCountStats(query *m.GetSystemUserCountStatsQuery) error {
var rawSql = `SELECT COUNT(id) AS Count FROM ` + dialect.Quote("user")
var stats m.SystemUserCountStats
_, err := x.SQL(rawSql).Get(&stats)
if err != nil {
return err
}
query.Result = &stats
return err
}
package sqlstore
import (
"context"
"testing"
m "github.com/grafana/grafana/pkg/models"
......@@ -20,7 +21,7 @@ func TestStatsDataAccess(t *testing.T) {
Convey("Get system user count stats should not results in error", func() {
query := m.GetSystemUserCountStatsQuery{}
err := GetSystemUserCountStats(&query)
err := GetSystemUserCountStats(context.Background(), &query)
So(err, ShouldBeNil)
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment