From 5bc1f5b4e8b83d64efd16d6f7a994bad893353b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sun, 31 Jan 2021 12:37:28 -0500 Subject: [PATCH] fix: #9231, fix redis pubsub connection regression from https://github.com/NodeBB/NodeBB/commit/fdfbc90255c9e5d5a1e83efada606f43c72fa22f --- src/database/redis.js | 6 ++--- src/database/redis/pubsub.js | 50 +++++++++++++++++++----------------- src/socket.io/index.js | 5 ++-- src/start.js | 3 ++- test/mocks/databasemock.js | 2 +- 5 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/database/redis.js b/src/database/redis.js index 276e67f1e2..ff71ee5afc 100644 --- a/src/database/redis.js +++ b/src/database/redis.js @@ -103,10 +103,10 @@ redisModule.info = async function (cxn) { return redisData; }; -redisModule.socketAdapter = function () { +redisModule.socketAdapter = async function () { const redisAdapter = require('socket.io-redis'); - const pub = connection.connect(nconf.get('redis')); - const sub = connection.connect(nconf.get('redis')); + const pub = await connection.connect(nconf.get('redis')); + const sub = await connection.connect(nconf.get('redis')); return redisAdapter({ key: 'db:' + nconf.get('redis:database') + ':adapter_key', pubClient: pub, diff --git a/src/database/redis/pubsub.js b/src/database/redis/pubsub.js index f699d21ff5..73d7c11b33 100644 --- a/src/database/redis/pubsub.js +++ b/src/database/redis/pubsub.js @@ -1,31 +1,35 @@ 'use strict'; -var nconf = require('nconf'); -var util = require('util'); -var winston = require('winston'); -var EventEmitter = require('events').EventEmitter; +const nconf = require('nconf'); +const util = require('util'); +const winston = require('winston'); +const EventEmitter = require('events').EventEmitter; const connection = require('./connection'); -var channelName; -var PubSub = function () { - var self = this; - var subClient = connection.connect(); - this.pubClient = connection.connect(); - +let channelName; +const PubSub = function () { + const self = this; channelName = 'db:' + nconf.get('redis:database') + ':pubsub_channel'; - subClient.subscribe(channelName); - - subClient.on('message', function (channel, message) { - if (channel !== channelName) { - return; - } - - try { - var msg = JSON.parse(message); - self.emit(msg.event, msg.data); - } catch (err) { - winston.error(err.stack); - } + + connection.connect().then(function (client) { + self.subClient = client; + self.subClient.subscribe(channelName); + self.subClient.on('message', function (channel, message) { + if (channel !== channelName) { + return; + } + + try { + var msg = JSON.parse(message); + self.emit(msg.event, msg.data); + } catch (err) { + winston.error(err.stack); + } + }); + }); + + connection.connect().then(function (client) { + self.pubClient = client; }); }; diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 1a099fd4e6..165f5213ef 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -16,7 +16,7 @@ const Namespaces = {}; const Sockets = module.exports; -Sockets.init = function (server) { +Sockets.init = async function (server) { requireModules(); const SocketIO = require('socket.io').Server; @@ -30,7 +30,8 @@ Sockets.init = function (server) { // io.adapter(require('./single-host-cluster')); // } else if (nconf.get('redis')) { if (nconf.get('redis')) { - io.adapter(require('../database/redis').socketAdapter()); + const adapter = await require('../database/redis').socketAdapter(); + io.adapter(adapter); } else { winston.warn('clustering detected, you should setup redis!'); } diff --git a/src/start.js b/src/start.js index 4cc23c90b9..a0640287fb 100644 --- a/src/start.js +++ b/src/start.js @@ -31,7 +31,8 @@ start.start = async function () { await db.initSessionStore(); const webserver = require('./webserver'); - require('./socket.io').init(webserver.server); + const sockets = require('./socket.io'); + await sockets.init(webserver.server); if (nconf.get('runJobs')) { require('./notifications').startJobs(); diff --git a/test/mocks/databasemock.js b/test/mocks/databasemock.js index 24990ee826..e51786258e 100644 --- a/test/mocks/databasemock.js +++ b/test/mocks/databasemock.js @@ -152,7 +152,7 @@ before(async function () { const webserver = require('../../src/webserver'); const sockets = require('../../src/socket.io'); - sockets.init(webserver.server); + await sockets.init(webserver.server); require('../../src/notifications').startJobs(); require('../../src/user').startJobs();