fix: redis pubsub not being required correctly
split connection logic into separate modulev1.18.x
parent
186321e646
commit
8d4f20865f
@ -0,0 +1,67 @@
|
||||
'use strict';
|
||||
|
||||
const nconf = require('nconf');
|
||||
|
||||
const winston = require('winston');
|
||||
const _ = require('lodash');
|
||||
|
||||
const connection = module.exports;
|
||||
|
||||
connection.getConnectionString = function (mongo) {
|
||||
mongo = mongo || nconf.get('mongo');
|
||||
var usernamePassword = '';
|
||||
var uri = mongo.uri || '';
|
||||
if (mongo.username && mongo.password) {
|
||||
usernamePassword = nconf.get('mongo:username') + ':' + encodeURIComponent(nconf.get('mongo:password')) + '@';
|
||||
} else if (!uri.includes('@') || !uri.slice(uri.indexOf('://') + 3, uri.indexOf('@'))) {
|
||||
winston.warn('You have no mongo username/password setup!');
|
||||
}
|
||||
|
||||
// Sensible defaults for Mongo, if not set
|
||||
if (!mongo.host) {
|
||||
mongo.host = '127.0.0.1';
|
||||
}
|
||||
if (!mongo.port) {
|
||||
mongo.port = 27017;
|
||||
}
|
||||
const dbName = mongo.database;
|
||||
if (dbName === undefined || dbName === '') {
|
||||
winston.warn('You have no database name, using "nodebb"');
|
||||
mongo.database = 'nodebb';
|
||||
}
|
||||
|
||||
var hosts = mongo.host.split(',');
|
||||
var ports = mongo.port.toString().split(',');
|
||||
var servers = [];
|
||||
|
||||
for (var i = 0; i < hosts.length; i += 1) {
|
||||
servers.push(hosts[i] + ':' + ports[i]);
|
||||
}
|
||||
|
||||
return uri || 'mongodb://' + usernamePassword + servers.join() + '/' + mongo.database;
|
||||
};
|
||||
|
||||
connection.getConnectionOptions = function (mongo) {
|
||||
mongo = mongo || nconf.get('mongo');
|
||||
var connOptions = {
|
||||
poolSize: 10,
|
||||
reconnectTries: 3600,
|
||||
reconnectInterval: 1000,
|
||||
autoReconnect: true,
|
||||
connectTimeoutMS: 90000,
|
||||
useNewUrlParser: true,
|
||||
};
|
||||
|
||||
return _.merge(connOptions, mongo.options || {});
|
||||
};
|
||||
|
||||
connection.connect = function (options, callback) {
|
||||
callback = callback || function () { };
|
||||
|
||||
const mongoClient = require('mongodb').MongoClient;
|
||||
|
||||
const connString = connection.getConnectionString(options);
|
||||
const connOptions = connection.getConnectionOptions(options);
|
||||
|
||||
mongoClient.connect(connString, connOptions, callback);
|
||||
};
|
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
var mubsub = require('mubsub-nbb');
|
||||
var db = require('../mongo');
|
||||
var client = mubsub(db.getConnectionString(), db.getConnectionOptions());
|
||||
const mubsub = require('mubsub-nbb');
|
||||
const connection = require('./connection');
|
||||
const client = mubsub(connection.getConnectionString(), connection.getConnectionOptions());
|
||||
|
||||
module.exports = client.channel('pubsub');
|
||||
|
@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
const nconf = require('nconf');
|
||||
const winston = require('winston');
|
||||
const _ = require('lodash');
|
||||
|
||||
const connection = module.exports;
|
||||
|
||||
connection.getConnectionOptions = function (postgres) {
|
||||
postgres = postgres || nconf.get('postgres');
|
||||
// Sensible defaults for PostgreSQL, if not set
|
||||
if (!postgres.host) {
|
||||
postgres.host = '127.0.0.1';
|
||||
}
|
||||
if (!postgres.port) {
|
||||
postgres.port = 5432;
|
||||
}
|
||||
const dbName = postgres.database;
|
||||
if (dbName === undefined || dbName === '') {
|
||||
winston.warn('You have no database name, using "nodebb"');
|
||||
postgres.database = 'nodebb';
|
||||
}
|
||||
|
||||
var connOptions = {
|
||||
host: postgres.host,
|
||||
port: postgres.port,
|
||||
user: postgres.username,
|
||||
password: postgres.password,
|
||||
database: postgres.database,
|
||||
};
|
||||
|
||||
return _.merge(connOptions, postgres.options || {});
|
||||
};
|
||||
|
||||
connection.connect = function (options, callback) {
|
||||
const Pool = require('pg').Pool;
|
||||
|
||||
const connOptions = connection.getConnectionOptions(options);
|
||||
|
||||
const db = new Pool(connOptions);
|
||||
|
||||
db.connect(function (err) {
|
||||
callback(err, db);
|
||||
});
|
||||
};
|
@ -0,0 +1,71 @@
|
||||
'use strict';
|
||||
|
||||
const nconf = require('nconf');
|
||||
const redis = require('redis');
|
||||
const winston = require('winston');
|
||||
const _ = require('lodash');
|
||||
|
||||
const connection = module.exports;
|
||||
|
||||
connection.getConnectionOptions = function (redis) {
|
||||
redis = redis || nconf.get('redis');
|
||||
let connOptions = {};
|
||||
if (redis.password) {
|
||||
connOptions.auth_pass = redis.password;
|
||||
}
|
||||
|
||||
connOptions = _.merge(connOptions, redis.options || {});
|
||||
return connOptions;
|
||||
};
|
||||
|
||||
connection.connect = function (options, callback) {
|
||||
callback = callback || function () {};
|
||||
options = options || nconf.get('redis');
|
||||
var redis_socket_or_host = options.host;
|
||||
var cxn;
|
||||
var callbackCalled = false;
|
||||
|
||||
const connOptions = connection.getConnectionOptions(options);
|
||||
|
||||
if (redis_socket_or_host && redis_socket_or_host.indexOf('/') >= 0) {
|
||||
/* If redis.host contains a path name character, use the unix dom sock connection. ie, /tmp/redis.sock */
|
||||
cxn = redis.createClient(options.host, connOptions);
|
||||
} else {
|
||||
/* Else, connect over tcp/ip */
|
||||
cxn = redis.createClient(options.port, options.host, connOptions);
|
||||
}
|
||||
|
||||
cxn.on('error', function (err) {
|
||||
winston.error(err.stack);
|
||||
if (!callbackCalled) {
|
||||
callbackCalled = true;
|
||||
callback(err);
|
||||
}
|
||||
});
|
||||
|
||||
cxn.on('ready', function () {
|
||||
if (!callbackCalled) {
|
||||
callbackCalled = true;
|
||||
callback(null, cxn);
|
||||
}
|
||||
});
|
||||
|
||||
if (options.password) {
|
||||
cxn.auth(options.password);
|
||||
}
|
||||
|
||||
var dbIdx = parseInt(options.database, 10);
|
||||
if (dbIdx >= 0) {
|
||||
cxn.select(dbIdx, function (err) {
|
||||
if (err) {
|
||||
winston.error('NodeBB could not select Redis database. Redis returned the following error', err);
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
callbackCalled = true;
|
||||
return callback(new Error('[[error:no-database-selected]]'));
|
||||
}
|
||||
|
||||
return cxn;
|
||||
};
|
Loading…
Reference in New Issue