-
Notifications
You must be signed in to change notification settings - Fork 4
/
dbconnector.go
executable file
·85 lines (70 loc) · 2.18 KB
/
dbconnector.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
package db
import (
"time"
"fmt"
"github.com/ottemo/commerce/utils"
)
// DBConnector takes logic of connection to DB and keeps it alive
type DBConnector struct {
connector InterfaceDBConnector
}
// NewDBConnector returns new DBConnector instance
func NewDBConnector(connector InterfaceDBConnector) *DBConnector {
return &DBConnector{connector: connector}
}
// ConnectAsync makes connection process asynchronously
func (it *DBConnector) ConnectAsync() error {
go func() {
if err := it.Connect(); err != nil {
it.log("Internal error: unable to start "+it.connector.GetEngineName()+" DBEngine: "+err.Error())
}
}()
return nil
}
// Connect implements connection process algorithm
func (it *DBConnector) Connect() error {
var connectionParams = it.connector.GetConnectionParams()
it.log("Connecting to "+it.connector.GetEngineName()+" DB. Timeout: 10 seconds.")
var ticker = time.NewTicker(it.connector.GetValidationInterval())
var isConnectMessageShown = false
for !it.connector.IsConnected() {
err := it.connector.Connect(connectionParams)
if err != nil {
if !isConnectMessageShown {
it.log("Can't connect to DBEngine: " + err.Error())
isConnectMessageShown = true;
}
it.log("Wait " + utils.InterfaceToString(10) + " seconds to reconnect.")
_ = <-ticker.C
} else {
it.connector.SetConnected(true)
it.log("DB connection established.")
}
}
err := it.connector.AfterConnect(connectionParams)
if err != nil {
it.log("After DB connect error:")
}
go func() {
for range ticker.C {
if err := it.connector.Ping(); err != nil {
it.connector.SetConnected(false)
it.log("DB connection lost. Reconnect in 60 seconds.")
if err := it.connector.Reconnect(connectionParams); err != nil {
it.log("Unable to reconnect: "+err.Error())
}
} else if !it.connector.IsConnected() { // Show message only once
it.connector.SetConnected(true)
it.log("DB connection restored.")
}
}
}()
err = OnDatabaseStart()
return err
}
// log outputs messages to stdout and connector endpoint
func (it *DBConnector) log(message string) {
// output to stdout
fmt.Println(time.Now().Format(time.RFC3339), message)
it.connector.LogConnection(message)
}