Fix MongoDB connectivity checking

This commit is contained in:
syuilo 2019-02-04 11:48:59 +09:00
parent 6abd281cd9
commit f8b2b746b5

View File

@ -13,7 +13,7 @@ import * as portscanner from 'portscanner';
import * as isRoot from 'is-root'; import * as isRoot from 'is-root';
import Xev from 'xev'; import Xev from 'xev';
import * as sysUtils from 'systeminformation'; import * as sysUtils from 'systeminformation';
import mongo, { nativeDbConn } from './db/mongodb'; import { nativeDbConn } from './db/mongodb';
import Logger from './misc/logger'; import Logger from './misc/logger';
import serverStats from './daemons/server-stats'; import serverStats from './daemons/server-stats';
@ -193,7 +193,12 @@ async function init(): Promise<Config> {
} }
// Try to connect to MongoDB // Try to connect to MongoDB
await checkMongoDB(config); try {
await checkMongoDB(config);
} catch (e) {
bootLogger.error('Cannot connect to database', true);
process.exit(1);
}
return config; return config;
} }
@ -201,24 +206,33 @@ async function init(): Promise<Config> {
const requiredMongoDBVersion = [3, 6]; const requiredMongoDBVersion = [3, 6];
function checkMongoDB(config: Config) { function checkMongoDB(config: Config) {
const mongoDBLogger = bootLogger.createSubLogger('db'); return new Promise((res, rej) => {
const u = config.mongodb.user ? encodeURIComponent(config.mongodb.user) : null; const mongoDBLogger = bootLogger.createSubLogger('db');
const p = config.mongodb.pass ? encodeURIComponent(config.mongodb.pass) : null; const u = config.mongodb.user ? encodeURIComponent(config.mongodb.user) : null;
const uri = `mongodb://${u && p ? `${u}:****@` : ''}${config.mongodb.host}:${config.mongodb.port}/${config.mongodb.db}`; const p = config.mongodb.pass ? encodeURIComponent(config.mongodb.pass) : null;
mongoDBLogger.info(`Connecting to ${uri}`); const uri = `mongodb://${u && p ? `${u}:****@` : ''}${config.mongodb.host}:${config.mongodb.port}/${config.mongodb.db}`;
mongoDBLogger.info(`Connecting to ${uri} ...`);
mongo.then(() => { nativeDbConn().then(db => {
mongoDBLogger.succ('Connectivity confirmed'); mongoDBLogger.succ('Connectivity confirmed');
nativeDbConn().then(db => db.admin().serverInfo()).then(x => x.version).then((version: string) => { db.admin().serverInfo().then(x => {
mongoDBLogger.info(`Version: ${version}`); const version = x.version as string;
if (lessThan(version.split('.').map(x => parseInt(x, 10)), requiredMongoDBVersion)) { mongoDBLogger.info(`Version: ${version}`);
mongoDBLogger.error(`MongoDB version is less than ${requiredMongoDBVersion.join('.')}. Please upgrade it.`); if (lessThan(version.split('.').map(x => parseInt(x, 10)), requiredMongoDBVersion)) {
process.exit(1); mongoDBLogger.error(`MongoDB version is less than ${requiredMongoDBVersion.join('.')}. Please upgrade it.`);
} rej('outdated version');
} else {
res();
}
}).catch(err => {
mongoDBLogger.error(`Failed to fetch server info: ${err.message}`);
rej(err);
});
}).catch(err => {
mongoDBLogger.error(err.message);
rej(err);
}); });
}).catch(err => {
mongoDBLogger.error(err.message);
}); });
} }