Skip to content

Commit 35f6af6

Browse files
authored
Try to recconnect if mongodb connection drops (#73)
* Try to recconnect if mongodb connection drops * Set mongodb.connection_timeout and mongodb.health_check_interval in config
1 parent adfcfe5 commit 35f6af6

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

server/configs/codeflow.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ plugins:
3232
database: "codeflow"
3333
uri: "mongodb://mongo:27017"
3434
ssl: false
35+
connection_timeout: 3
36+
health_check_interval: 10
3537
service_address: ":3001"
3638
builds:
3739
path: "/builds"

server/plugins/codeflow/codeflow.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,33 @@ func (x *Codeflow) Start(events chan agent.Event) error {
141141
return conn, err
142142
}
143143

144-
config.DialInfo.Timeout = time.Second * 3
144+
config.DialInfo.Timeout = time.Second * viper.GetDuration("plugins.codeflow.mongodb.connection_timeout")
145145
}
146146

147147
db, err = bongo.Connect(config)
148148
if err != nil {
149149
log.Fatal(err)
150150
}
151151

152+
// Try to reconnect if connection drops
153+
go func(session *mgo.Session) {
154+
var err error
155+
for {
156+
err = session.Ping()
157+
if err != nil {
158+
fmt.Println("Lost connection to MongoDB!!")
159+
session.Refresh()
160+
err = session.Ping()
161+
if err == nil {
162+
fmt.Println("Reconnect to MongoDB successful.")
163+
} else {
164+
panic("Reconnect to MongoDB failed!!")
165+
}
166+
}
167+
time.Sleep(time.Second * viper.GetDuration("plugins.codeflow.mongodb.health_check_interval"))
168+
}
169+
}(db.Session)
170+
152171
go x.Listen()
153172

154173
log.Printf("Started Codeflow service on %s\n", x.ServiceAddress)

0 commit comments

Comments
 (0)