Refactoring: Extract checkMongoDB function
This commit is contained in:
parent
3709ba95cd
commit
d3c7129e1f
37
src/index.ts
37
src/index.ts
|
@ -13,7 +13,6 @@ 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 { 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';
|
||||||
|
@ -23,6 +22,7 @@ import { Config } from './config/types';
|
||||||
import { lessThan } from './prelude/array';
|
import { lessThan } from './prelude/array';
|
||||||
import * as pkg from '../package.json';
|
import * as pkg from '../package.json';
|
||||||
import { program } from './argv';
|
import { program } from './argv';
|
||||||
|
import { checkMongoDB } from './misc/check-mongodb';
|
||||||
|
|
||||||
const logger = new Logger('core', 'cyan');
|
const logger = new Logger('core', 'cyan');
|
||||||
const bootLogger = logger.createSubLogger('boot', 'magenta');
|
const bootLogger = logger.createSubLogger('boot', 'magenta');
|
||||||
|
@ -199,7 +199,7 @@ async function init(): Promise<Config> {
|
||||||
|
|
||||||
// Try to connect to MongoDB
|
// Try to connect to MongoDB
|
||||||
try {
|
try {
|
||||||
await checkMongoDB(config);
|
await checkMongoDB(config, bootLogger);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
bootLogger.error('Cannot connect to database', true);
|
bootLogger.error('Cannot connect to database', true);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
@ -208,39 +208,6 @@ async function init(): Promise<Config> {
|
||||||
return config;
|
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) {
|
async function spawnWorkers(limit: number = Infinity) {
|
||||||
const workers = Math.min(limit, os.cpus().length);
|
const workers = Math.min(limit, os.cpus().length);
|
||||||
bootLogger.info(`Starting ${workers} worker${workers === 1 ? '' : 's'}...`);
|
bootLogger.info(`Starting ${workers} worker${workers === 1 ? '' : 's'}...`);
|
||||||
|
|
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in New Issue