Files
NodeBB/src/database/redis.js
Barış Soner Uşaklı 805dcd7ca2 Async refactor in place (#7736)
* feat: allow both callback&and await

* feat: ignore async key

* feat: callbackify and promisify in same file

* Revert "feat: callbackify and promisify in same file"

This reverts commit cea206a9b8.

* feat: no need to store .callbackify

* feat: change getTopics to async

* feat: remove .async

* fix: byScore

* feat: rewrite topics/index and social with async/await

* fix: rewrite topics/data.js

fix issue with async.waterfall, only pass result if its not undefined

* feat: add callbackify to redis/psql

* feat: psql use await

* fix: redis 🌋

* feat: less returns

* feat: more await rewrite

* fix: redis tests

* feat: convert sortedSetAdd

rewrite psql transaction to async/await

* feat: 🐶

* feat: test

* feat: log client and query

* feat: log bind

* feat: more logs

* feat: more logs

* feat: check perform

* feat: dont callbackify transaction

* feat: remove logs

* fix: main functions

* feat: more logs

* fix: increment

* fix: rename

* feat: remove cls

* fix: remove console.log

* feat: add deprecation message to .async usage

* feat: update more dbal methods

* fix: redis :voodoo:

* feat:  fix redis zrem, convert setObject

* feat: upgrade getObject methods

* fix: psql getObjectField

* fix: redis tests

* feat: getObjectKeys

* feat: getObjectValues

* feat: isObjectField

* fix: add missing return

* feat: delObjectField

* feat: incrObjectField

* fix: add missing await

* feat: remove exposed helpers

* feat: list methods

* feat: flush/empty

* feat: delete

* fix: redis delete all

* feat: get/set

* feat: incr/rename

* feat: type

* feat: expire

* feat: setAdd

* feat: setRemove

* feat: isSetMember

* feat: getSetMembers

* feat: setCount, setRemoveRandom

* feat: zcard,zcount

* feat: sortedSetRank

* feat: isSortedSetMember

* feat: zincrby

* feat: sortedSetLex

* feat: processSortedSet

* fix: add mising await

* feat: debug psql

* fix: psql test

* fix: test

* fix: another test

* fix: test fix

* fix: psql tests

* feat: remove logs

* feat: user arrow func

use builtin async promises

* feat: topic bookmarks

* feat: topic.delete

* feat: topic.restore

* feat: topics.purge

* feat: merge

* feat: suggested

* feat: topics/user.js

* feat: topics modules

* feat: topics/follow

* fix: deprecation msg

* feat: fork

* feat: topics/posts

* feat: sorted/recent

* feat: topic/teaser

* feat: topics/tools

* feat: topics/unread

* feat: add back node versions

disable deprecation notice
wrap async controllers in try/catch

* feat: use db directly

* feat: promisify in place

* fix: redis/psql

* feat: deprecation message

logs for psql

* feat: more logs

* feat: more logs

* feat: logs again

* feat: more logs

* fix: call release

* feat: restore travis, remove logs

* fix: loops

* feat: remove .async. usage
2019-07-09 12:46:49 -04:00

224 lines
6.1 KiB
JavaScript

'use strict';
var _ = require('lodash');
var async = require('async');
var winston = require('winston');
var nconf = require('nconf');
var semver = require('semver');
var session = require('express-session');
var redis = require('redis');
var redisClient;
var redisModule = module.exports;
redisModule.questions = [
{
name: 'redis:host',
description: 'Host IP or address of your Redis instance',
default: nconf.get('redis:host') || '127.0.0.1',
},
{
name: 'redis:port',
description: 'Host port of your Redis instance',
default: nconf.get('redis:port') || 6379,
},
{
name: 'redis:password',
description: 'Password of your Redis database',
hidden: true,
default: nconf.get('redis:password') || '',
before: function (value) { value = value || nconf.get('redis:password') || ''; return value; },
},
{
name: 'redis:database',
description: 'Which database to use (0..n)',
default: nconf.get('redis:database') || 0,
},
];
redisModule.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;
};
redisModule.init = function (callback) {
callback = callback || function () { };
redisClient = redisModule.connect(nconf.get('redis'), function (err) {
if (err) {
winston.error('NodeBB could not connect to your Redis database. Redis returned the following error', err);
return callback(err);
}
redisModule.client = redisClient;
require('./redis/promisify')(redisClient);
require('./redis/main')(redisClient, redisModule);
require('./redis/hash')(redisClient, redisModule);
require('./redis/sets')(redisClient, redisModule);
require('./redis/sorted')(redisClient, redisModule);
require('./redis/list')(redisClient, redisModule);
require('./redis/transaction')(redisClient, redisModule);
redisModule.async = require('../promisify')(redisModule, ['client', 'sessionStore', 'connect']);
callback();
});
};
redisModule.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 = redisModule.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;
};
redisModule.createSessionStore = function (options, callback) {
const meta = require('../meta');
const sessionStore = require('connect-redis')(session);
const client = redisModule.connect(options);
const store = new sessionStore({
client: client,
ttl: meta.getSessionTTLSeconds(),
});
if (typeof callback === 'function') {
callback(null, store);
}
};
redisModule.createIndices = function (callback) {
setImmediate(callback);
};
redisModule.checkCompatibility = function (callback) {
async.waterfall([
function (next) {
redisModule.info(redisModule.client, next);
},
function (info, next) {
redisModule.checkCompatibilityVersion(info.redis_version, next);
},
], callback);
};
redisModule.checkCompatibilityVersion = function (version, callback) {
if (semver.lt(version, '2.8.9')) {
return callback(new Error('Your Redis version is not new enough to support NodeBB, please upgrade Redis to v2.8.9 or higher.'));
}
callback();
};
redisModule.close = function (callback) {
callback = callback || function () {};
redisClient.quit(function (err) {
callback(err);
});
};
redisModule.info = function (cxn, callback) {
async.waterfall([
function (next) {
if (cxn) {
return setImmediate(next, null, cxn);
}
redisModule.connect(nconf.get('redis'), next);
},
function (cxn, next) {
redisModule.client = redisModule.client || cxn;
cxn.info(next);
},
function (data, next) {
var lines = data.toString().split('\r\n').sort();
var redisData = {};
lines.forEach(function (line) {
var parts = line.split(':');
if (parts[1]) {
redisData[parts[0]] = parts[1];
}
});
const keyInfo = redisData['db' + nconf.get('redis:database')];
if (keyInfo) {
redisData.keys = keyInfo.split(',')[0].replace('keys=', '');
redisData.expires = keyInfo.split(',')[1].replace('expires=', '');
redisData.avg_ttl = keyInfo.split(',')[2].replace('avg_ttl=', '');
}
redisData.instantaneous_input = (redisData.instantaneous_input_kbps / 1024).toFixed(3);
redisData.instantaneous_output = (redisData.instantaneous_output_kbps / 1024).toFixed(3);
redisData.total_net_input = (redisData.total_net_input_bytes / (1024 * 1024 * 1024)).toFixed(3);
redisData.total_net_output = (redisData.total_net_output_bytes / (1024 * 1024 * 1024)).toFixed(3);
redisData.used_memory_human = (redisData.used_memory / (1024 * 1024 * 1024)).toFixed(3);
redisData.raw = JSON.stringify(redisData, null, 4);
redisData.redis = true;
next(null, redisData);
},
], callback);
};
redisModule.socketAdapter = function () {
var redisAdapter = require('socket.io-redis');
var pub = redisModule.connect(nconf.get('redis'));
var sub = redisModule.connect(nconf.get('redis'));
return redisAdapter({
key: 'db:' + nconf.get('redis:database') + ':adapter_key',
pubClient: pub,
subClient: sub,
});
};