-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopic.go
More file actions
200 lines (165 loc) · 5.35 KB
/
Copy pathtopic.go
File metadata and controls
200 lines (165 loc) · 5.35 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"fmt"
"math/rand"
"net/http"
"regexp"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/fatih/color"
"github.com/golang/protobuf/proto"
"github.com/larskluge/babl-server/kafka"
. "github.com/larskluge/babl-server/utils"
pbm "github.com/larskluge/babl/protobuf/messages"
"gopkg.in/bsm/sarama-cluster.v2"
)
var regexSup = regexp.MustCompile("^supervisor.*")
var regexText = regexp.MustCompile("^text/plain.*")
func ParseTopic(Topics []string) {
wait_here_forever := make(chan bool)
log.SetLevel(log.DebugLevel)
client := *kafka.NewClient([]string{Broker}, "babl-admin", true)
topics, err := client.Topics()
Check(err)
f := func(topic string) bool { return regexSup.MatchString(topic) }
supTopics := filter(topics, f)
var moduleTopics []string
for _, t := range Topics {
r := regexp.MustCompile(t)
f = func(topic string) bool { return r.MatchString(topic) }
modTopics := filter(topics, f)
moduleTopics = append(moduleTopics, modTopics...)
}
moduleTopics = removeDuplicatesUnordered(moduleTopics)
client.Close()
clientgroup := kafka.NewClientGroup([]string{Broker}, "babl-admin", true)
defer (*clientgroup).Close()
go parseGroup(clientgroup, moduleTopics)
go parseSupervisors(clientgroup, supTopics)
<-wait_here_forever
}
func ConsumeGroup(client *cluster.Client, topics []string, ch chan *kafka.ConsumerData) {
group := "babl-admin_"
r := rand.New(rand.NewSource(time.Now().UnixNano()))
group += fmt.Sprint(r.Uint32())
log.WithFields(log.Fields{"topics": topics, "group": group, "offset": "newest"}).Info("Consuming Groups")
consumer, err := cluster.NewConsumerFromClient(client, group, topics)
Check(err)
defer consumer.Close()
go consumeErrors(consumer)
go consumeNotifications(consumer)
for msg := range consumer.Messages() {
data := kafka.ConsumerData{Topic: msg.Topic, Key: string(msg.Key), Value: msg.Value, Processed: make(chan string, 1)}
ch <- &data //SEND
metadata := <-data.Processed //LISTEN
consumer.MarkOffset(msg, metadata)
}
log.Println("ConsumerGroups: Done consuming topic/groups", topics)
}
func consumeErrors(consumer *cluster.Consumer) {
for err := range consumer.Errors() {
log.WithFields(log.Fields{"error": err.Error()}).Warn("ConsumeGroup: Error")
}
}
func consumeNotifications(consumer *cluster.Consumer) {
for note := range consumer.Notifications() {
log.WithFields(log.Fields{"rebalanced": note}).Info("ConsumeGroup: Notification")
}
}
func parseGroup(clientgroup *cluster.Client, topics []string) {
ch := make(chan *kafka.ConsumerData)
go ConsumeGroup(clientgroup, topics, ch)
var stdin string
for {
data, _ := <-ch //LISTEN
rid := SplitLast(data.Key, ".")
res := "error"
method := SplitLast(data.Topic, ".")
switch method {
case "IO":
in := &pbm.BinRequest{}
err := proto.Unmarshal(data.Value, in)
Check(err)
if regexText.MatchString(http.DetectContentType(in.Stdin)) {
stdin = string(in.Stdin)
} else {
stdin = http.DetectContentType(in.Stdin)
}
logIoData(rid, data.Topic, len(in.Stdin), in.Env, in.PayloadUrl, stdin)
res = "success"
case "Ping":
in := &pbm.Empty{}
err := proto.Unmarshal(data.Value, in)
Check(err)
logPingData(rid, data.Topic)
res = "success"
}
data.Processed <- res //SEND
}
}
func parseSupervisors(clientgroup *cluster.Client, topics []string) {
ch := make(chan *kafka.ConsumerData)
go ConsumeGroup(clientgroup, topics, ch)
for {
data, _ := <-ch //LISTEN
var stdout, stderr string
rid := SplitLast(data.Key, ".")
in := &pbm.BinReply{}
err := proto.Unmarshal(data.Value, in)
Check(err)
if regexText.MatchString(http.DetectContentType(in.Stdout)) {
stdout = string(in.Stdout)
} else {
stdout = http.DetectContentType(in.Stdout)
}
if regexText.MatchString(http.DetectContentType(in.Stderr)) {
stderr = string(in.Stderr)
} else {
stderr = http.DetectContentType(in.Stderr)
}
logSupervisorData(rid, data.Topic, len(in.Stdout), in.Exitcode, in.PayloadUrl, stdout, stderr)
data.Processed <- "success" // SEND
}
}
func envFormatted(env map[string]string) (s string) {
for k, v := range env {
s += fmt.Sprintf(" %s=\"%s\"", k, v)
}
return strings.TrimSpace(s)
}
func logIoData(rid, topic string, size_in int, env map[string]string, payload_url string, stdin string) {
fmt.Printf("-> RID:%-7s%-42s IN__LEN:%-14d ENV: %s\tPAYLOAD_URL:%s IN:%s\n", rid, topic, size_in, envFormatted(env), payload_url, stdin)
}
func logPingData(rid, topic string) {
fmt.Printf("-> RID:%-7s%-42s\t%s\n", rid, topic, "PING")
}
func logSupervisorData(rid, topic string, size_out int, exit_code int32, payload_url string, stdout string, stderr string) {
if exit_code != 0 {
color.Set(color.FgRed)
}
fmt.Printf("<- RID:%-7s%-42s OUT_LEN:%-14d EXIT_CODE: %d\tPAYLOAD_URL:%s\t (OUT:%s,ERR:%s)\n", rid, topic, size_out, exit_code, payload_url, stdout, stderr)
color.Unset()
}
func filter(s []string, fn func(string) bool) []string {
var r []string // == nil
for _, v := range s {
if fn(v) {
r = append(r, v)
}
}
return r
}
func removeDuplicatesUnordered(elements []string) []string {
encountered := map[string]bool{}
// Create a map of all unique elements.
for v := range elements {
encountered[elements[v]] = true
}
// Place all keys from the map into a slice.
result := []string{}
for key, _ := range encountered {
result = append(result, key)
}
return result
}