-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (129 loc) · 3.49 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
140
141
142
143
144
145
146
147
148
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"os"
"strconv"
"time"
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"net/url"
)
func main() {
dbType := os.Getenv("DB_TYPE")
if dbType == "" {
dbType = "mysql"
}
// mysql
mysqlName := os.Getenv("MYSQL_NAME")
if mysqlName == "" {
mysqlName = "test"
}
mysqlUser := os.Getenv("MYSQL_USER")
if mysqlUser == "" {
mysqlUser = "root"
}
mysqlPass := os.Getenv("MYSQL_PASS")
if mysqlPass == "" {
mysqlPass = ""
}
mysqlHost := os.Getenv("MYSQL_HOST")
if mysqlHost == "" && dbType == "mysql" {
fmt.Fprintf(os.Stderr, "\"MYSQL_HOST\" not set, but \"DB_TYPE\" is set \"mysql\"")
os.Exit(1)
}
mysqlPort := os.Getenv("MYSQL_PORT")
if mysqlPort == "" {
mysqlPort = "3306"
}
// mongodb
mongoUri := os.Getenv("MONGODB_URI")
if mongoUri == "" && dbType == "mongodb" {
fmt.Fprintf(os.Stderr, "\"MONGODB_URI\" not set, but \"DB_TYPE\" is set \"mongodb\"")
os.Exit(1)
}
triesStr := os.Getenv("TRIES")
tries, err := strconv.Atoi(triesStr)
if err != nil {
tries = 10
}
i := 1
for i = 1; i <= tries; i += 1 {
sleepS := 3*i + 1
sleep := time.Duration(sleepS) * time.Second
if dbType == "mysql" {
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", mysqlUser, mysqlPass, mysqlHost, mysqlPort, mysqlName))
if err != nil {
fmt.Fprintf(os.Stderr, "Try (%d/%d) sleep %d seconds error connect to '%s@password@tcp(%s:%s)/%s': %v\n", i, tries, sleepS, mysqlUser, mysqlHost, mysqlPort, mysqlName, err)
time.Sleep(sleep)
continue
}
defer db.Close()
_, err = GetSQLTables(db)
if err != nil {
fmt.Fprintf(os.Stderr, "Try (%d/%d) sleep %d seconds error: %v\n", i, tries, sleepS, err)
time.Sleep(sleep)
continue
}
fmt.Println("Connect success")
break
}
if dbType == "mongodb" {
url, err := url.Parse(mongoUri)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: cannot get db from uri: %v\n", err)
os.Exit(1)
}
dbName := url.Path
if dbName[0] == '/' {
dbName = url.Path[1:]
}
client, err := mongo.NewClient(options.Client().ApplyURI(mongoUri))
if err != nil {
fmt.Fprintf(os.Stderr, "Try (%d/%d) sleep %d seconds error mongodb connect to '%s': %v\n", i, tries, sleepS, url.Host)
time.Sleep(sleep)
continue
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Error cannot create context %v\n", err)
os.Exit(1)
}
_, err = client.Database(dbName).ListCollectionNames(ctx, bson.D{})
if err != nil {
fmt.Fprintf(os.Stderr, "Try (%d/%d) sleep %d seconds error list collections: %v\n", i, tries, sleepS, err)
time.Sleep(sleep)
continue
}
fmt.Println("Connect success")
break
}
}
if i == tries + 1 {
fmt.Fprintf(os.Stderr, "Connection attempts have failed")
os.Exit(2)
}
}
func GetSQLTables(db *sql.DB) ([]string, error) {
errorFuncName := "Func GetSQLTables() error"
query := "SHOW TABLES"
tableRows, err := db.Query(query)
if err != nil {
return nil, fmt.Errorf("%s: query: '%s': %v", errorFuncName, query, err)
}
defer tableRows.Close()
var tables []string
for tableRows.Next() {
var table string
err = tableRows.Scan(&table)
if err != nil {
return nil, fmt.Errorf("%s: for query '%s', cannot read table. Error: %v", errorFuncName, query, err)
}
tables = append(tables, table)
}
return tables, nil
}