Skip to content

Commit

Permalink
wait for db server to be available before running annotate()
Browse files Browse the repository at this point in the history
  • Loading branch information
cmil committed Oct 17, 2020
1 parent 5c34601 commit 795eb75
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion annotate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
7 changes: 0 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
Expand Down
39 changes: 39 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*/
Expand Down Expand Up @@ -96,6 +134,7 @@ function onError (error) {
process.exit(1);
// cannot fall through
default:
console.log(error);
throw error;
}
}
Expand Down

0 comments on commit 795eb75

Please sign in to comment.