Commit d8db5189 by Torkel Ödegaard

More work on events, still have to convert pascal case event type name to…

More work on events, still have to convert pascal case event type name to rabbitmq dot notation, but after that should be done
parent 525179eb
...@@ -85,7 +85,6 @@ func runWeb(c *cli.Context) { ...@@ -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)
......
...@@ -61,3 +61,19 @@ type AccountUpdated struct { ...@@ -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"`
}
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"`
}
...@@ -92,7 +92,7 @@ func UpdateAccount(cmd *m.UpdateAccountCommand) error { ...@@ -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,
......
...@@ -43,12 +43,13 @@ func EnsureAdminUser() { ...@@ -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)
} }
} }
......
...@@ -7,6 +7,7 @@ import ( ...@@ -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() { ...@@ -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) ...@@ -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 { ...@@ -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
}
cmd.Result = user sess.publishAfterCommit(&events.UserCreated{
_ = bus.Publish(&m.Notification{
EventType: "user.create",
Timestamp: user.Created, Timestamp: user.Created,
Priority: m.PRIO_INFO, Id: user.Id,
Payload: user, Name: user.Name,
Login: user.Login,
Email: user.Email,
}) })
return err
cmd.Result = user
return nil
}) })
} }
...@@ -131,7 +136,7 @@ func GetUserByLogin(query *m.GetUserByLoginQuery) error { ...@@ -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 { ...@@ -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 {
// 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 return err
}
sess.publishAfterCommit(&events.UserUpdated{
Timestamp: user.Created,
Id: user.Id,
Name: user.Name,
Login: user.Login,
Email: user.Email,
})
return nil
}) })
} }
......
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