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
This commit is contained in:
Barış Soner Uşaklı
2020-07-07 20:13:14 -04:00
parent 8853cd1aa5
commit c2ca02dfc7
10 changed files with 34 additions and 36 deletions

View File

@@ -308,7 +308,7 @@ postgresModule.createSessionStore = function (options, callback) {
const store = new sessionStore({ const store = new sessionStore({
pool: db, pool: db,
ttl: meta.getSessionTTLSeconds(), ttl: meta.getSessionTTLSeconds(),
pruneSessionInterval: nconf.get('isPrimary') === 'true' ? 60 : false, pruneSessionInterval: nconf.get('isPrimary') ? 60 : false,
}); });
callback(null, store); callback(null, store);
} }
@@ -317,7 +317,7 @@ postgresModule.createSessionStore = function (options, callback) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
if (nconf.get('isPrimary') !== 'true') { if (!nconf.get('isPrimary')) {
return done(db); return done(db);
} }
db.query(` db.query(`

View File

@@ -38,7 +38,7 @@ Meta.userOrGroupExists = async function (slug) {
return userExists || groupExists; return userExists || groupExists;
}; };
if (nconf.get('isPrimary') === 'true') { if (nconf.get('isPrimary')) {
pubsub.on('meta:restart', function (data) { pubsub.on('meta:restart', function (data) {
if (data.hostname !== os.hostname()) { if (data.hostname !== os.hostname()) {
restart(); restart();

View File

@@ -116,7 +116,7 @@ Plugins.reload = async function () {
} }
// If some plugins are incompatible, throw the warning here // 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(''); 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.'); 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) { for (var x = 0, numPlugins = Plugins.versionWarning.length; x < numPlugins; x += 1) {

View File

@@ -41,7 +41,7 @@ if (process.platform === 'win32') {
} }
module.exports = function (Plugins) { module.exports = function (Plugins) {
if (nconf.get('isPrimary') === 'true') { if (nconf.get('isPrimary')) {
pubsub.on('plugins:toggleInstall', function (data) { pubsub.on('plugins:toggleInstall', function (data) {
if (data.hostname !== os.hostname()) { if (data.hostname !== os.hostname()) {
toggleInstall(data.id, data.version); toggleInstall(data.id, data.version);

View File

@@ -54,14 +54,22 @@ function loadConfig(configFile) {
upload_path: 'public/uploads', upload_path: 'public/uploads',
views_dir: path.join(dirname, 'build/public/templates'), views_dir: path.join(dirname, 'build/public/templates'),
version: pkg.version, version: pkg.version,
isCluster: false,
isPrimary: true,
jobsDisabled: false,
}); });
if (!nconf.get('isCluster')) { // Explicitly cast as Bool, loader.js passes in isCluster as string 'true'/'false'
nconf.set('isPrimary', 'true'); var castAsBool = ['isCluster', 'isPrimary', 'jobsDisabled'];
nconf.set('isCluster', 'false'); 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');
} }
var isPrimary = nconf.get('isPrimary'); });
nconf.set('isPrimary', isPrimary === undefined ? 'true' : isPrimary); nconf.stores.env.readOnly = true;
nconf.set('runJobs', nconf.get('isPrimary') && !nconf.get('jobsDisabled'));
// Ensure themes_path is a full filepath // Ensure themes_path is a full filepath
nconf.set('themes_path', path.resolve(dirname, nconf.get('themes_path'))); 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_path', path.resolve(nconf.get('base_dir'), nconf.get('upload_path')));
nconf.set('upload_url', '/assets/uploads'); 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 // nconf defaults, if not set in config
if (!nconf.get('sessionKey')) { if (!nconf.get('sessionKey')) {

View File

@@ -14,7 +14,7 @@ function get() {
var pubsub; var pubsub;
if (nconf.get('isCluster') === 'false') { if (!nconf.get('isCluster')) {
if (noCluster) { if (noCluster) {
real = noCluster; real = noCluster;
return real; return real;

View File

@@ -27,6 +27,7 @@ Sockets.init = function (server) {
path: nconf.get('relative_path') + '/socket.io', path: nconf.get('relative_path') + '/socket.io',
}); });
if (nconf.get('isCluster')) {
if (nconf.get('singleHostCluster')) { if (nconf.get('singleHostCluster')) {
io.adapter(require('./single-host-cluster')); io.adapter(require('./single-host-cluster'));
} else if (nconf.get('redis')) { } else if (nconf.get('redis')) {
@@ -34,6 +35,7 @@ Sockets.init = function (server) {
} else { } else {
io.adapter(db.socketAdapter()); io.adapter(db.socketAdapter());
} }
}
io.use(socketioWildcard); io.use(socketioWildcard);
io.use(authorize); io.use(authorize);

View File

@@ -79,7 +79,7 @@ async function runUpgrades() {
} }
function printStartupInfo() { function printStartupInfo() {
if (nconf.get('isPrimary') === 'true') { if (nconf.get('isPrimary')) {
winston.info('Initializing NodeBB v%s %s', nconf.get('version'), nconf.get('url')); winston.info('Initializing NodeBB v%s %s', nconf.get('version'), nconf.get('url'));
const host = nconf.get(nconf.get('database') + ':host'); const host = nconf.get(nconf.get('database') + ':host');

View File

@@ -39,9 +39,9 @@ const urlObject = url.parse(nconf.get('url'));
const relativePath = urlObject.pathname !== '/' ? urlObject.pathname : ''; const relativePath = urlObject.pathname !== '/' ? urlObject.pathname : '';
nconf.set('relative_path', relativePath); nconf.set('relative_path', relativePath);
if (!nconf.get('isCluster')) { if (nconf.get('isCluster') === undefined) {
nconf.set('isPrimary', 'true'); nconf.set('isPrimary', true);
nconf.set('isCluster', 'true'); nconf.set('isCluster', true);
} }
const dbType = nconf.get('database'); const dbType = nconf.get('database');

View File

@@ -8,7 +8,7 @@ var pubsub = require('../src/pubsub');
describe('pubsub', function () { describe('pubsub', function () {
it('should use the plain event emitter', function (done) { it('should use the plain event emitter', function (done) {
nconf.set('isCluster', 'false'); nconf.set('isCluster', false);
pubsub.reset(); pubsub.reset();
pubsub.on('testEvent', function (message) { pubsub.on('testEvent', function (message) {
assert.equal(message.foo, 1); assert.equal(message.foo, 1);
@@ -21,7 +21,7 @@ describe('pubsub', function () {
it('should use same event emitter', function (done) { it('should use same event emitter', function (done) {
pubsub.on('dummyEvent', function (message) { pubsub.on('dummyEvent', function (message) {
assert.equal(message.foo, 2); assert.equal(message.foo, 2);
nconf.set('isCluster', 'true'); nconf.set('isCluster', true);
pubsub.removeAllListeners('dummyEvent'); pubsub.removeAllListeners('dummyEvent');
pubsub.reset(); pubsub.reset();
done(); done();