refactor: async/await

v1.18.x
Barış Soner Uşaklı 5 years ago
parent 91e8e3905c
commit 02d38caf47

@ -9,7 +9,7 @@ if (!databaseName) {
process.exit(); process.exit();
} }
var primaryDB = require('./' + databaseName); const primaryDB = require('./' + databaseName);
primaryDB.parseIntFields = function (data, intFields, requestedFields) { primaryDB.parseIntFields = function (data, intFields, requestedFields) {
intFields.forEach((field) => { intFields.forEach((field) => {
@ -19,7 +19,7 @@ primaryDB.parseIntFields = function (data, intFields, requestedFields) {
}); });
}; };
primaryDB.initSessionStore = function (callback) { primaryDB.initSessionStore = async function () {
const sessionStoreConfig = nconf.get('session_store') || nconf.get('redis') || nconf.get(databaseName); const sessionStoreConfig = nconf.get('session_store') || nconf.get('redis') || nconf.get(databaseName);
let sessionStoreDB = primaryDB; let sessionStoreDB = primaryDB;
@ -30,13 +30,7 @@ primaryDB.initSessionStore = function (callback) {
sessionStoreDB = require('./redis'); sessionStoreDB = require('./redis');
} }
sessionStoreDB.createSessionStore(sessionStoreConfig, function (err, sessionStore) { primaryDB.sessionStore = await sessionStoreDB.createSessionStore(sessionStoreConfig);
if (err) {
return callback(err);
}
primaryDB.sessionStore = sessionStore;
callback();
});
}; };
module.exports = primaryDB; module.exports = primaryDB;

