From 795eb756d76f0bb82ccb550ddf3cac573a553944 Mon Sep 17 00:00:00 2001 From: Carsten Milling Date: Sat, 17 Oct 2020 14:27:50 +0200 Subject: [PATCH] wait for db server to be available before running annotate() --- annotate.js | 2 +- app.js | 7 ------- bin/www | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/annotate.js b/annotate.js index 434143b..84da45d 100644 --- a/annotate.js +++ b/annotate.js @@ -33,7 +33,7 @@ const xqFindDocs = const queryfile = path.join(__dirname, 'xqy', 'annotate.xq'); const xqAnnotate = fs.readFileSync(queryfile, 'utf-8'); -module.exports = function () { +module.exports = async function () { return new Promise((resolve, reject) => { // eslint-disable-next-line promise/prefer-await-to-then query(xqFindDocs).then(response => { diff --git a/app.js b/app.js index a6986d4..e26139c 100644 --- a/app.js +++ b/app.js @@ -27,7 +27,6 @@ const path = require('path'); const express = require('express'); const debug = require('debug')('histvv:app'); const logger = require('morgan'); -const annotate = require('./annotate'); const staticHtml = require('./static'); const finish = require('./finish'); const config = require('./config'); @@ -38,12 +37,6 @@ debug({...config, db: { password: config.db.password ? '*****' : config.db.password }}); -annotate().then(n => { - console.log('all documents prepared (%s new)', n); -}).catch(error => { - console.warn(error); -}); - const routeHandlerFactory = require('./routehandler.js')(); const app = express(); diff --git a/bin/www b/bin/www index 090c586..21fd3c3 100755 --- a/bin/www +++ b/bin/www @@ -27,9 +27,11 @@ */ const http = require('http'); +const waitOn = require('wait-on'); const debug = require('debug')('histvv:server'); const config = require('../config'); const app = require('../app'); +const annotate = require('../annotate'); /** * Get port from environment and store in Express. @@ -44,6 +46,42 @@ app.set('port', port); const server = http.createServer(app); +(async function () { + const { + db: { + name: dbname, + port, + host, + user: username, + password + } + } = config; + + try { + const url = `http-get://${host}:${port}/rest/${dbname}`; + debug(`waiting for ${url}`); + await waitOn({ + resources: [url], + interval: 1000, + timeout: 30000, + window: 1500, + auth: {username, password} + }); + debug('database available'); + } catch (error) { + debug('database not available'); + console.log(error); + process.exit(1); + } + + // eslint-disable-next-line promise/prefer-await-to-then + annotate().then(n => { + console.log('all documents prepared (%s new)', n); + }).catch(error => { + console.warn(error); + }); +})(); + /** * Listen on provided port, on all network interfaces. */ @@ -96,6 +134,7 @@ function onError (error) { process.exit(1); // cannot fall through default: + console.log(error); throw error; } }