From 8c8e2ae190b8c797fbd1c395ad562937c8db1d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 8 Dec 2016 23:51:56 +0300 Subject: [PATCH] closes #5271 --- src/database/mongo.js | 51 +++++++++++++----------- src/database/redis.js | 18 +++++---- src/install.js | 91 ++++++++++++++++++++----------------------- 3 files changed, 82 insertions(+), 78 deletions(-) diff --git a/src/database/mongo.js b/src/database/mongo.js index 1528671770..7d5131f800 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -111,13 +111,11 @@ if (err) { return callback(err); } - createSessionStore(); - createIndices(); + createSessionStore(); }); } else { winston.warn('You have no mongo password setup!'); createSessionStore(); - createIndices(); } function createSessionStore() { @@ -137,32 +135,39 @@ db: db }); } - } + callback(); + } + }); + }; - function createIndices() { - 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); - } - winston.info('[database] Checking database indices done!'); - callback(err); - }); - } + module.createIndices = function(callback) { + function createIndex(collection, index, options, callback) { + module.client.collection(collection).createIndex(index, options, callback); + } + + if (!module.client) { + winston.warn('[database/createIndices] database not initialized'); + return callback(); + } - function createIndex(collection, index, options, callback) { - db.collection(collection).createIndex(index, options, 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(); + }); }; module.checkCompatibility = function (callback) { - var mongoPkg = require.main.require('./node_modules/mongodb/package.json'), - err = semver.lt(mongoPkg.version, '2.0.0') ? new Error('The `mongodb` package is out-of-date, please run `./nodebb setup` again.') : null; + var mongoPkg = require.main.require('./node_modules/mongodb/package.json'); + var err = semver.lt(mongoPkg.version, '2.0.0') ? new Error('The `mongodb` package is out-of-date, please run `./nodebb setup` again.') : null; if (err) { err.stacktrace = false; diff --git a/src/database/redis.js b/src/database/redis.js index e583aff4ff..e6184db758 100644 --- a/src/database/redis.js +++ b/src/database/redis.js @@ -2,13 +2,13 @@ (function (module) { - var winston = require('winston'), - nconf = require('nconf'), - semver = require('semver'), - session = require('express-session'), - redis, - connectRedis, - redisClient; + var winston = require('winston'); + var nconf = require('nconf'); + var semver = require('semver'); + var session = require('express-session'); + var redis; + var connectRedis; + var redisClient; module.questions = [ { @@ -107,6 +107,10 @@ return cxn; }; + module.createIndices = function (callback) { + setImmediate(callback); + }; + module.checkCompatibility = function (callback) { module.info(module.client, function (err, info) { if (err) { diff --git a/src/install.js b/src/install.js index 711932bb00..acf7aadc97 100644 --- a/src/install.js +++ b/src/install.js @@ -1,16 +1,15 @@ 'use strict'; -var async = require('async'), - fs = require('fs'), - path = require('path'), - prompt = require('prompt'), - winston = require('winston'), - nconf = require('nconf'), - utils = require('../public/src/utils.js'); +var async = require('async'); +var fs = require('fs'); +var path = require('path'); +var prompt = require('prompt'); +var winston = require('winston'); +var nconf = require('nconf'); +var utils = require('../public/src/utils.js'); - -var install = {}, - questions = {}; +var install = {}; +var questions = {}; questions.main = [ { @@ -124,40 +123,33 @@ function setupConfig(next) { prompt.delimiter = ''; prompt.colors = false; - if (!install.values) { - prompt.get(questions.main, function (err, config) { - if (err) { - process.stdout.write('\n\n'); - winston.warn('NodeBB setup ' + err.message); - process.exit(); + async.waterfall([ + function (next) { + if (install.values) { + // Use provided values, fall back to defaults + var config = {}; + var redisQuestions = require('./database/redis').questions; + var mongoQuestions = require('./database/mongo').questions; + var allQuestions = questions.main.concat(questions.optional).concat(redisQuestions).concat(mongoQuestions); + + allQuestions.forEach(function (question) { + config[question.name] = install.values[question.name] || question['default'] || undefined; + }); + setImmediate(next, null, config); + } else { + prompt.get(questions.main, next); } - - configureDatabases(config, function (err, config) { - completeConfigSetup(err, config, next); - }); - }); - } else { - // Use provided values, fall back to defaults - var config = {}, - redisQuestions = require('./database/redis').questions, - mongoQuestions = require('./database/mongo').questions, - allQuestions = questions.main.concat(questions.optional).concat(redisQuestions).concat(mongoQuestions); - - allQuestions.forEach(function (question) { - config[question.name] = install.values[question.name] || question['default'] || undefined; - }); - - configureDatabases(config, function (err, config) { - completeConfigSetup(err, config, next); - }); - } + }, + function (config, next) { + configureDatabases(config, next); + }, + function (config, next) { + completeConfigSetup(config, next); + } + ], next); } -function completeConfigSetup(err, config, next) { - if (err) { - return next(err); - } - +function completeConfigSetup(config, next) { // Add CI object if (install.ciVals) { config.test_database = {}; @@ -168,13 +160,17 @@ function completeConfigSetup(err, config, next) { } } - install.save(config, function (err) { - if (err) { - return next(err); + async.waterfall([ + function (next) { + install.save(config, next); + }, + function (next) { + require('./database').init(next); + }, + function (next) { + require('./database').createIndices(next); } - - require('./database').init(next); - }); + ], next); } function setupDefaultConfigs(next) { @@ -491,7 +487,6 @@ function setCopyrightWidget(next) { install.setup = function (callback) { - async.series([ checkSetupFlag, checkCIFlag,