change style

v1.18.x
barisusakli 8 years ago
parent a2784738a4
commit b7b65c7d63

@ -1,239 +1,240 @@
'use strict'; 'use strict';
(function (module) {
var winston = require('winston');
var async = require('async');
var nconf = require('nconf');
var session = require('express-session');
var _ = require('underscore');
var semver = require('semver');
var db;
_.mixin(require('underscore.deep'));
module.questions = [
{
name: 'mongo:host',
description: 'Host IP or address of your MongoDB instance',
default: nconf.get('mongo:host') || '127.0.0.1',
},
{
name: 'mongo:port',
description: 'Host port of your MongoDB instance',
default: nconf.get('mongo:port') || 27017,
},
{
name: 'mongo:username',
description: 'MongoDB username',
default: nconf.get('mongo:username') || '',
},
{
name: 'mongo:password',
description: 'Password of your MongoDB database',
hidden: true,
default: nconf.get('mongo:password') || '',
before: function (value) { value = value || nconf.get('mongo:password') || ''; return value; },
},
{
name: 'mongo:database',
description: 'MongoDB database name',
default: nconf.get('mongo:database') || 'nodebb',
},
];
module.helpers = module.helpers || {};
module.helpers.mongo = require('./mongo/helpers');
module.init = function (callback) {
callback = callback || function () { };
var mongoClient = require('mongodb').MongoClient;
var usernamePassword = '';
if (nconf.get('mongo:username') && nconf.get('mongo:password')) {
usernamePassword = nconf.get('mongo:username') + ':' + encodeURIComponent(nconf.get('mongo:password')) + '@';
}
// Sensible defaults for Mongo, if not set var winston = require('winston');
if (!nconf.get('mongo:host')) { var async = require('async');
nconf.set('mongo:host', '127.0.0.1'); var nconf = require('nconf');
} var session = require('express-session');
if (!nconf.get('mongo:port')) { var _ = require('underscore');
nconf.set('mongo:port', 27017); var semver = require('semver');
} var db;
if (!nconf.get('mongo:database')) {
nconf.set('mongo:database', 'nodebb'); _.mixin(require('underscore.deep'));
}
var mongoModule = module.exports;
mongoModule.questions = [
{
name: 'mongo:host',
description: 'Host IP or address of your MongoDB instance',
default: nconf.get('mongo:host') || '127.0.0.1',
},
{
name: 'mongo:port',
description: 'Host port of your MongoDB instance',
default: nconf.get('mongo:port') || 27017,
},
{
name: 'mongo:username',
description: 'MongoDB username',
default: nconf.get('mongo:username') || '',
},
{
name: 'mongo:password',
description: 'Password of your MongoDB database',
hidden: true,
default: nconf.get('mongo:password') || '',
before: function (value) { value = value || nconf.get('mongo:password') || ''; return value; },
},
{
name: 'mongo:database',
description: 'MongoDB database name',
default: nconf.get('mongo:database') || 'nodebb',
},
];
mongoModule.helpers = mongoModule.helpers || {};
mongoModule.helpers.mongo = require('./mongo/helpers');
mongoModule.init = function (callback) {
callback = callback || function () { };
var mongoClient = require('mongodb').MongoClient;
var usernamePassword = '';
if (nconf.get('mongo:username') && nconf.get('mongo:password')) {
usernamePassword = nconf.get('mongo:username') + ':' + encodeURIComponent(nconf.get('mongo:password')) + '@';
}
// Sensible defaults for Mongo, if not set
if (!nconf.get('mongo:host')) {
nconf.set('mongo:host', '127.0.0.1');
}
if (!nconf.get('mongo:port')) {
nconf.set('mongo:port', 27017);
}
if (!nconf.get('mongo:database')) {
nconf.set('mongo:database', 'nodebb');
}
var hosts = nconf.get('mongo:host').split(',');
var ports = nconf.get('mongo:port').toString().split(',');
var servers = [];
for (var i = 0; i < hosts.length; i += 1) {
servers.push(hosts[i] + ':' + ports[i]);
}
var connString = 'mongodb://' + usernamePassword + servers.join() + '/' + nconf.get('mongo:database');
var connOptions = {
poolSize: 10,
reconnectTries: 3600,
reconnectInterval: 1000,
autoReconnect: true,
};
var hosts = nconf.get('mongo:host').split(','); connOptions = _.deepExtend(connOptions, nconf.get('mongo:options') || {});
var ports = nconf.get('mongo:port').toString().split(',');
var servers = [];
for (var i = 0; i < hosts.length; i += 1) { mongoClient.connect(connString, connOptions, function (err, _db) {
servers.push(hosts[i] + ':' + ports[i]); if (err) {
winston.error('NodeBB could not connect to your Mongo database. Mongo returned the following error: ' + err.message);
return callback(err);
} }
var connString = 'mongodb://' + usernamePassword + servers.join() + '/' + nconf.get('mongo:database'); db = _db;
var connOptions = {
poolSize: 10,
reconnectTries: 3600,
reconnectInterval: 1000,
autoReconnect: true,
};
connOptions = _.deepExtend(connOptions, nconf.get('mongo:options') || {});
mongoClient.connect(connString, connOptions, function (err, _db) {
if (err) {
winston.error('NodeBB could not connect to your Mongo database. Mongo returned the following error: ' + err.message);
return callback(err);
}
db = _db;
module.client = db;
require('./mongo/main')(db, module);
require('./mongo/hash')(db, module);
require('./mongo/sets')(db, module);
require('./mongo/sorted')(db, module);
require('./mongo/list')(db, module);
if (nconf.get('mongo:password') && nconf.get('mongo:username')) {
db.authenticate(nconf.get('mongo:username'), nconf.get('mongo:password'), function (err) {
callback(err);
});
} else {
winston.warn('You have no mongo password setup!');
callback();
}
});
};
module.initSessionStore = function (callback) {
var meta = require('../meta');
var sessionStore;
var ttl = meta.getSessionTTLSeconds(); mongoModule.client = db;
if (nconf.get('redis')) { require('./mongo/main')(db, mongoModule);
sessionStore = require('connect-redis')(session); require('./mongo/hash')(db, mongoModule);
var rdb = require('./redis'); require('./mongo/sets')(db, mongoModule);
rdb.client = rdb.connect(); require('./mongo/sorted')(db, mongoModule);
require('./mongo/list')(db, mongoModule);
module.sessionStore = new sessionStore({ if (nconf.get('mongo:password') && nconf.get('mongo:username')) {
client: rdb.client, db.authenticate(nconf.get('mongo:username'), nconf.get('mongo:password'), function (err) {
ttl: ttl, callback(err);
});
} else if (nconf.get('mongo')) {
sessionStore = require('connect-mongo')(session);
module.sessionStore = new sessionStore({
db: db,
ttl: ttl,
}); });
} else {
winston.warn('You have no mongo password setup!');
callback();
} }
});
};
callback(); mongoModule.initSessionStore = function (callback) {
}; var meta = require('../meta');
var sessionStore;
module.createIndices = function (callback) { var ttl = meta.getSessionTTLSeconds();
function createIndex(collection, index, options, callback) {
module.client.collection(collection).createIndex(index, options, callback);
}
if (!module.client) { if (nconf.get('redis')) {
winston.warn('[database/createIndices] database not initialized'); sessionStore = require('connect-redis')(session);
return callback(); var rdb = require('./redis');
} rdb.client = rdb.connect();
winston.info('[database] Checking database indices.'); mongoModule.sessionStore = new sessionStore({
async.series([ client: rdb.client,
async.apply(createIndex, 'objects', { _key: 1, score: -1 }, { background: true }), ttl: ttl,
async.apply(createIndex, 'objects', { _key: 1, value: -1 }, { background: true, unique: true, sparse: true }),
async.apply(createIndex, 'objects', { expireAt: 1 }, { expireAfterSeconds: 0, background: true }),
], function (err) {
if (err) {
winston.error('Error creating index ' + err.message);
return callback(err);
}
winston.info('[database] Checking database indices done!');
callback();
}); });
}; } else if (nconf.get('mongo')) {
sessionStore = require('connect-mongo')(session);
module.checkCompatibility = function (callback) { mongoModule.sessionStore = new sessionStore({
var mongoPkg = require('mongodb/package.json'); db: db,
ttl: ttl,
if (semver.lt(mongoPkg.version, '2.0.0')) { });
return callback(new Error('The `mongodb` package is out-of-date, please run `./nodebb setup` again.')); }
callback();
};
mongoModule.createIndices = function (callback) {
function createIndex(collection, index, options, callback) {
mongoModule.client.collection(collection).createIndex(index, options, callback);
}
if (!mongoModule.client) {
winston.warn('[database/createIndices] database not initialized');
return callback();
}
winston.info('[database] Checking database indices.');
async.series([
async.apply(createIndex, 'objects', { _key: 1, score: -1 }, { background: true }),
async.apply(createIndex, 'objects', { _key: 1, value: -1 }, { background: true, unique: true, sparse: true }),
async.apply(createIndex, 'objects', { expireAt: 1 }, { expireAfterSeconds: 0, background: true }),
], function (err) {
if (err) {
winston.error('Error creating index ' + err.message);
return callback(err);
} }
winston.info('[database] Checking database indices done!');
callback(); callback();
}; });
};
module.info = function (db, callback) {
if (!db) { mongoModule.checkCompatibility = function (callback) {
return callback(); var mongoPkg = require('mongodb/package.json');
}
async.parallel({ if (semver.lt(mongoPkg.version, '2.0.0')) {
serverStatus: function (next) { return callback(new Error('The `mongodb` package is out-of-date, please run `./nodebb setup` again.'));
db.command({ serverStatus: 1 }, next); }
},
stats: function (next) { callback();
db.command({ dbStats: 1 }, next); };
},
listCollections: function (next) { mongoModule.info = function (db, callback) {
db.listCollections().toArray(function (err, items) { if (!db) {
if (err) { return callback();
return next(err); }
} async.parallel({
async.map(items, function (collection, next) { serverStatus: function (next) {
db.collection(collection.name).stats(next); db.command({ serverStatus: 1 }, next);
}, next); },
}); stats: function (next) {
}, db.command({ dbStats: 1 }, next);
}, function (err, results) { },
if (err) { listCollections: function (next) {
return callback(err); db.listCollections().toArray(function (err, items) {
} if (err) {
var stats = results.stats; return next(err);
var scale = 1024 * 1024; }
async.map(items, function (collection, next) {
results.listCollections = results.listCollections.map(function (collectionInfo) { db.collection(collection.name).stats(next);
return { }, next);
name: collectionInfo.ns,
count: collectionInfo.count,
size: collectionInfo.size,
avgObjSize: collectionInfo.avgObjSize,
storageSize: collectionInfo.storageSize,
totalIndexSize: collectionInfo.totalIndexSize,
indexSizes: collectionInfo.indexSizes,
};
}); });
},
stats.mem = results.serverStatus.mem; }, function (err, results) {
stats.collectionData = results.listCollections; if (err) {
stats.network = results.serverStatus.network; return callback(err);
stats.raw = JSON.stringify(stats, null, 4); }
var stats = results.stats;
stats.avgObjSize = stats.avgObjSize.toFixed(2); var scale = 1024 * 1024;
stats.dataSize = (stats.dataSize / scale).toFixed(2);
stats.storageSize = (stats.storageSize / scale).toFixed(2); results.listCollections = results.listCollections.map(function (collectionInfo) {
stats.fileSize = stats.fileSize ? (stats.fileSize / scale).toFixed(2) : 0; return {
stats.indexSize = (stats.indexSize / scale).toFixed(2); name: collectionInfo.ns,
stats.storageEngine = results.serverStatus.storageEngine ? results.serverStatus.storageEngine.name : 'mmapv1'; count: collectionInfo.count,
stats.host = results.serverStatus.host; size: collectionInfo.size,
stats.version = results.serverStatus.version; avgObjSize: collectionInfo.avgObjSize,
stats.uptime = results.serverStatus.uptime; storageSize: collectionInfo.storageSize,
stats.mongo = true; totalIndexSize: collectionInfo.totalIndexSize,
indexSizes: collectionInfo.indexSizes,
callback(null, stats); };
}); });
};
module.close = function () { stats.mem = results.serverStatus.mem;
db.close(); stats.collectionData = results.listCollections;
}; stats.network = results.serverStatus.network;
}(exports)); stats.raw = JSON.stringify(stats, null, 4);
stats.avgObjSize = stats.avgObjSize.toFixed(2);
stats.dataSize = (stats.dataSize / scale).toFixed(2);
stats.storageSize = (stats.storageSize / scale).toFixed(2);
stats.fileSize = stats.fileSize ? (stats.fileSize / scale).toFixed(2) : 0;
stats.indexSize = (stats.indexSize / scale).toFixed(2);
stats.storageEngine = results.serverStatus.storageEngine ? results.serverStatus.storageEngine.name : 'mmapv1';
stats.host = results.serverStatus.host;
stats.version = results.serverStatus.version;
stats.uptime = results.serverStatus.uptime;
stats.mongo = true;
callback(null, stats);
});
};
mongoModule.close = function () {
db.close();
};

Loading…
Cancel
Save