-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
139 lines (117 loc) · 3.21 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
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
package main
import (
"encoding/json"
"flag"
"github.com/getsentry/raven-go"
"github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
"os/signal"
"sync"
)
var (
logger = logrus.New()
userIdentifier string
// command line parameters
serverAddress *string
maxQueueSize *int
metricsIntervalSeconds *int
// build-time variables
ravenDSN string
gitRevision string
enableMetrics string
enableGlobalReport string
defaultServer string
lang string
)
func init() {
// logrus formatter
customFormatter := new(logrus.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
customFormatter.FullTimestamp = true
logger.SetFormatter(customFormatter)
logger.SetLevel(logrus.DebugLevel)
// raven
err := raven.SetDSN(ravenDSN)
if err != nil {
logger.Warnf("Set DSN failed: %s", err.Error())
}
// user identifier
userIdentifier = getUserIdentifier()
}
// getUserIdentifier reads lemon_seed from current directory, if no file exists, generate one.
func getUserIdentifier() string {
var config Configuration
if _, err := os.Stat("./lemon_seed"); os.IsNotExist(err) {
// not exist, generate one
config.ClientID = uuid.NewV4().String()
configBytes, err := json.Marshal(config)
if err != nil {
logger.Error("Marshal error when generating configBytes: %s", err.Error())
}
err = ioutil.WriteFile("./lemon_seed", configBytes, 0666)
if err != nil {
logger.Error("Error when write config to seed: %s", err.Error())
}
return config.ClientID
} else {
// lemon_config exist
body, err := ioutil.ReadFile("./lemon_seed")
if err != nil {
logger.Fatal("Read exception.")
}
err = json.Unmarshal(body, &config)
if err != nil {
logger.Fatalf("Unmarshal error when reading seed: %s", err.Error())
}
return config.ClientID
}
}
func main() {
var stopChannel = make(chan struct{})
var wg sync.WaitGroup
serverAddress = flag.String("server", defaultServer, "Address of server(must start with scheme)")
maxQueueSize = flag.Int("queue-size", 10, "Max queue size")
metricsIntervalSeconds = flag.Int("metrics-interval", 30, "Metrics interval")
flag.Parse()
logger.WithFields(logrus.Fields{
"server": *serverAddress,
"user": userIdentifier,
"queueSize": *maxQueueSize}).Infof(currentLangBundle.Starting, gitRevision)
taskChannel := make(chan Task)
// task fetching goroutine
go func(stop <-chan struct{}) {
defer wg.Done()
wg.Add(1)
fetchTask(taskChannel, stop)
}(stopChannel)
// task consuming goroutine
go func(stop <-chan struct{}) {
defer wg.Done()
wg.Add(1)
consume(taskChannel, stop)
}(stopChannel)
// metrics goroutine
if enableMetrics == "true" {
go func(stop <-chan struct{}) {
defer wg.Done()
wg.Add(1)
metricsFlusher(stop)
}(stopChannel)
}
// global report goroutine
if enableGlobalReport == "true" {
go globalReport()
}
// handle signal
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt, os.Kill)
<-signalChannel // block until receive quit signal
logger.Info(currentLangBundle.Exiting)
// notify each goroutine to exit
close(stopChannel)
// wait until all goroutine exit
wg.Wait()
logger.Info(currentLangBundle.Exited)
}