-
Notifications
You must be signed in to change notification settings - Fork 3
/
gf_eth_monitor.go
343 lines (282 loc) · 9.85 KB
/
gf_eth_monitor.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
GloFlow application and media management/publishing platform
Copyright (C) 2020 Ivan Trajkovic
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package main
import (
"os"
"fmt"
"path"
"context"
"github.com/spf13/cobra"
"github.com/spf13/viper"
log "github.com/sirupsen/logrus"
"github.com/gloflow/gloflow/go/gf_core"
"github.com/gloflow/gloflow/go/gf_web3/gf_eth_core"
"github.com/gloflow/gloflow/go/gf_web3/gf_web3_lib"
)
//-------------------------------------------------
func main() {
logFun, _ := gf_core.LogsInit()
log.SetOutput(os.Stdout)
cmd__base := cmdsInit(logFun)
err := cmd__base.Execute()
if err != nil {
panic(err)
}
}
//-------------------------------------------------
func runtimeGet(p_config_path_str string,
pLogFun func(string, string)) (*gf_eth_core.GF_runtime, error) {
// CONFIG
config_dir_path_str := path.Dir(p_config_path_str) // "./../config/"
config_name_str := path.Base(p_config_path_str) // "gf_eth_monitor"
config, err := config__init(config_dir_path_str, config_name_str)
if err != nil {
fmt.Println(err)
fmt.Println("failed to load config")
return nil, err
}
// RUNTIME_SYS
runtimeSys := &gf_core.RuntimeSys{
Service_name_str: "gf_eth_monitor",
LogFun: pLogFun,
// SENTRY - enable it for error reporting
ErrorsSendToSentryBool: true,
}
runtime, err := gf_eth_core.RuntimeGet(config, runtimeSys)
if err != nil {
return nil, err
}
return runtime, nil
}
//-------------------------------------------------
func cmdsInit(pLogFun func(string, string)) *cobra.Command {
// BASE
cmd__base := &cobra.Command{
Use: "gf_eth_monitor",
Short: "gf_eth_monitor server",
Long: "",
Run: func(p_cmd *cobra.Command, p_args []string) {
},
}
//--------------------
// CLI_ARGUMENT - CONFIG
cli_config_path__default_str := "./../config/gf_eth_monitor"
var cli_config_path_str string
cmd__base.PersistentFlags().StringVarP(&cli_config_path_str, "config", "", cli_config_path__default_str,
"config file path on the local FS")
//--------------------
// CLI_ARGUMENT - PORT
cmd__base.PersistentFlags().StringP("port", "p", "PORT NUMBER",
"port on which to listen for HTTP traffic") // Cobra CLI argument
err := viper.BindPFlag("port", cmd__base.PersistentFlags().Lookup("port")) // Bind Cobra CLI argument to a Viper configuration (for default value)
if err != nil {
fmt.Println("failed to bind CLI arg to Viper config")
panic(err)
}
// ENV
err = viper.BindEnv("port", "GF_PORT")
if err != nil {
fmt.Println("failed to bind ENV var to Viper config")
panic(err)
}
//--------------------
// CLI_ARGUMENT - PORT__METRICS
cmd__base.PersistentFlags().StringP("port_metrics", "", "METRICS PORT NUMBER",
"port on which to listen for metrics HTTP traffic")
err = viper.BindPFlag("port_metrics", cmd__base.PersistentFlags().Lookup("port_metrics"))
if err != nil {
fmt.Println("failed to bind CLI arg to Viper config")
panic(err)
}
// ENV
err = viper.BindEnv("port_metrics", "GF_PORT_METRICS")
if err != nil {
fmt.Println("failed to bind ENV var to Viper config")
panic(err)
}
//--------------------
// CLI_ARGUMENT - MONGODB_HOST
cmd__base.PersistentFlags().StringP("mongodb_host", "m", "MONGODB HOST", "mongodb host to which to connect") // Cobra CLI argument
err = viper.BindPFlag("mongodb_host", cmd__base.PersistentFlags().Lookup("mongodb_host")) // Bind Cobra CLI argument to a Viper configuration (for default value)
if err != nil {
fmt.Println("failed to bind CLI arg to Viper config")
panic(err)
}
// ENV
err = viper.BindEnv("mongodb_host", "GF_MONGODB_HOST")
if err != nil {
fmt.Println("failed to bind ENV var to Viper config")
panic(err)
}
//--------------------
// CLI_ARGUMENT - INFLUXDB_HOST
cmd__base.PersistentFlags().StringP("influxdb_host", "i", "INFLUXDB HOST", "influxdb host to which to connect") // Cobra CLI argument
err = viper.BindPFlag("influxdb_host", cmd__base.PersistentFlags().Lookup("influxdb_host")) // Bind Cobra CLI argument to a Viper configuration (for default value)
if err != nil {
fmt.Println("failed to bind CLI arg to Viper config")
panic(err)
}
// ENV
err = viper.BindEnv("influxdb_host", "GF_INFLUXDB_HOST")
if err != nil {
fmt.Println("failed to bind ENV var to Viper config")
panic(err)
}
//--------------------
// CLI_ARGUMENT - AWS_SQS_QUEUE
cmd__base.PersistentFlags().StringP("aws_sqs_queue", "q", "AWS SQS QUEUE", "AWS SQS queue from which to consume events")
err = viper.BindPFlag("aws_sqs_queue", cmd__base.PersistentFlags().Lookup("aws_sqs_queue"))
if err != nil {
fmt.Println("failed to bind CLI arg to Viper config")
panic(err)
}
// ENV
err = viper.BindEnv("aws_sqs_queue", "GF_AWS_SQS_QUEUE")
if err != nil {
fmt.Println("failed to bind ENV var to Viper config")
panic(err)
}
//--------------------
// CLI_ARGUMENT - WORKERS_AWS_DISCOVERY
cmd__base.PersistentFlags().StringP("workers_aws_discovery", "", "WORKERS HOSTS", "if AWS EC2 discovery should be enbaled to dynamicaly discover workers")
err = viper.BindPFlag("workers_aws_discovery", cmd__base.PersistentFlags().Lookup("workers_aws_discovery"))
if err != nil {
fmt.Println("failed to bind CLI arg to Viper config")
panic(err)
}
// ENV
err = viper.BindEnv("workers_aws_discovery", "GF_WORKERS_AWS_DISCOVERY")
if err != nil {
fmt.Println("failed to bind ENV var to Viper config")
panic(err)
}
//--------------------
// CLI_ARGUMENT - WORKERS_HOSTS
cmd__base.PersistentFlags().StringP("workers_hosts", "", "WORKERS HOSTS", "list of all workers hosts, ',' separated")
err = viper.BindPFlag("workers_hosts", cmd__base.PersistentFlags().Lookup("workers_hosts"))
if err != nil {
fmt.Println("failed to bind CLI arg to Viper config")
panic(err)
}
// ENV
err = viper.BindEnv("workers_hosts", "GF_WORKERS_HOSTS")
if err != nil {
fmt.Println("failed to bind ENV var to Viper config")
panic(err)
}
//--------------------
// CLI_ARGUMENT - SENTRY_ENDPOINT
cmd__base.PersistentFlags().StringP("sentry_endpoint", "", "SENTRY ENDPOINT", "Sentry endpoint to send errors to")
err = viper.BindPFlag("sentry_endpoint", cmd__base.PersistentFlags().Lookup("sentry_endpoint"))
if err != nil {
fmt.Println("failed to bind CLI arg to Viper config")
panic(err)
}
// ENV
err = viper.BindEnv("sentry_endpoint", "GF_SENTRY_ENDPOINT")
if err != nil {
fmt.Println("failed to bind ENV var to Viper config")
panic(err)
}
//--------------------
// CLI_ARGUMENT - EVENTS_CONSUME
cmd__base.PersistentFlags().StringP("events_consume", "", "EVENTS CONSUME", "on/off consumption and processing of events from queue")
err = viper.BindPFlag("events_consume", cmd__base.PersistentFlags().Lookup("events_consume"))
if err != nil {
fmt.Println("failed to bind CLI arg to Viper config")
panic(err)
}
// ENV
err = viper.BindEnv("events_consume", "GF_EVENTS_CONSUME")
if err != nil {
fmt.Println("failed to bind ENV var to Viper config")
panic(err)
}
//--------------------
// CLI_ARGUMENT - PY_PLUGINS
cmd__base.PersistentFlags().StringP("py_plugins_dir_path", "", "PY PLUGINS DIR PATH", "path to the directory holding Py plugin files")
err = viper.BindPFlag("py_plugins_dir_path", cmd__base.PersistentFlags().Lookup("py_plugins_dir_path"))
if err != nil {
fmt.Println("failed to bind CLI arg to Viper config")
panic(err)
}
// ENV
err = viper.BindEnv("py_plugins_dir_path", "GF_PY_PLUGINS_BASE_DIR_PATH")
if err != nil {
fmt.Println("failed to bind ENV var to Viper config")
panic(err)
}
//--------------------
// START
cmd__start := &cobra.Command{
Use: "start",
Short: "start some service or function",
Run: func(p_cmd *cobra.Command, p_args []string) {
},
}
// START_SERVICE
cmd__start_service := &cobra.Command{
Use: "service",
Short: "start the gf_eth_monitor service",
Long: "start the gf_eth_monitor service in a target cluster",
Run: func(p_cmd *cobra.Command, p_args []string) {
runtime, err := runtimeGet(cli_config_path_str, pLogFun)
if err != nil {
panic(err)
}
/*service_info := gf_web3_lib.GF_service_info{
Port_str: runtime.Config.Port_str,
SQS_queue_name_str: runtime.Config.AWS_SQS_queue_str,
Influxdb_client: runtime.Influxdb_client,
}*/
// RUN_SERVICE
gf_web3_lib.Run_service(runtime)
},
}
//--------------------
// TEST
cmd__test := &cobra.Command{
Use: "test",
Short: "test some functionility",
Run: func(p_cmd *cobra.Command, p_args []string) {
},
}
// TEST_WORKER_EVENT_PROCESS
cmd__test_worker_event_process := &cobra.Command{
Use: "worker_event_process",
Short: "test processing of worker events",
Run: func(p_cmd *cobra.Command, p_args []string) {
runtime, err := runtimeGet(cli_config_path_str, pLogFun)
if err != nil {
panic(err)
}
SQS_queue_name_str := runtime.Config.AWS_SQS_queue_str
queue_info, err := gf_web3_lib.Event__init_queue(SQS_queue_name_str, nil)
if err != nil {
panic(err)
}
ctx := context.Background()
// PROCESS_SINGLE_EVENT
gf_web3_lib.Event__process_from_sqs(queue_info, ctx, nil, runtime)
},
}
//--------------------
cmd__start.AddCommand(cmd__start_service)
cmd__test.AddCommand(cmd__test_worker_event_process)
cmd__base.AddCommand(cmd__start)
cmd__base.AddCommand(cmd__test)
return cmd__base
}