From ca332d23c037c72dab1e2bd8e6e20a8e322c4c4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 1 Apr 2015 12:28:01 -0400 Subject: [PATCH] wait for createIndex errors --- src/database/mongo.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/database/mongo.js b/src/database/mongo.js index e420f003b9..f0c45cbc12 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -40,6 +40,7 @@ module.helpers.mongo = require('./mongo/helpers'); module.init = function(callback) { + callback = callback || function() {}; try { var sessionStore; mongoClient = require('mongodb').MongoClient; @@ -85,10 +86,10 @@ db = _db; module.client = db; - + if (!nconf.get('redis')) { // TEMP: to fix connect-mongo, see https://github.com/kcbanner/connect-mongo/issues/161 - db.openCalled = true + db.openCalled = true; module.sessionStore = new sessionStore({ db: db }); @@ -119,29 +120,26 @@ } function createIndices() { - createIndex('objects', {_key: 1, score: -1}, {background:true}); - createIndex('objects', {_key: 1, value: -1}, {background:true}); - createIndex('objects', {expireAt: 1}, {expireAfterSeconds:0, background:true}); + async.parallel([ + async.apply(createIndex, 'objects', {_key: 1, score: -1}, {background: true}), + async.apply(createIndex, 'objects', {_key: 1, value: -1}, {background: true}), + async.apply(createIndex, 'objects', {expireAt: 1}, {expireAfterSeconds: 0, background: true}), - createIndex('searchtopic', {content: 'text', uid: 1, cid: 1}, {background:true}); - createIndex('searchtopic', {id: 1}, {background:true}); + async.apply(createIndex, 'searchtopic', {content: 'text', uid: 1, cid: 1}, {background: true}), + async.apply(createIndex, 'searchtopic', {id: 1}, {background: true}), - - createIndex('searchpost', {content: 'text', uid: 1, cid: 1}, {background:true}); - createIndex('searchpost', {id: 1}, {background:true}); - - - if (typeof callback === 'function') { - callback(); - } + async.apply(createIndex, 'searchpost', {content: 'text', uid: 1, cid: 1}, {background: true}), + async.apply(createIndex, 'searchpost', {id: 1}, {background: true}) + ], callback); } - function createIndex(collection, index, options) { + function createIndex(collection, index, options, callback) { db.collection(collection).ensureIndex(index, options, function(err) { if (err) { winston.error('Error creating index ' + err.message); } + callback(err); }); } });