forked from convox/rack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotify.go
67 lines (52 loc) · 1.38 KB
/
notify.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package models
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/convox/rack/client"
"github.com/ddollar/logger"
)
// NotificationTopic is the SNS topic for notifications
var NotificationTopic = os.Getenv("NOTIFICATION_TOPIC")
// PauseNotifications turns off notifications globally
var PauseNotifications = false
// uniform error handling
func NotifyError(action string, err error, data map[string]string) error {
data["message"] = err.Error()
return Notify(action, "error", data)
}
func NotifySuccess(action string, data map[string]string) error {
return Notify(action, "success", data)
}
func Notify(name, status string, data map[string]string) error {
if PauseNotifications {
return nil
}
log := logger.New("ns=kernel")
data["rack"] = os.Getenv("RACK")
event := &client.NotifyEvent{
Action: name,
Status: status,
Data: data,
Timestamp: time.Now().UTC(),
}
message, err := json.Marshal(event)
if err != nil {
return err
}
fmt.Printf("models EventSend msg=%q\n", message)
params := &sns.PublishInput{
Message: aws.String(string(message)), // Required
Subject: aws.String(name),
TargetArn: aws.String(NotificationTopic),
}
resp, err := SNS().Publish(params)
if err != nil {
return err
}
log.At("Notify").Log("message-id=%q", *resp.MessageId)
return nil
}