forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier.go
51 lines (44 loc) · 1.31 KB
/
notifier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"encoding/json"
)
type NotificationCommand string
const (
NoticeApiUpdated NotificationCommand = "ApiUpdated"
NoticeApiRemoved NotificationCommand = "ApiRemoved"
NoticeApiAdded NotificationCommand = "ApiAdded"
NoticeGroupReload NotificationCommand = "GroupReload"
NoticePolicyChanged NotificationCommand = "PolicyChanged"
)
// Notification is a type that encodes a message published to a pub sub channel
type Notification struct {
Command NotificationCommand `json:"command"`
Payload string `json:"payload"`
}
// Notifier is an interface that sends notifications
type Notifier interface {
Notify(string) bool
}
// RedisNotifier implements Notifier and will use redis pub/sub channles to send notifications
type RedisNotifier struct {
store *RedisClusterStorageManager
channel string
}
// Notify will send a notification to a channel
func (r *RedisNotifier) Notify(notification Notification) bool {
toSend, err := json.Marshal(notification)
if err != nil {
log.Error("Problem marshalling notification!")
log.Error(err)
return false
} else {
log.Debug("Sending notification", notification)
sentErr := r.store.Publish(r.channel, string(toSend))
if sentErr != nil {
log.Error("Could not send notification")
log.Error(err)
return false
}
}
return true
}