Skip to content

Commit

Permalink
database/master: Update connection check
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed Aug 27, 2018
1 parent 39db8a1 commit acbf952
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/bot/databases/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,27 @@ class IMasterController extends Interface {
async connect () {
let allOnline = true
for (let worker in cluster.workers) {
if (cluster.workers[worker].state !== 'online') allOnline = false
if (cluster.workers[worker].state !== 'online') {
allOnline = false
break
}
}
if (allOnline) setTimeout(() => { this.connected = true; DEBUG_MASTER('Connected') }, 5000) // TODO: send workers db find and if returned then its ok
else setTimeout(() => this.connect(), 10)
if (!(await this.checkConnection()) || !allOnline) return setTimeout(() => this.connect(), 1000) // re-do on first error
else this.connected = allOnline
}

async checkConnection () {
let timeout = new Promise((resolve, reject) => {
let id = setTimeout(() => {
clearTimeout(id)
DEBUG_MASTER('Connection to db timeouted')
resolve(false)
}, 5000)
})
return Promise.race([
this.find('info'),
timeout
])
}

sendRequest (resolve, reject, id, data) {
Expand Down

0 comments on commit acbf952

Please sign in to comment.