-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (104 loc) · 3.06 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
package main
import (
dpfm_api_caller "data-platform-api-article-doc-reads-rmq-kube/DPFM_API_Caller"
dpfm_api_input_reader "data-platform-api-article-doc-reads-rmq-kube/DPFM_API_Input_Reader"
dpfm_api_output_formatter "data-platform-api-article-doc-reads-rmq-kube/DPFM_API_Output_Formatter"
"data-platform-api-article-doc-reads-rmq-kube/config"
"encoding/json"
"fmt"
"time"
"github.com/latonaio/golang-logging-library-for-data-platform/logger"
database "github.com/latonaio/golang-mysql-network-connector"
rabbitmq "github.com/latonaio/rabbitmq-golang-client-for-data-platform"
)
func main() {
l := logger.NewLogger()
conf := config.NewConf()
db, err := database.NewMySQL(conf.DB)
rmq, err := rabbitmq.NewRabbitmqClient(conf.RMQ.URL(), conf.RMQ.QueueFrom(), "", conf.RMQ.QueueToSQL(), 0)
if err != nil {
l.Fatal(err.Error())
}
defer rmq.Close()
iter, err := rmq.Iterator()
if err != nil {
l.Fatal(err.Error())
}
defer rmq.Stop()
caller := dpfm_api_caller.NewDPFMAPICaller(conf, rmq, db)
for msg := range iter {
l.Debug("received queue message")
start := time.Now()
err = callProcess(rmq, caller, conf, msg)
if err != nil {
msg.Fail()
continue
}
msg.Success()
l.Info("process time %v\n", time.Since(start).Milliseconds())
}
}
func recovery(l *logger.Logger, err *error) {
if e := recover(); e != nil {
*err = fmt.Errorf("error occurred: %w", e)
l.Error(err)
return
}
}
func getSessionID(data map[string]interface{}) string {
id := fmt.Sprintf("%v", data["runtime_session_id"])
return id
}
func callProcess(rmq *rabbitmq.RabbitmqClient, caller *dpfm_api_caller.DPFMAPICaller, conf *config.Conf, msg rabbitmq.RabbitmqMessage) (err error) {
l := logger.NewLogger()
defer recovery(l, &err)
l.AddHeaderInfo(map[string]interface{}{"runtime_session_id": getSessionID(msg.Data())})
var input dpfm_api_input_reader.SDC
var output dpfm_api_output_formatter.SDC
err = json.Unmarshal(msg.Raw(), &input)
if err != nil {
l.Error(err)
return
}
err = json.Unmarshal(msg.Raw(), &output)
if err != nil {
l.Error(err)
return
}
accepter := getAccepter(&input)
res, errs := caller.AsyncReads(accepter, &input, &output, l)
if len(errs) != 0 {
for _, err := range errs {
l.Error(err)
}
var errStr interface{}
errStr = fmt.Sprintf("%v", errs)
output.APIProcessingResult = getBoolPtr(false)
output.APIProcessingError = errs[0].Error()
output.Message = errStr
rmq.Send(conf.RMQ.QueueToResponse(), &output)
//rmq.Send("data-platform-api-request-reads-cache-manager-receive-queue", output)
return errs[0]
}
output.APIProcessingResult = getBoolPtr(true)
output.Message = res
l.JsonParseOut(output)
rmq.Send(conf.RMQ.QueueToResponse(), output)
//rmq.Send("data-platform-api-request-reads-cache-manager-receive-queue", output)
return nil
}
func getAccepter(input *dpfm_api_input_reader.SDC) []string {
accepter := input.Accepter
if len(input.Accepter) == 0 {
accepter = []string{"All"}
}
if accepter[0] == "All" {
accepter = []string{
"HeaderDoc",
}
}
return accepter
}
func getBoolPtr(b bool) *bool {
return &b
}