More work on events, still have to convert pascal case event type name to rabbitmq dot notation, but after that should be done

This commit is contained in:
Torkel Ödegaard
2015-02-04 17:15:05 +01:00
parent 525179eb85
commit d8db5189c1
6 changed files with 48 additions and 47 deletions
-1
View File
@@ -85,7 +85,6 @@ func runWeb(c *cli.Context) {
eventpublisher.Init() eventpublisher.Init()
var err error var err error
m := newMacaron() m := newMacaron()
api.Register(m) api.Register(m)
+16
View File
@@ -61,3 +61,19 @@ type AccountUpdated struct {
Id int64 `json:"id"` Id int64 `json:"id"`
Name string `json:"name"` Name string `json:"name"`
} }
type UserCreated struct {
Timestamp time.Time `json:"timestamp"`
Id int64 `json:"id"`
Name string `json:"name"`
Login string `json:"login"`
Email string `json:"email"`
}
type UserUpdated struct {
Timestamp time.Time `json:"timestamp"`
Id int64 `json:"id"`
Name string `json:"name"`
Login string `json:"login"`
Email string `json:"email"`
}
-21
View File
@@ -1,21 +0,0 @@
package models
import (
"time"
)
type EventPriority string
const (
PRIO_DEBUG EventPriority = "DEBUG"
PRIO_INFO EventPriority = "INFO"
PRIO_ERROR EventPriority = "ERROR"
)
type Notification struct {
EventType string `json:"event_type"`
Timestamp time.Time `json:"timestamp"`
Priority EventPriority `json:"priority"`
Payload interface{} `json:"payload"`
}
+1 -1
View File
@@ -92,7 +92,7 @@ func UpdateAccount(cmd *m.UpdateAccountCommand) error {
return err return err
} }
sess.publishAfterCommit(events.AccountUpdated{ sess.publishAfterCommit(&events.AccountUpdated{
Timestamp: account.Updated, Timestamp: account.Updated,
Id: account.Id, Id: account.Id,
Name: account.Name, Name: account.Name,
+3 -2
View File
@@ -43,12 +43,13 @@ func EnsureAdminUser() {
cmd.IsAdmin = true cmd.IsAdmin = true
if err = bus.Dispatch(&cmd); err != nil { if err = bus.Dispatch(&cmd); err != nil {
log.Fatal(3, "Failed to create default admin user", err) log.Error(3, "Failed to create default admin user", err)
return
} }
log.Info("Created default admin user: %v", setting.AdminUser) log.Info("Created default admin user: %v", setting.AdminUser)
} else if err != nil { } else if err != nil {
log.Fatal(3, "Could not determine if admin user exists: %v", err) log.Error(3, "Could not determine if admin user exists: %v", err)
} }
} }
+28 -22
View File
@@ -7,6 +7,7 @@ import (
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
"github.com/torkelo/grafana-pro/pkg/bus" "github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/events"
m "github.com/torkelo/grafana-pro/pkg/models" m "github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/setting" "github.com/torkelo/grafana-pro/pkg/setting"
"github.com/torkelo/grafana-pro/pkg/util" "github.com/torkelo/grafana-pro/pkg/util"
@@ -23,7 +24,7 @@ func init() {
bus.AddHandler("sql", GetUserAccounts) bus.AddHandler("sql", GetUserAccounts)
} }
func getAccountIdForNewUser(userEmail string, sess *xorm.Session) (int64, error) { func getAccountIdForNewUser(userEmail string, sess *session) (int64, error) {
var account m.Account var account m.Account
if setting.SingleAccountMode { if setting.SingleAccountMode {
@@ -51,7 +52,7 @@ func getAccountIdForNewUser(userEmail string, sess *xorm.Session) (int64, error)
} }
func CreateUser(cmd *m.CreateUserCommand) error { func CreateUser(cmd *m.CreateUserCommand) error {
return inTransaction(func(sess *xorm.Session) error { return inTransaction2(func(sess *session) error {
accountId, err := getAccountIdForNewUser(cmd.Email, sess) accountId, err := getAccountIdForNewUser(cmd.Email, sess)
if err != nil { if err != nil {
return err return err
@@ -92,16 +93,20 @@ func CreateUser(cmd *m.CreateUserCommand) error {
accountUser.Role = m.RoleType(setting.DefaultAccountRole) accountUser.Role = m.RoleType(setting.DefaultAccountRole)
} }
_, err = sess.Insert(&accountUser) if _, err = sess.Insert(&accountUser); err != nil {
return err
}
sess.publishAfterCommit(&events.UserCreated{
Timestamp: user.Created,
Id: user.Id,
Name: user.Name,
Login: user.Login,
Email: user.Email,
})
cmd.Result = user cmd.Result = user
_ = bus.Publish(&m.Notification{ return nil
EventType: "user.create",
Timestamp: user.Created,
Priority: m.PRIO_INFO,
Payload: user,
})
return err
}) })
} }
@@ -131,7 +136,7 @@ func GetUserByLogin(query *m.GetUserByLoginQuery) error {
} }
func UpdateUser(cmd *m.UpdateUserCommand) error { func UpdateUser(cmd *m.UpdateUserCommand) error {
return inTransaction(func(sess *xorm.Session) error { return inTransaction2(func(sess *session) error {
user := m.User{ user := m.User{
Name: cmd.Name, Name: cmd.Name,
@@ -140,18 +145,19 @@ func UpdateUser(cmd *m.UpdateUserCommand) error {
Updated: time.Now(), Updated: time.Now(),
} }
_, err := sess.Id(cmd.UserId).Update(&user) if _, err := sess.Id(cmd.UserId).Update(&user); err != nil {
if err == nil { return err
// silently ignore failures to publish events.
user.Id = cmd.UserId
_ = bus.Publish(&m.Notification{
EventType: "user.update",
Timestamp: user.Updated,
Priority: m.PRIO_INFO,
Payload: user,
})
} }
return err
sess.publishAfterCommit(&events.UserUpdated{
Timestamp: user.Created,
Id: user.Id,
Name: user.Name,
Login: user.Login,
Email: user.Email,
})
return nil
}) })
} }