Skip to content

Commit

Permalink
fix: #8474
Browse files Browse the repository at this point in the history
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
  • Loading branch information
barisusakli committed Jul 8, 2020
1 parent 8853cd1 commit c2ca02d
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/database/postgres.js
Expand Up @@ -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);
}
Expand All @@ -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(`
Expand Down
2 changes: 1 addition & 1 deletion src/meta/index.js
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/index.js
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/install.js
Expand Up @@ -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);
Expand Down
32 changes: 14 additions & 18 deletions src/prestart.js
Expand Up @@ -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')));
Expand All @@ -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')) {
Expand Down
2 changes: 1 addition & 1 deletion src/pubsub.js
Expand Up @@ -14,7 +14,7 @@ function get() {

var pubsub;

if (nconf.get('isCluster') === 'false') {
if (!nconf.get('isCluster')) {
if (noCluster) {
real = noCluster;
return real;
Expand Down
14 changes: 8 additions & 6 deletions src/socket.io/index.js
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/start.js
Expand Up @@ -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');
Expand Down
6 changes: 3 additions & 3 deletions test/mocks/databasemock.js
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions test/pubsub.js
Expand Up @@ -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);
Expand All @@ -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();
Expand Down

0 comments on commit c2ca02d

Please sign in to comment.