-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_producer.go
117 lines (93 loc) · 2.32 KB
/
main_producer.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
package main
import (
"context"
"time"
"github.com/jackc/pgx/v4"
"github.com/urfave/cli/v2"
)
func mainProducer(c *cli.Context) error {
conf := newConfig(
c.String("log-level"),
c.Uint("static-cache-ttl"),
c.String("collector-url"),
)
redisStep:
red, redErr := redisGetClient(c.String("redis-uri"))
if redErr != nil {
conf.getLogger().
Error().
Msg(redErr.Error())
time.Sleep(time.Duration(1) * time.Second)
goto redisStep
}
conf.getLogger().
Debug().
Msg("successfully ping to redis")
/**
* projects
*/
managementCallInterval := time.Duration(c.Int64("management-call-interval")) * time.Second
projectsManager := newProjectsManager()
projectsJSON := c.String("management-projects-json")
// it's static from json file
if projectsJSON != "" {
projects, projectsErr := projectsLoadJSON(c.String("management-projects-json"))
if projectsErr != nil {
return projectsErr
}
projectsManagerErr := projectsManager.load(projects)
if projectsManagerErr != nil {
return projectsManagerErr
}
} else { // from management
projects, projectsErr := projectsLoad(c.String("management-projects-endpoint"))
if projectsErr != nil {
return projectsErr
}
projectsManagerErr := projectsManager.load(projects)
if projectsManagerErr != nil {
return projectsManagerErr
}
go func() {
for {
time.Sleep(managementCallInterval)
e := workerProjects(c.String("management-projects-endpoint"), projectsManager)
if e != nil {
conf.getLogger().
Error().
Str("type", "projects_load").
Str("e", e.Error()).
Bool("success", false).
Send()
continue
}
conf.getLogger().
Info().
Str("type", "projects_load").
Int("number", projectsManager.total).
Bool("success", true).
Send()
}
}()
}
conn, connErr := pgx.Connect(context.Background(), c.String("postgis-uri"))
if connErr != nil {
return connErr
}
defer conn.Close(context.Background())
geoParser, geoParserErr := newGeoParser(conn, c.String("mmdb-city-path"), c.String("mmdb-asn-path"))
if geoParserErr != nil {
return geoParserErr
}
refererParser := newRefererParser()
userAgentParser := newUserAgentParser()
app := newHTTPServer(
conf,
geoParser,
refererParser,
userAgentParser,
projectsManager,
red,
)
return app.Listen(c.String("listen"))
}