From c2ca02dfc7f78726b0a7a4aa586f5bf6c8b984ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Tue, 7 Jul 2020 20:13:14 -0400 Subject: [PATCH] fix: #8474 make isPrimary and isCluster always booleans they were strings when using ./nodebb start and boolean if they were in config.json and started with node app.js --- src/database/postgres.js | 4 ++-- src/meta/index.js | 2 +- src/plugins/index.js | 2 +- src/plugins/install.js | 2 +- src/prestart.js | 32 ++++++++++++++------------------ src/pubsub.js | 2 +- src/socket.io/index.js | 14 ++++++++------ src/start.js | 2 +- test/mocks/databasemock.js | 6 +++--- test/pubsub.js | 4 ++-- 10 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/database/postgres.js b/src/database/postgres.js index 7a642301f1..55b7ce99b0 100644 --- a/src/database/postgres.js +++ b/src/database/postgres.js @@ -308,7 +308,7 @@ postgresModule.createSessionStore = function (options, callback) { const store = new sessionStore({ pool: db, ttl: meta.getSessionTTLSeconds(), - pruneSessionInterval: nconf.get('isPrimary') === 'true' ? 60 : false, + pruneSessionInterval: nconf.get('isPrimary') ? 60 : false, }); callback(null, store); } @@ -317,7 +317,7 @@ postgresModule.createSessionStore = function (options, callback) { if (err) { return callback(err); } - if (nconf.get('isPrimary') !== 'true') { + if (!nconf.get('isPrimary')) { return done(db); } db.query(` diff --git a/src/meta/index.js b/src/meta/index.js index 870a8eaa58..4dda954001 100644 --- a/src/meta/index.js +++ b/src/meta/index.js @@ -38,7 +38,7 @@ Meta.userOrGroupExists = async function (slug) { return userExists || groupExists; }; -if (nconf.get('isPrimary') === 'true') { +if (nconf.get('isPrimary')) { pubsub.on('meta:restart', function (data) { if (data.hostname !== os.hostname()) { restart(); diff --git a/src/plugins/index.js b/src/plugins/index.js index 9d5e93abce..5a89ce6fe7 100644 --- a/src/plugins/index.js +++ b/src/plugins/index.js @@ -116,7 +116,7 @@ Plugins.reload = async function () { } // If some plugins are incompatible, throw the warning here - if (Plugins.versionWarning.length && nconf.get('isPrimary') === 'true') { + if (Plugins.versionWarning.length && nconf.get('isPrimary')) { console.log(''); winston.warn('[plugins/load] The following plugins may not be compatible with your version of NodeBB. This may cause unintended behaviour or crashing. In the event of an unresponsive NodeBB caused by this plugin, run `./nodebb reset -p PLUGINNAME` to disable it.'); for (var x = 0, numPlugins = Plugins.versionWarning.length; x < numPlugins; x += 1) { diff --git a/src/plugins/install.js b/src/plugins/install.js index dace50b10e..2fd7e01732 100644 --- a/src/plugins/install.js +++ b/src/plugins/install.js @@ -41,7 +41,7 @@ if (process.platform === 'win32') { } module.exports = function (Plugins) { - if (nconf.get('isPrimary') === 'true') { + if (nconf.get('isPrimary')) { pubsub.on('plugins:toggleInstall', function (data) { if (data.hostname !== os.hostname()) { toggleInstall(data.id, data.version); diff --git a/src/prestart.js b/src/prestart.js index d49126bd18..2f83fc8103 100644 --- a/src/prestart.js +++ b/src/prestart.js @@ -54,14 +54,22 @@ function loadConfig(configFile) { upload_path: 'public/uploads', views_dir: path.join(dirname, 'build/public/templates'), version: pkg.version, + isCluster: false, + isPrimary: true, + jobsDisabled: false, }); - if (!nconf.get('isCluster')) { - nconf.set('isPrimary', 'true'); - nconf.set('isCluster', 'false'); - } - var isPrimary = nconf.get('isPrimary'); - nconf.set('isPrimary', isPrimary === undefined ? 'true' : isPrimary); + // Explicitly cast as Bool, loader.js passes in isCluster as string 'true'/'false' + var castAsBool = ['isCluster', 'isPrimary', 'jobsDisabled']; + nconf.stores.env.readOnly = false; + castAsBool.forEach(function (prop) { + var value = nconf.get(prop); + if (value !== undefined) { + nconf.set(prop, typeof value === 'boolean' ? value : String(value).toLowerCase() === 'true'); + } + }); + nconf.stores.env.readOnly = true; + nconf.set('runJobs', nconf.get('isPrimary') && !nconf.get('jobsDisabled')); // Ensure themes_path is a full filepath nconf.set('themes_path', path.resolve(dirname, nconf.get('themes_path'))); @@ -71,18 +79,6 @@ function loadConfig(configFile) { nconf.set('upload_path', path.resolve(nconf.get('base_dir'), nconf.get('upload_path'))); nconf.set('upload_url', '/assets/uploads'); - // Explicitly cast 'jobsDisabled' as Bool - var castAsBool = ['jobsDisabled']; - nconf.stores.env.readOnly = false; - castAsBool.forEach(function (prop) { - var value = nconf.get(prop); - if (value) { - nconf.set(prop, typeof value === 'boolean' ? value : String(value).toLowerCase() === 'true'); - } - }); - nconf.stores.env.readOnly = true; - - nconf.set('runJobs', nconf.get('isPrimary') === 'true' && !nconf.get('jobsDisabled')); // nconf defaults, if not set in config if (!nconf.get('sessionKey')) { diff --git a/src/pubsub.js b/src/pubsub.js index 1df776cd2f..62c78246db 100644 --- a/src/pubsub.js +++ b/src/pubsub.js @@ -14,7 +14,7 @@ function get() { var pubsub; - if (nconf.get('isCluster') === 'false') { + if (!nconf.get('isCluster')) { if (noCluster) { real = noCluster; return real; diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 714727692c..01694533d4 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -27,12 +27,14 @@ Sockets.init = function (server) { path: nconf.get('relative_path') + '/socket.io', }); - if (nconf.get('singleHostCluster')) { - io.adapter(require('./single-host-cluster')); - } else if (nconf.get('redis')) { - io.adapter(require('../database/redis').socketAdapter()); - } else { - io.adapter(db.socketAdapter()); + if (nconf.get('isCluster')) { + if (nconf.get('singleHostCluster')) { + io.adapter(require('./single-host-cluster')); + } else if (nconf.get('redis')) { + io.adapter(require('../database/redis').socketAdapter()); + } else { + io.adapter(db.socketAdapter()); + } } io.use(socketioWildcard); diff --git a/src/start.js b/src/start.js index 062a601cc6..30636cb4d0 100644 --- a/src/start.js +++ b/src/start.js @@ -79,7 +79,7 @@ async function runUpgrades() { } function printStartupInfo() { - if (nconf.get('isPrimary') === 'true') { + if (nconf.get('isPrimary')) { winston.info('Initializing NodeBB v%s %s', nconf.get('version'), nconf.get('url')); const host = nconf.get(nconf.get('database') + ':host'); diff --git a/test/mocks/databasemock.js b/test/mocks/databasemock.js index 2a89f9772a..73e952e3b3 100644 --- a/test/mocks/databasemock.js +++ b/test/mocks/databasemock.js @@ -39,9 +39,9 @@ const urlObject = url.parse(nconf.get('url')); const relativePath = urlObject.pathname !== '/' ? urlObject.pathname : ''; nconf.set('relative_path', relativePath); -if (!nconf.get('isCluster')) { - nconf.set('isPrimary', 'true'); - nconf.set('isCluster', 'true'); +if (nconf.get('isCluster') === undefined) { + nconf.set('isPrimary', true); + nconf.set('isCluster', true); } const dbType = nconf.get('database'); diff --git a/test/pubsub.js b/test/pubsub.js index 873e252e2a..6a8e440159 100644 --- a/test/pubsub.js +++ b/test/pubsub.js @@ -8,7 +8,7 @@ var pubsub = require('../src/pubsub'); describe('pubsub', function () { it('should use the plain event emitter', function (done) { - nconf.set('isCluster', 'false'); + nconf.set('isCluster', false); pubsub.reset(); pubsub.on('testEvent', function (message) { assert.equal(message.foo, 1); @@ -21,7 +21,7 @@ describe('pubsub', function () { it('should use same event emitter', function (done) { pubsub.on('dummyEvent', function (message) { assert.equal(message.foo, 2); - nconf.set('isCluster', 'true'); + nconf.set('isCluster', true); pubsub.removeAllListeners('dummyEvent'); pubsub.reset(); done();