-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
113 lines (96 loc) · 2.72 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
package main
import (
"context"
"fmt"
_ "github.com/ClickHouse/clickhouse-go"
_ "github.com/go-sql-driver/mysql"
"github.com/kos-v/dsnparser"
_ "github.com/lib/pq"
"log"
"resmo-db-mapper/pkg"
"resmo-db-mapper/pkg/config"
"time"
)
var (
version string
)
func main() {
if err := realMain(); err != nil {
log.Fatalln("failed to run resmo-database-agent:", err)
}
}
func realMain() error {
var config config.Config
err := config.ReadConfig(version)
if err != nil {
return fmt.Errorf("error while reading config : %w", err)
}
ctxDur, err := time.ParseDuration(config.Timeout)
if err != nil {
log.Printf("failed to parse the context duration from the configuration: %s. The context timeout duration is set to 10 seconds\n", err)
ctxDur = 10 * time.Second
}
ctx, cancel := context.WithTimeout(context.Background(), ctxDur)
defer cancel()
log.Printf("config: %s read successfully, will start to run queries\n", config)
dbType, err := getDatabaseType(config.DSN)
if err != nil {
return fmt.Errorf("error while getting database type from DSN: %w", err)
}
if config.Schedule == "" {
err := runQueries(ctx, config, dbType)
if err != nil {
return fmt.Errorf("error while running queries: %w", err)
}
log.Println("database resources ingested successfully")
return nil
}
dur, err := time.ParseDuration(config.Schedule)
if err != nil {
return fmt.Errorf("could not parse schedule from config: %w", err)
}
ticker := time.NewTicker(dur)
defer ticker.Stop()
for range ticker.C {
log.Println("running queries again with schedule: ", config.Schedule)
ctx, cancel := context.WithTimeout(context.Background(), ctxDur)
err := runQueries(ctx, config, dbType)
if err != nil {
return fmt.Errorf("error while running queries: %w", err)
}
cancel()
}
log.Println("database resources ingested successfully")
return nil
}
func runQueries(ctx context.Context, config config.Config, dbType string) error {
switch dbType {
case "mongo":
err := pkg.RunMongoQueries(ctx, config, dbType)
if err != nil {
return fmt.Errorf("mongo runner error: %w", err)
}
default:
err := pkg.RunSQLDatabaseQueries(ctx, config, dbType)
if err != nil {
return fmt.Errorf("sql runner error: %w", err)
}
}
return nil
}
func getDatabaseType(connectionString string) (string, error) {
var dbType string
scheme := dsnparser.Parse(connectionString).GetScheme()
if scheme == "postgres" || scheme == "postgresql" {
dbType = "postgres"
} else if scheme == "mongodb" {
dbType = "mongo"
} else if scheme == "clickhouse" {
dbType = "clickhouse"
} else if scheme == "mysql" {
dbType = "mysql"
} else {
return "", fmt.Errorf("unsupported database type for connection string: %s", connectionString)
}
return dbType, nil
}