From d3c7129e1f457f99cf10490a10ddff55fbeb29e2 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 5 Feb 2019 06:43:36 +0900 Subject: [PATCH] Refactoring: Extract checkMongoDB function --- src/index.ts | 37 ++----------------------------------- src/misc/check-mongodb.ts | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 35 deletions(-) create mode 100644 src/misc/check-mongodb.ts diff --git a/src/index.ts b/src/index.ts index 915873723..af18977a2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,7 +13,6 @@ import * as portscanner from 'portscanner'; import * as isRoot from 'is-root'; import Xev from 'xev'; import * as sysUtils from 'systeminformation'; -import { nativeDbConn } from './db/mongodb'; import Logger from './misc/logger'; import serverStats from './daemons/server-stats'; @@ -23,6 +22,7 @@ import { Config } from './config/types'; import { lessThan } from './prelude/array'; import * as pkg from '../package.json'; import { program } from './argv'; +import { checkMongoDB } from './misc/check-mongodb'; const logger = new Logger('core', 'cyan'); const bootLogger = logger.createSubLogger('boot', 'magenta'); @@ -199,7 +199,7 @@ async function init(): Promise { // Try to connect to MongoDB try { - await checkMongoDB(config); + await checkMongoDB(config, bootLogger); } catch (e) { bootLogger.error('Cannot connect to database', true); process.exit(1); @@ -208,39 +208,6 @@ async function init(): Promise { return config; } -const requiredMongoDBVersion = [3, 6]; - -function checkMongoDB(config: Config) { - return new Promise((res, rej) => { - const mongoDBLogger = bootLogger.createSubLogger('db'); - const u = config.mongodb.user ? encodeURIComponent(config.mongodb.user) : null; - const p = config.mongodb.pass ? encodeURIComponent(config.mongodb.pass) : null; - const uri = `mongodb://${u && p ? `${u}:****@` : ''}${config.mongodb.host}:${config.mongodb.port}/${config.mongodb.db}`; - mongoDBLogger.info(`Connecting to ${uri} ...`); - - nativeDbConn().then(db => { - mongoDBLogger.succ('Connectivity confirmed'); - - db.admin().serverInfo().then(x => { - const version = x.version as string; - mongoDBLogger.info(`Version: ${version}`); - if (lessThan(version.split('.').map(x => parseInt(x, 10)), requiredMongoDBVersion)) { - 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); - }); - }); -} - async function spawnWorkers(limit: number = Infinity) { const workers = Math.min(limit, os.cpus().length); bootLogger.info(`Starting ${workers} worker${workers === 1 ? '' : 's'}...`); diff --git a/src/misc/check-mongodb.ts b/src/misc/check-mongodb.ts new file mode 100644 index 000000000..f3839da31 --- /dev/null +++ b/src/misc/check-mongodb.ts @@ -0,0 +1,37 @@ +import { nativeDbConn } from '../db/mongodb'; +import { Config } from '../config/types'; +import Logger from './logger'; +import { lessThan } from '../prelude/array'; + +const requiredMongoDBVersion = [3, 6]; + +export function checkMongoDB(config: Config, logger: Logger) { + return new Promise((res, rej) => { + const mongoDBLogger = logger.createSubLogger('db'); + const u = config.mongodb.user ? encodeURIComponent(config.mongodb.user) : null; + const p = config.mongodb.pass ? encodeURIComponent(config.mongodb.pass) : null; + const uri = `mongodb://${u && p ? `${u}:****@` : ''}${config.mongodb.host}:${config.mongodb.port}/${config.mongodb.db}`; + mongoDBLogger.info(`Connecting to ${uri} ...`); + + nativeDbConn().then(db => { + mongoDBLogger.succ('Connectivity confirmed'); + + db.admin().serverInfo().then(x => { + const version = x.version as string; + mongoDBLogger.info(`Version: ${version}`); + if (lessThan(version.split('.').map(x => parseInt(x, 10)), requiredMongoDBVersion)) { + 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); + }); + }); +}