mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: async listen testSocket
This commit is contained in:
@@ -12,7 +12,6 @@ const app = express();
|
|||||||
app.renderAsync = util.promisify((tpl, data, callback) => app.render(tpl, data, callback));
|
app.renderAsync = util.promisify((tpl, data, callback) => app.render(tpl, data, callback));
|
||||||
let server;
|
let server;
|
||||||
const winston = require('winston');
|
const winston = require('winston');
|
||||||
const async = require('async');
|
|
||||||
const flash = require('connect-flash');
|
const flash = require('connect-flash');
|
||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
const cookieParser = require('cookie-parser');
|
const cookieParser = require('cookie-parser');
|
||||||
@@ -91,7 +90,7 @@ exports.listen = async function () {
|
|||||||
|
|
||||||
plugins.hooks.fire('action:nodebb.ready');
|
plugins.hooks.fire('action:nodebb.ready');
|
||||||
|
|
||||||
await util.promisify(listen)();
|
await listen();
|
||||||
};
|
};
|
||||||
|
|
||||||
async function initializeNodeBB() {
|
async function initializeNodeBB() {
|
||||||
@@ -227,8 +226,7 @@ function setupCookie() {
|
|||||||
return cookie;
|
return cookie;
|
||||||
}
|
}
|
||||||
|
|
||||||
function listen(callback) {
|
async function listen() {
|
||||||
callback = callback || function () { };
|
|
||||||
let port = nconf.get('port');
|
let port = nconf.get('port');
|
||||||
const isSocket = isNaN(port) && !Array.isArray(port);
|
const isSocket = isNaN(port) && !Array.isArray(port);
|
||||||
const socketPath = isSocket ? nconf.get('port') : '';
|
const socketPath = isSocket ? nconf.get('port') : '';
|
||||||
@@ -261,63 +259,59 @@ function listen(callback) {
|
|||||||
const args = isSocket ? [socketPath] : [port, bind_address];
|
const args = isSocket ? [socketPath] : [port, bind_address];
|
||||||
let oldUmask;
|
let oldUmask;
|
||||||
|
|
||||||
args.push((err) => {
|
|
||||||
if (err) {
|
|
||||||
winston.info(`[startup] NodeBB was unable to listen on: ${bind_address}:${port}`);
|
|
||||||
process.exit();
|
|
||||||
}
|
|
||||||
|
|
||||||
winston.info(`NodeBB is now listening on: ${isSocket ? socketPath : `${bind_address}:${port}`}`);
|
|
||||||
if (oldUmask) {
|
|
||||||
process.umask(oldUmask);
|
|
||||||
}
|
|
||||||
callback();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Alter umask if necessary
|
|
||||||
if (isSocket) {
|
if (isSocket) {
|
||||||
oldUmask = process.umask('0000');
|
oldUmask = process.umask('0000');
|
||||||
module.exports.testSocket(socketPath, (err) => {
|
try {
|
||||||
|
await exports.testSocket(socketPath);
|
||||||
|
} catch (err) {
|
||||||
|
winston.error(`[startup] NodeBB was unable to secure domain socket access (${socketPath})\n${err.stack}`);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
server.listen(...args.concat([function (err) {
|
||||||
|
const onText = `${isSocket ? socketPath : `${bind_address}:${port}`}`;
|
||||||
if (err) {
|
if (err) {
|
||||||
winston.error(`[startup] NodeBB was unable to secure domain socket access (${socketPath})\n${err.stack}`);
|
winston.info(`[startup] NodeBB was unable to listen on: ${onText}`);
|
||||||
throw err;
|
reject(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
server.listen(...args);
|
winston.info(`NodeBB is now listening on: ${onText}`);
|
||||||
});
|
if (oldUmask) {
|
||||||
} else {
|
process.umask(oldUmask);
|
||||||
server.listen(...args);
|
}
|
||||||
}
|
resolve();
|
||||||
|
}]));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.testSocket = function (socketPath, callback) {
|
exports.testSocket = async function (socketPath) {
|
||||||
if (typeof socketPath !== 'string') {
|
if (typeof socketPath !== 'string') {
|
||||||
return callback(new Error(`invalid socket path : ${socketPath}`));
|
throw new Error(`invalid socket path : ${socketPath}`);
|
||||||
}
|
}
|
||||||
const net = require('net');
|
const net = require('net');
|
||||||
const file = require('./file');
|
const file = require('./file');
|
||||||
async.series([
|
const exists = await file.exists(socketPath);
|
||||||
function (next) {
|
if (!exists) {
|
||||||
file.exists(socketPath, (err, exists) => {
|
return;
|
||||||
if (exists) {
|
}
|
||||||
next();
|
return new Promise((resolve, reject) => {
|
||||||
} else {
|
const testSocket = new net.Socket();
|
||||||
callback(err);
|
testSocket.on('error', (err) => {
|
||||||
}
|
if (err.code !== 'ECONNREFUSED') {
|
||||||
|
return reject(err);
|
||||||
|
}
|
||||||
|
// The socket was stale, kick it out of the way
|
||||||
|
fs.unlink(socketPath, (err) => {
|
||||||
|
if (err) reject(err); else resolve();
|
||||||
});
|
});
|
||||||
},
|
});
|
||||||
function (next) {
|
testSocket.connect({ path: socketPath }, () => {
|
||||||
const testSocket = new net.Socket();
|
// Something's listening here, abort
|
||||||
testSocket.on('error', (err) => {
|
reject(new Error('port-in-use'));
|
||||||
next(err.code !== 'ECONNREFUSED' ? err : null);
|
});
|
||||||
});
|
});
|
||||||
testSocket.connect({ path: socketPath }, () => {
|
|
||||||
// Something's listening here, abort
|
|
||||||
callback(new Error('port-in-use'));
|
|
||||||
});
|
|
||||||
},
|
|
||||||
async.apply(fs.unlink, socketPath), // The socket was stale, kick it out of the way
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
require('./promisify')(exports);
|
require('./promisify')(exports);
|
||||||
|
|||||||
Reference in New Issue
Block a user