-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
83 lines (73 loc) · 1.71 KB
/
event.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package event
import (
"crypto/rand"
"encoding/json"
"os"
"strconv"
"time"
log "github.com/cihub/seelog"
"github.com/HailoOSS/platform/util"
"github.com/HailoOSS/service/nsq"
gouuid "github.com/nu7hatch/gouuid"
)
const (
CreateRule = "CREATED"
DeleteRule = "DELETED"
nsqTopic = "platform.events"
)
var (
hostname string
azName string
)
func init() {
var err error
if hostname, err = os.Hostname(); err != nil {
hostname = "localhost.unknown"
}
if azName, err = util.GetAwsAZName(); err != nil {
azName = "unknown"
}
}
// generatePseudoRand is used in the rare event of proper uuid generation failing
func generatePseudoRand() string {
alphanum := "0123456789abcdefghigklmnopqrst"
var bytes = make([]byte, 10)
rand.Read(bytes)
for i, b := range bytes {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
return string(bytes)
}
func PubRuleChange(service, version, action, user string, weight int32) {
var uuid string
u4, err := gouuid.NewV4()
if err != nil {
uuid = generatePseudoRand()
} else {
uuid = u4.String()
}
event := map[string]interface{}{
"id": uuid,
"timestamp": strconv.Itoa(int(time.Now().Unix())),
"type": "com.HailoOSS.kernel.binding.event",
"details": map[string]string{
"ServiceName": service,
"ServiceVersion": version,
"AzName": azName,
"Hostname": hostname,
"Action": action,
"Weight": strconv.Itoa(int(weight)),
"UserId": user,
},
}
bytes, err := json.Marshal(event)
if err != nil {
log.Errorf("Error marshaling nsq event message for %v:%v", event, err)
return
}
err = nsq.Publish(nsqTopic, bytes)
if err != nil {
log.Errorf("Error publishing message to NSQ: %v", err)
return
}
}