Merge branch 'notifications'
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
package eventpublisher
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/streadway/amqp"
|
||||
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||
"github.com/torkelo/grafana-pro/pkg/events"
|
||||
"github.com/torkelo/grafana-pro/pkg/setting"
|
||||
)
|
||||
|
||||
var (
|
||||
url string
|
||||
exchange string
|
||||
conn *amqp.Connection
|
||||
channel *amqp.Channel
|
||||
)
|
||||
|
||||
func getConnection() (*amqp.Connection, error) {
|
||||
c, err := amqp.Dial(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c, err
|
||||
}
|
||||
|
||||
func getChannel() (*amqp.Channel, error) {
|
||||
ch, err := conn.Channel()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = ch.ExchangeDeclare(
|
||||
exchange, // name
|
||||
"topic", // type
|
||||
true, // durable
|
||||
false, // auto-deleted
|
||||
false, // internal
|
||||
false, // no-wait
|
||||
nil, // arguments
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ch, err
|
||||
}
|
||||
|
||||
func Init() {
|
||||
sec := setting.Cfg.Section("event_publisher")
|
||||
|
||||
if !sec.Key("enabled").MustBool(false) {
|
||||
return
|
||||
}
|
||||
|
||||
url = sec.Key("rabbitmq_url").String()
|
||||
exchange = sec.Key("exchange").String()
|
||||
bus.AddWildcardListener(eventListener)
|
||||
|
||||
if err := Setup(); err != nil {
|
||||
log.Fatal(4, "Failed to connect to notification queue: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Every connection should declare the topology they expect
|
||||
func Setup() error {
|
||||
c, err := getConnection()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conn = c
|
||||
ch, err := getChannel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
channel = ch
|
||||
|
||||
// listen for close events so we can reconnect.
|
||||
errChan := channel.NotifyClose(make(chan *amqp.Error))
|
||||
go func() {
|
||||
for e := range errChan {
|
||||
fmt.Println("connection to rabbitmq lost.")
|
||||
fmt.Println(e)
|
||||
fmt.Println("attempting to create new rabbitmq channel.")
|
||||
ch, err := getChannel()
|
||||
if err == nil {
|
||||
channel = ch
|
||||
break
|
||||
}
|
||||
|
||||
//could not create channel, so lets close the connection
|
||||
// and re-create.
|
||||
_ = conn.Close()
|
||||
|
||||
for err != nil {
|
||||
time.Sleep(2 * time.Second)
|
||||
fmt.Println("attempting to reconnect to rabbitmq.")
|
||||
err = Setup()
|
||||
}
|
||||
fmt.Println("Connected to rabbitmq again.")
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func publish(routingKey string, msgString []byte) {
|
||||
err := channel.Publish(
|
||||
exchange, //exchange
|
||||
routingKey, // routing key
|
||||
false, // mandatory
|
||||
false, // immediate
|
||||
amqp.Publishing{
|
||||
ContentType: "application/json",
|
||||
Body: msgString,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
// failures are most likely because the connection was lost.
|
||||
// the connection will be re-established, so just keep
|
||||
// retrying every 2seconds until we successfully publish.
|
||||
time.Sleep(2 * time.Second)
|
||||
fmt.Println("publish failed, retrying.")
|
||||
publish(routingKey, msgString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func eventListener(event interface{}) error {
|
||||
wireEvent, err := events.ToOnWriteEvent(event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
msgString, err := json.Marshal(wireEvent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
routingKey := fmt.Sprintf("%s.%s", wireEvent.Priority, wireEvent.EventType)
|
||||
// this is run in a greenthread and we expect that publish will keep
|
||||
// retrying until the message gets sent.
|
||||
go publish(routingKey, msgString)
|
||||
return nil
|
||||
}
|
||||
@@ -3,9 +3,8 @@ package sqlstore
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
|
||||
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||
"github.com/torkelo/grafana-pro/pkg/events"
|
||||
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||
)
|
||||
|
||||
@@ -48,7 +47,7 @@ func GetAccountByName(query *m.GetAccountByNameQuery) error {
|
||||
}
|
||||
|
||||
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
return inTransaction2(func(sess *session) error {
|
||||
|
||||
account := m.Account{
|
||||
Name: cmd.Name,
|
||||
@@ -60,7 +59,6 @@ func CreateAccount(cmd *m.CreateAccountCommand) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// create inital admin account user
|
||||
user := m.AccountUser{
|
||||
AccountId: account.Id,
|
||||
UserId: cmd.UserId,
|
||||
@@ -72,19 +70,34 @@ func CreateAccount(cmd *m.CreateAccountCommand) error {
|
||||
_, err := sess.Insert(&user)
|
||||
cmd.Result = account
|
||||
|
||||
sess.publishAfterCommit(&events.AccountCreated{
|
||||
Timestamp: account.Created,
|
||||
Id: account.Id,
|
||||
Name: account.Name,
|
||||
})
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateAccount(cmd *m.UpdateAccountCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
return inTransaction2(func(sess *session) error {
|
||||
|
||||
account := m.Account{
|
||||
Name: cmd.Name,
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
_, err := sess.Id(cmd.AccountId).Update(&account)
|
||||
return err
|
||||
if _, err := sess.Id(cmd.AccountId).Update(&account); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sess.publishAfterCommit(&events.AccountUpdated{
|
||||
Timestamp: account.Updated,
|
||||
Id: account.Id,
|
||||
Name: account.Name,
|
||||
})
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||
"github.com/torkelo/grafana-pro/pkg/log"
|
||||
)
|
||||
|
||||
type dbTransactionFunc func(sess *xorm.Session) error
|
||||
type dbTransactionFunc2 func(sess *session) error
|
||||
|
||||
type session struct {
|
||||
*xorm.Session
|
||||
events []interface{}
|
||||
}
|
||||
|
||||
func (sess *session) publishAfterCommit(msg interface{}) {
|
||||
sess.events = append(sess.events, msg)
|
||||
}
|
||||
|
||||
func inTransaction(callback dbTransactionFunc) error {
|
||||
var err error
|
||||
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
|
||||
if err = sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = callback(sess)
|
||||
|
||||
if err != nil {
|
||||
sess.Rollback()
|
||||
return err
|
||||
} else if err = sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func inTransaction2(callback dbTransactionFunc2) error {
|
||||
var err error
|
||||
|
||||
sess := session{Session: x.NewSession()}
|
||||
|
||||
defer sess.Close()
|
||||
if err = sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = callback(&sess)
|
||||
|
||||
if err != nil {
|
||||
sess.Rollback()
|
||||
return err
|
||||
} else if err = sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(sess.events) > 0 {
|
||||
for _, e := range sess.events {
|
||||
if err = bus.Publish(e); err != nil {
|
||||
log.Error(3, "Failed to publish event after commit", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -43,12 +43,13 @@ func EnsureAdminUser() {
|
||||
cmd.IsAdmin = true
|
||||
|
||||
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)
|
||||
} 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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,27 +150,3 @@ func LoadConfig() {
|
||||
DbCfg.SslMode = sec.Key("ssl_mode").String()
|
||||
DbCfg.Path = sec.Key("path").MustString("data/grafana.db")
|
||||
}
|
||||
|
||||
type dbTransactionFunc func(sess *xorm.Session) error
|
||||
|
||||
func inTransaction(callback dbTransactionFunc) error {
|
||||
var err error
|
||||
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
|
||||
if err = sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = callback(sess)
|
||||
|
||||
if err != nil {
|
||||
sess.Rollback()
|
||||
return err
|
||||
} else if err = sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/go-xorm/xorm"
|
||||
|
||||
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||
"github.com/torkelo/grafana-pro/pkg/events"
|
||||
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||
"github.com/torkelo/grafana-pro/pkg/setting"
|
||||
"github.com/torkelo/grafana-pro/pkg/util"
|
||||
@@ -23,7 +24,7 @@ func init() {
|
||||
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
|
||||
|
||||
if setting.SingleAccountMode {
|
||||
@@ -51,7 +52,7 @@ func getAccountIdForNewUser(userEmail string, sess *xorm.Session) (int64, 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)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -94,10 +95,20 @@ func CreateUser(cmd *m.CreateUserCommand) error {
|
||||
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
|
||||
return err
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -127,7 +138,7 @@ func GetUserByLogin(query *m.GetUserByLoginQuery) error {
|
||||
}
|
||||
|
||||
func UpdateUser(cmd *m.UpdateUserCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
return inTransaction2(func(sess *session) error {
|
||||
|
||||
user := m.User{
|
||||
Name: cmd.Name,
|
||||
@@ -136,8 +147,19 @@ func UpdateUser(cmd *m.UpdateUserCommand) error {
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
_, err := sess.Id(cmd.UserId).Update(&user)
|
||||
return err
|
||||
if _, err := sess.Id(cmd.UserId).Update(&user); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sess.publishAfterCommit(&events.UserUpdated{
|
||||
Timestamp: user.Created,
|
||||
Id: user.Id,
|
||||
Name: user.Name,
|
||||
Login: user.Login,
|
||||
Email: user.Email,
|
||||
})
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user