@ -1,90 +1,75 @@
'use strict'; 'use strict';
var async = require('async'); const nconf = require('nconf');
var nconf = require('nconf'); const url = require('url');
var url = require('url'); const winston = require('winston');
var winston = require('winston');
var start = module.exports; const start = module.exports;
start.start = function () { start.start = async function () {
var db = require('./database'); const db = require('./database');
setupConfigs(); setupConfigs();
printStartupInfo(); printStartupInfo();
addProcessHandlers(); addProcessHandlers();
try {
await db.init();
const meta = require('./meta');
await Promise.all([
db.checkCompatibility(),
meta.configs.init(),
require('./upgrade').check(),
]);
if (nconf.get('dep-check') === undefined || nconf.get('dep-check') !== false) {
await meta.dependencies.check();
} else {
winston.warn('[init] Dependency checking skipped!');
}
async.waterfall([ await db.initSessionStore();
function (next) {
db.init(next); const webserver = require('./webserver');
}, require('./socket.io').init(webserver.server);
function (next) {
var meta = require('./meta'); if (nconf.get('runJobs')) {
async.parallel([ require('./notifications').startJobs();
async.apply(db.checkCompatibility), require('./user').startJobs();
async.apply(meta.configs.init), require('./plugins').startJobs();
function (next) {
if (nconf.get('dep-check') === undefined || nconf.get('dep-check') !== false) {
meta.dependencies.check(next);
} else {
winston.warn('[init] Dependency checking skipped!');
setImmediate(next);
}
},
function (next) {
require('./upgrade').check(next);
},
], function (err) {
next(err);
});
},
function (next) {
db.initSessionStore(next);
},
function (next) {
var webserver = require('./webserver');
require('./socket.io').init(webserver.server);
if (nconf.get('runJobs')) {
require('./notifications').startJobs();
require('./user').startJobs();
require('./plugins').startJobs();
}
webserver.listen(next);
},
], function (err) {
if (err) {
switch (err.message) {
case 'schema-out-of-date':
winston.error('Your NodeBB schema is out-of-date. Please run the following command to bring your dataset up to spec:');
winston.error(' ./nodebb upgrade');
break;
case 'dependencies-out-of-date':
winston.error('One or more of NodeBB\'s dependent packages are out-of-date. Please run the following command to update them:');
winston.error(' ./nodebb upgrade');
break;
case 'dependencies-missing':
winston.error('One or more of NodeBB\'s dependent packages are missing. Please run the following command to update them:');
winston.error(' ./nodebb upgrade');
break;
default:
winston.error(err);
break;
}
// Either way, bad stuff happened. Abort start.
process.exit();
} }
await webserver.listen();
if (process.send) { if (process.send) {
process.send({ process.send({
action: 'listening', action: 'listening',
}); });
} }
}); } catch (err) {
switch (err.message) {
case 'schema-out-of-date':
winston.error('Your NodeBB schema is out-of-date. Please run the following command to bring your dataset up to spec:');
winston.error(' ./nodebb upgrade');
break;
case 'dependencies-out-of-date':
winston.error('One or more of NodeBB\'s dependent packages are out-of-date. Please run the following command to update them:');
winston.error(' ./nodebb upgrade');
break;
case 'dependencies-missing':
winston.error('One or more of NodeBB\'s dependent packages are missing. Please run the following command to update them:');
winston.error(' ./nodebb upgrade');
break;
default:
winston.error(err);
break;
}
// Either way, bad stuff happened. Abort start.
process.exit();
}
}; };
function setupConfigs() { function setupConfigs() {
@ -93,8 +78,8 @@ function setupConfigs() {
nconf.set('sessionKey', 'express.sid'); nconf.set('sessionKey', 'express.sid');
} }
// Parse out the relative_url and other goodies from the configured URL // Parse out the relative_url and other goodies from the configured URL
var urlObject = url.parse(nconf.get('url')); const urlObject = url.parse(nconf.get('url'));
var relativePath = urlObject.pathname !== '/' ? urlObject.pathname.replace(/\/+$/, '') : ''; const relativePath = urlObject.pathname !== '/' ? urlObject.pathname.replace(/\/+$/, '') : '';
nconf.set('base_url', urlObject.protocol + '//' + urlObject.host); nconf.set('base_url', urlObject.protocol + '//' + urlObject.host);
nconf.set('secure', urlObject.protocol === 'https:'); nconf.set('secure', urlObject.protocol === 'https:');
nconf.set('use_port', !!urlObject.port); nconf.set('use_port', !!urlObject.port);
@ -106,8 +91,8 @@ function printStartupInfo() {
if (nconf.get('isPrimary') === 'true') { if (nconf.get('isPrimary') === 'true') {
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'));
var host = nconf.get(nconf.get('database') + ':host'); const host = nconf.get(nconf.get('database') + ':host');
var storeLocation = host ? 'at ' + host + (!host.includes('/') ? ':' + nconf.get(nconf.get('database') + ':port') : '') : ''; const storeLocation = host ? 'at ' + host + (!host.includes('/') ? ':' + nconf.get(nconf.get('database') + ':port') : '') : '';
winston.verbose('* using %s store %s', nconf.get('database'), storeLocation); winston.verbose('* using %s store %s', nconf.get('database'), storeLocation);
winston.verbose('* using themes stored in: %s', nconf.get('themes_path')); winston.verbose('* using themes stored in: %s', nconf.get('themes_path'));
@ -138,27 +123,19 @@ function restart() {
} }
} }
function shutdown(code) { async function shutdown(code) {
winston.info('[app] Shutdown (SIGTERM/SIGINT) Initialised.'); winston.info('[app] Shutdown (SIGTERM/SIGINT) Initialised.');
async.waterfall([ try {
function (next) { await require('./webserver').destroy();
require('./webserver').destroy(next); winston.info('[app] Web server closed to connections.');
}, await require('./analytics').writeData();
function (next) { winston.info('[app] Live analytics saved.');
winston.info('[app] Web server closed to connections.'); await require('./database').close();
require('./analytics').writeData(next);
},
function (next) {
winston.info('[app] Live analytics saved.');
require('./database').close(next);
},
], function (err) {
if (err) {
winston.error(err);
return process.exit(code || 0);
}
winston.info('[app] Database connection closed.'); winston.info('[app] Database connection closed.');
winston.info('[app] Shutdown complete.'); winston.info('[app] Shutdown complete.');
process.exit(code || 0); process.exit(code || 0);
}); } catch (err) {
winston.error(err);
return process.exit(code || 0);
}
} }

@ -125,7 +125,7 @@ describe('Build', function (done) {
before(function (done) { before(function (done) {
async.parallel([ async.parallel([
async.apply(rimraf, path.join(__dirname, '../build/public')), async.apply(rimraf, path.join(__dirname, '../build/public')),
async.apply(db.activatePlugin, 'nodebb-plugin-markdown'), async.apply(db.sortedSetAdd, 'plugins:active', Date.now(), 'nodebb-plugin-markdown'),
], done); ], done);
}); });

@ -7,15 +7,15 @@
require('../../require-main'); require('../../require-main');
var async = require('async'); const path = require('path');
var path = require('path'); const nconf = require('nconf');
var nconf = require('nconf'); const url = require('url');
var url = require('url'); const util = require('util');
global.env = process.env.TEST_ENV || 'production'; global.env = process.env.TEST_ENV || 'production';
var winston = require('winston'); const winston = require('winston');
var packageInfo = require('../../package'); const packageInfo = require('../../package');
winston.add(new winston.transports.Console({ winston.add(new winston.transports.Console({
format: winston.format.combine( format: winston.format.combine(
@ -33,8 +33,8 @@ nconf.defaults({
relative_path: '', relative_path: '',
}); });
var urlObject = url.parse(nconf.get('url')); const urlObject = url.parse(nconf.get('url'));
var 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')) {
@ -42,9 +42,9 @@ if (!nconf.get('isCluster')) {
nconf.set('isCluster', 'true'); nconf.set('isCluster', 'true');
} }
var dbType = nconf.get('database'); const dbType = nconf.get('database');
var testDbConfig = nconf.get('test_database'); const testDbConfig = nconf.get('test_database');
var productionDbConfig = nconf.get(dbType); const productionDbConfig = nconf.get(dbType);
if (!testDbConfig) { if (!testDbConfig) {
const errorText = 'test_database is not defined'; const errorText = 'test_database is not defined';
@ -100,161 +100,127 @@ nconf.set(dbType, testDbConfig);
winston.info('database config %s', dbType, testDbConfig); winston.info('database config %s', dbType, testDbConfig);
winston.info('environment ' + global.env); winston.info('environment ' + global.env);
var db = require('../../src/database'); const db = require('../../src/database');
module.exports = db; module.exports = db;
before(function (done) { before(async function () {
this.timeout(30000); this.timeout(30000);
async.series([ await db.init();
function (next) { await db.createIndices();
db.init(next); await setupMockDefaults();
}, await db.initSessionStore();
function (next) {
db.createIndices(next); const meta = require('../../src/meta');
},
function (next) { // nconf defaults, if not set in config
setupMockDefaults(next); if (!nconf.get('sessionKey')) {
}, nconf.set('sessionKey', 'express.sid');
function (next) { }
db.initSessionStore(next); // Parse out the relative_url and other goodies from the configured URL
}, const urlObject = url.parse(nconf.get('url'));
function (next) { const relativePath = urlObject.pathname !== '/' ? urlObject.pathname : '';
var meta = require('../../src/meta'); nconf.set('base_url', urlObject.protocol + '//' + urlObject.host);
nconf.set('secure', urlObject.protocol === 'https:');
// nconf defaults, if not set in config nconf.set('use_port', !!urlObject.port);
if (!nconf.get('sessionKey')) { nconf.set('relative_path', relativePath);
nconf.set('sessionKey', 'express.sid'); nconf.set('port', urlObject.port || nconf.get('port') || (nconf.get('PORT_ENV_VAR') ? nconf.get(nconf.get('PORT_ENV_VAR')) : false) || 4567);
} nconf.set('upload_path', path.join(nconf.get('base_dir'), nconf.get('upload_path')));
// Parse out the relative_url and other goodies from the configured URL nconf.set('upload_url', '/assets/uploads');
var urlObject = url.parse(nconf.get('url'));
var relativePath = urlObject.pathname !== '/' ? urlObject.pathname : ''; nconf.set('core_templates_path', path.join(__dirname, '../../src/views'));
nconf.set('base_url', urlObject.protocol + '//' + urlObject.host); nconf.set('base_templates_path', path.join(nconf.get('themes_path'), 'nodebb-theme-persona/templates'));
nconf.set('secure', urlObject.protocol === 'https:'); nconf.set('theme_templates_path', meta.config['theme:templates'] ? path.join(nconf.get('themes_path'), meta.config['theme:id'], meta.config['theme:templates']) : nconf.get('base_templates_path'));
nconf.set('use_port', !!urlObject.port); nconf.set('theme_config', path.join(nconf.get('themes_path'), 'nodebb-theme-persona', 'theme.json'));
nconf.set('relative_path', relativePath); nconf.set('bcrypt_rounds', 1);
nconf.set('port', urlObject.port || nconf.get('port') || (nconf.get('PORT_ENV_VAR') ? nconf.get(nconf.get('PORT_ENV_VAR')) : false) || 4567);
nconf.set('upload_path', path.join(nconf.get('base_dir'), nconf.get('upload_path'))); nconf.set('version', packageInfo.version);
nconf.set('upload_url', '/assets/uploads');
await meta.dependencies.check();
nconf.set('core_templates_path', path.join(__dirname, '../../src/views'));
nconf.set('base_templates_path', path.join(nconf.get('themes_path'), 'nodebb-theme-persona/templates')); const webserver = require('../../src/webserver');
nconf.set('theme_templates_path', meta.config['theme:templates'] ? path.join(nconf.get('themes_path'), meta.config['theme:id'], meta.config['theme:templates']) : nconf.get('base_templates_path')); const sockets = require('../../src/socket.io');
nconf.set('theme_config', path.join(nconf.get('themes_path'), 'nodebb-theme-persona', 'theme.json')); sockets.init(webserver.server);
nconf.set('bcrypt_rounds', 1);
require('../../src/notifications').startJobs();
nconf.set('version', packageInfo.version); require('../../src/user').startJobs();
meta.dependencies.check(next); await webserver.listen();
},
function (next) {
var webserver = require('../../src/webserver');
var sockets = require('../../src/socket.io');
sockets.init(webserver.server);
require('../../src/notifications').startJobs();
require('../../src/user').startJobs();
webserver.listen(next);
},
], done);
// Iterate over all of the test suites/contexts // Iterate over all of the test suites/contexts
this.test.parent.suites.forEach(function (suite) { this.test.parent.suites.forEach(function (suite) {
// Attach an afterAll listener that resets the defaults // Attach an afterAll listener that resets the defaults
suite.afterAll(function (done) { suite.afterAll(async function () {
setupMockDefaults(done); await setupMockDefaults();
}); });
}); });
}); });
function setupMockDefaults(callback) { async function setupMockDefaults() {
var meta = require('../../src/meta'); const meta = require('../../src/meta');
await db.emptydb();
async.series([
function (next) { require('../../src/groups').resetCache();
db.emptydb(next); require('../../src/posts/cache').reset();
}, require('../../src/cache').reset();
function (next) { winston.info('test_database flushed');
var groups = require('../../src/groups'); await setupDefaultConfigs(meta);
groups.resetCache(); await giveDefaultGlobalPrivileges();
var postCache = require('../../src/posts/cache'); await meta.configs.init();
postCache.reset(); meta.config.postDelay = 0;
var localCache = require('../../src/cache'); meta.config.initialPostDelay = 0;
localCache.reset(); meta.config.newbiePostDelay = 0;
next();
}, await enableDefaultPlugins();
function (next) {
winston.info('test_database flushed'); await meta.themes.set({
setupDefaultConfigs(meta, next); type: 'local',
}, id: 'nodebb-theme-persona',
function (next) { });
giveDefaultGlobalPrivileges(next);
}, const rimraf = util.promisify(require('rimraf'));
function (next) { await rimraf('test/uploads');
meta.configs.init(next);
}, const mkdirp = util.promisify(require('mkdirp'));
function (next) {
meta.config.postDelay = 0; const folders = [
meta.config.initialPostDelay = 0; 'test/uploads',
meta.config.newbiePostDelay = 0; 'test/uploads/category',
'test/uploads/files',
enableDefaultPlugins(next); 'test/uploads/system',
}, 'test/uploads/sounds',
function (next) { 'test/uploads/profile',
meta.themes.set({ ];
type: 'local', for (const folder of folders) {
id: 'nodebb-theme-persona', /* eslint-disable no-await-in-loop */
}, next); await mkdirp(folder);
}, }
function (next) {
var rimraf = require('rimraf');
rimraf('test/uploads', next);
},
function (next) {
var mkdirp = require('mkdirp');
async.eachSeries([
'test/uploads',
'test/uploads/category',
'test/uploads/files',
'test/uploads/system',
'test/uploads/sounds',
'test/uploads/profile',
], mkdirp, next);
},
], callback);
} }
db.setupMockDefaults = setupMockDefaults; db.setupMockDefaults = setupMockDefaults;
function setupDefaultConfigs(meta, next) { async function setupDefaultConfigs(meta) {
winston.info('Populating database with default configs, if not already set...\n'); winston.info('Populating database with default configs, if not already set...\n');
var defaults = require(path.join(nconf.get('base_dir'), 'install/data/defaults.json')); const defaults = require(path.join(nconf.get('base_dir'), 'install/data/defaults.json'));
defaults.eventLoopCheckEnabled = 0; defaults.eventLoopCheckEnabled = 0;
defaults.minimumPasswordStrength = 0; defaults.minimumPasswordStrength = 0;
meta.configs.setOnEmpty(defaults, next); await meta.configs.setOnEmpty(defaults);
} }
function giveDefaultGlobalPrivileges(next) { async function giveDefaultGlobalPrivileges() {
var privileges = require('../../src/privileges'); const privileges = require('../../src/privileges');
async.waterfall([ await privileges.global.give([
function (next) { 'chat', 'upload:post:image', 'signature', 'search:content',
privileges.global.give([ 'search:users', 'search:tags', 'local:login', 'view:users', 'view:tags', 'view:groups',
'chat', 'upload:post:image', 'signature', 'search:content', ], 'registered-users');
'search:users', 'search:tags', 'local:login', 'view:users', 'view:tags', 'view:groups', await privileges.global.give([
], 'registered-users', next); 'view:users', 'view:tags', 'view:groups',
}, ], 'guests');
function (next) {
privileges.global.give([
'view:users', 'view:tags', 'view:groups',
], 'guests', next);
},
], next);
} }
function enableDefaultPlugins(callback) { async function enableDefaultPlugins() {
winston.info('Enabling default plugins\n'); winston.info('Enabling default plugins\n');
var defaultEnabled = [ const defaultEnabled = [
'nodebb-plugin-dbsearch', 'nodebb-plugin-dbsearch',
'nodebb-plugin-soundpack-default', 'nodebb-plugin-soundpack-default',
'nodebb-widget-essentials', 'nodebb-widget-essentials',
@ -262,9 +228,5 @@ function enableDefaultPlugins(callback) {
winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled); winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled);
db.sortedSetAdd('plugins:active', Object.keys(defaultEnabled), defaultEnabled, callback); await db.sortedSetAdd('plugins:active', Object.keys(defaultEnabled), defaultEnabled);
} }
db.activatePlugin = function (id, callback) {
db.sortedSetAdd('plugins:active', Date.now(), id, callback);
};

Loading…
Cancel
Save