From a273292d972f1f35d262c576e696dd0992be8be7 Mon Sep 17 00:00:00 2001 From: Kata Date: Sun, 14 Apr 2019 12:57:53 -0500 Subject: [PATCH] [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. --- packages/roleypoly-server/Roleypoly.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/roleypoly-server/Roleypoly.js b/packages/roleypoly-server/Roleypoly.js index 26fe937..16d30b9 100644 --- a/packages/roleypoly-server/Roleypoly.js +++ b/packages/roleypoly-server/Roleypoly.js @@ -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)