5 require('gitter-web-persistence');
6 var mongoose = require('mongoose');
8 function die(stage, error) {
9 console.error('DB connection failed during ' + stage);
11 console.error(error.stack);
16 console.log('Connecting to database');
18 var db = mongoose.connection.db;
19 var adminDb = db.admin();
21 mongoose.connection.on('open', function() {
22 adminDb.ping(function(err, pingResult) {
23 if (err) return die('ping', err);
26 !pingResult.documents ||
27 !pingResult.documents.length ||
28 !pingResult.documents[0] ||
29 !pingResult.documents[0].ok
31 return die('ping', 'ping fail');
33 adminDb.replSetGetStatus(function(err, info) {
34 if (err) return die('repl', err);
35 if (!info || info.myState !== 1) return die('repl', 'replica set fail');
37 var pingtestCollection = db.collection('pingtest');
38 pingtestCollection.insert({ ping: Date.now() }, function(err) {
39 if (err) return die('insert', err);
41 pingtestCollection.remove({}, function(err) {
42 if (err) return die('remove', err);