Fix: stop in DB check (#3356)

This commit is contained in:
MeiMei 2018-11-21 12:45:40 +09:00 committed by syuilo
parent f7d5f597f3
commit b7b36973f7

View file

@ -23,7 +23,6 @@ import notesStats from './daemons/notes-stats';
import loadConfig from './config/load'; import loadConfig from './config/load';
import { Config } from './config/types'; import { Config } from './config/types';
import { lessThan } from './prelude/array'; import { lessThan } from './prelude/array';
import { Db } from 'mongodb';
const clusterLog = debug('misskey:cluster'); const clusterLog = debug('misskey:cluster');
const ev = new Xev(); const ev = new Xev();
@ -192,38 +191,35 @@ async function init(): Promise<Config> {
} }
// Try to connect to MongoDB // Try to connect to MongoDB
//await checkMongoDB(config); await checkMongoDB(config);
return config; return config;
} }
const requiredMongoDBVersion = [3, 6]; const requiredMongoDBVersion = [3, 6];
function checkMongoDB(config: Config): Promise<void> { function checkMongoDB(config: Config) {
const mongoDBLogger = new Logger('MongoDB'); const mongoDBLogger = new Logger('MongoDB');
const u = config.mongodb.user ? encodeURIComponent(config.mongodb.user) : null; const u = config.mongodb.user ? encodeURIComponent(config.mongodb.user) : null;
const p = config.mongodb.pass ? encodeURIComponent(config.mongodb.pass) : 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}`; const uri = `mongodb://${u && p ? `${u}:****@` : ''}${config.mongodb.host}:${config.mongodb.port}/${config.mongodb.db}`;
mongoDBLogger.info(`Connecting to ${uri}`); mongoDBLogger.info(`Connecting to ${uri}`);
return mongo.then(async () => { mongo.then(() => {
mongoDBLogger.succ('Connectivity confirmed'); mongoDBLogger.succ('Connectivity confirmed');
const runningMongoDBVersion = (await nativeDbConn().then(getMongoDBVersion)).split('.').map(x => parseInt(x, 10)); nativeDbConn().then(db => db.admin().serverInfo()).then(x => x.version).then((version: string) => {
mongoDBLogger.info(`Version: ${runningMongoDBVersion.join('.')}`); mongoDBLogger.info(`Version: ${version}`);
if (lessThan(runningMongoDBVersion, requiredMongoDBVersion)) { if (lessThan(version.split('.').map(x => parseInt(x, 10)), requiredMongoDBVersion)) {
mongoDBLogger.error(`MongoDB version is less than ${requiredMongoDBVersion.join('.')}. Please upgrade it.`); mongoDBLogger.error(`MongoDB version is less than ${requiredMongoDBVersion.join('.')}. Please upgrade it.`);
process.exit(1); process.exit(1);
} }
});
}).catch(err => { }).catch(err => {
mongoDBLogger.error(err.message); mongoDBLogger.error(err.message);
}); });
} }
async function getMongoDBVersion(db: Db): Promise<string> {
return (await db.admin().serverInfo()).version;
}
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);
Logger.info(`Starting ${workers} worker${workers === 1 ? '' : 's'}...`); Logger.info(`Starting ${workers} worker${workers === 1 ? '' : 's'}...`);