-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
executable file
·83 lines (71 loc) · 2.45 KB
/
main.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 main
import (
"flag"
"os"
"strconv"
"github.com/malike/go-kafka-alert/service"
"github.com/malike/go-kafka-alert/db"
"github.com/malike/go-kafka-alert/config"
)
var profile = ""
var configServer = "http://localhost:8888"
func main() {
logLevel := flag.String("loglevel", "error", "Possible options warn,trace,error,info")
name := flag.String("name", "go-kafka-alert", "Application name")
profileTerminal := flag.String("profile", "", "Configuration profile URL")
configServerTerminal := flag.String("config", "http://localhost:8888", "Config server base url")
flag.Parse()
if profileTerminal != nil {
profile = *profileTerminal
}
if configServerTerminal != nil {
configServer = *configServerTerminal
}
config.LogLevel = *logLevel
config.ApplicationName = *name
config.ConfigProfile = profile
config.ConfigServer = configServer
_, configErr := config.LoadConfiguration()
if configErr != nil {
config.Error.Println("Error loading config. Shutting down ")
os.Exit(1)
}
db.DialDB()
config.Trace.Println("Starting up Service with Log level '" + *logLevel + "'")
config.Trace.Println("Configuration file loaded successfully with '" +
strconv.Itoa(len(config.AppConfiguration.Templates)) + "' templates and " +
strconv.Itoa(config.AppConfiguration.Workers) + " workers processing events")
service.NewKafkaConsumer()
if service.KafkaConsumer == nil {
config.Error.Println("Error starting Kafka Consumer ")
os.Exit(1)
}
run := true
for run {
events, _ := service.GetEventFromKafkaStream()
if len(events) > 0 {
if len(events) <= config.AppConfiguration.Workers {
config.Info.Println("Distributing " + strconv.Itoa(len(events)) + " worker of the month")
go service.EventProcessorForChannel(events)
} else {
batchSize := len(events) / config.AppConfiguration.Workers
config.Info.Println("Distributing '" + strconv.Itoa(len(events)) + "' events for '" +
strconv.Itoa(config.AppConfiguration.Workers) +
"' workers '" + strconv.Itoa(batchSize) + "' each.")
//..else share
currentPointer := 0
var eventBatch []db.Event
for i := 1; i <= config.AppConfiguration.Workers; i++ {
//slice events ..using batchSize
if i == config.AppConfiguration.Workers {
eventBatch = events[currentPointer:]
} else {
eventBatch = events[currentPointer:batchSize]
}
go service.EventProcessorForChannel(eventBatch)
currentPointer = currentPointer + batchSize + 1
}
}
}
}
}