-
Notifications
You must be signed in to change notification settings - Fork 71.8k
/
mongo-storage.js
80 lines (66 loc) · 2.59 KB
/
mongo-storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
var mongodb = require('mongodb');
var connection = null;
function init (env, cb, forceNewConnection) {
var MongoClient = mongodb.MongoClient;
var mongo = {};
function maybe_connect (cb) {
if (connection != null && !forceNewConnection) {
console.log('Reusing MongoDB connection handler');
// If there is a valid callback, then return the Mongo-object
mongo.db = connection;
if (cb && cb.call) {
cb(null, mongo);
}
} else {
if (!env.storageURI) {
throw new Error('MongoDB connection string is missing. Please set MONGO_CONNECTION environment variable');
}
console.log('Setting up new connection to MongoDB');
var timeout = 30 * 1000;
var options = { reconnectInterval: 10000, reconnectTries: 500, connectTimeoutMS: timeout,
socketTimeoutMS: timeout, useNewUrlParser: true };
var connect_with_retry = function(i) {
return MongoClient.connect(env.storageURI, options, function connected(err, client) {
if (err) {
//console.log('err=', err)
if (err.name && err.name === "MongoNetworkError") {
var timeout = (i > 15) ? 60000 : i*3000;
console.log('Error connecting to MongoDB: %j - retrying in ' + timeout/1000 + ' sec', err);
setTimeout(connect_with_retry, timeout, i+1);
} else if (err.message) {
throw new Error('MongoDB connection string '+env.storageURI+' seems invalid: '+err.message) ;
}
} else {
console.log('Successfully established a connected to MongoDB');
var dbName = env.storageURI.split('/').pop().split('?');
dbName=dbName[0]; // drop Connection Options
mongo.db = client.db(dbName);
connection = mongo.db;
mongo.client = client;
// If there is a valid callback, then invoke the function to perform the callback
if (cb && cb.call) {
cb(err, mongo);
}
}
});
};
connect_with_retry(1);
}
}
mongo.collection = function get_collection (name) {
return connection.collection(name);
};
mongo.ensureIndexes = function ensureIndexes (collection, fields) {
fields.forEach(function (field) {
console.info('ensuring index for: ' + field);
collection.ensureIndex(field, function (err) {
if (err) {
console.error('unable to ensureIndex for: ' + field + ' - ' + err);
}
});
});
};
return maybe_connect(cb);
}
module.exports = init;