[server] retry the SQL connection if it isn't immediately awake on start

this prevents `yarn dev` from erroring out since postgres takes more than 0ms to start.
This commit is contained in:
41666 2019-04-14 12:57:53 -05:00
parent 10d5979ce1
commit a273292d97
No known key found for this signature in database
GPG key ID: BC51D07640DC10AF

View file

@ -13,6 +13,7 @@ import PresentationService from './services/presentation'
import RPCServer from './rpc'
import fetchModels, { type Models } from './models'
import fetchApis from './api'
import retry from 'async-retry'
import type SocketIO from 'socket.io'
import type KoaApp, { Context } from 'koa'
@ -113,8 +114,18 @@ class Roleypoly {
this.ctx.sql = sequelize
this.M = fetchModels(sequelize)
this.ctx.M = this.M
await retry(async bail => {
try {
await sequelize.authenticate()
} catch (e) {
log.error('sequelize failed to connect.', e.message)
throw e
}
})
if (!process.env.DB_NO_SYNC) {
await sequelize.sync()
await this.ctx.sql.sync()
}
this.ctx.server = new ServerService(this.ctx)