2014-02-22 05:25:31 +04:00
|
|
|
var cp = require('child_process'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
fs = require('fs'),
|
|
|
|
url = require('url'),
|
|
|
|
net = require('net'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2014-02-22 05:25:31 +04:00
|
|
|
path = require('path'),
|
|
|
|
config = require('../../server/config');
|
2014-09-10 08:06:24 +04:00
|
|
|
|
2014-02-22 05:25:31 +04:00
|
|
|
function findFreePort(port) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
if (typeof port === 'string') {
|
|
|
|
port = parseInt(port);
|
2014-02-22 05:25:31 +04:00
|
|
|
}
|
2014-08-17 10:17:23 +04:00
|
|
|
|
|
|
|
if (typeof port !== 'number') {
|
|
|
|
port = 2368;
|
|
|
|
}
|
|
|
|
|
|
|
|
port = port + 1;
|
|
|
|
|
|
|
|
var server = net.createServer();
|
|
|
|
|
2014-09-03 19:42:55 +04:00
|
|
|
server.on('error', function (e) {
|
2014-08-17 10:17:23 +04:00
|
|
|
if (e.code === 'EADDRINUSE') {
|
|
|
|
resolve(findFreePort(port));
|
|
|
|
} else {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-09-03 19:42:55 +04:00
|
|
|
server.listen(port, function () {
|
2014-08-17 10:17:23 +04:00
|
|
|
var listenPort = server.address().port;
|
2014-09-03 19:42:55 +04:00
|
|
|
server.close(function () {
|
2014-08-17 10:17:23 +04:00
|
|
|
resolve(listenPort);
|
|
|
|
});
|
2014-02-22 05:25:31 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a copy of current config object from file, to be modified before
|
|
|
|
// passing to forkGhost() method
|
|
|
|
function forkConfig() {
|
|
|
|
// require caches values, and we want to read it fresh from the file
|
2014-07-17 18:33:21 +04:00
|
|
|
delete require.cache[config.paths.config];
|
|
|
|
return _.cloneDeep(require(config.paths.config)[process.env.NODE_ENV]);
|
2014-02-22 05:25:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new fork of Ghost process with a given config
|
|
|
|
// Useful for tests that want to verify certain config options
|
|
|
|
function forkGhost(newConfig, envName) {
|
|
|
|
envName = envName || 'forked';
|
2014-08-17 10:17:23 +04:00
|
|
|
|
|
|
|
return findFreePort(newConfig.server ? newConfig.server.port : undefined)
|
2014-09-03 19:42:55 +04:00
|
|
|
.then(function (port) {
|
2014-02-22 05:25:31 +04:00
|
|
|
newConfig.server = newConfig.server || {};
|
|
|
|
newConfig.server.port = port;
|
|
|
|
newConfig.url = url.format(_.extend(url.parse(newConfig.url), {port: port, host: null}));
|
2014-08-17 10:17:23 +04:00
|
|
|
|
2014-07-17 18:33:21 +04:00
|
|
|
var newConfigFile = path.join(config.paths.appRoot, 'config.test' + port + '.js');
|
2014-08-17 10:17:23 +04:00
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
2014-09-03 19:42:55 +04:00
|
|
|
fs.writeFile(newConfigFile, 'module.exports = {' + envName + ': ' + JSON.stringify(newConfig) + '}', function (err) {
|
2014-08-17 10:17:23 +04:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
2014-02-22 05:25:31 +04:00
|
|
|
}
|
2014-08-17 10:17:23 +04:00
|
|
|
|
|
|
|
// setup process environment for the forked Ghost to use the new config file
|
2014-09-03 19:42:55 +04:00
|
|
|
var env = _.clone(process.env),
|
|
|
|
baseKill,
|
|
|
|
child,
|
|
|
|
pingTries = 0,
|
|
|
|
pingCheck,
|
|
|
|
pingStop = function () {
|
|
|
|
if (pingCheck) {
|
|
|
|
clearInterval(pingCheck);
|
|
|
|
pingCheck = undefined;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
env.GHOST_CONFIG = newConfigFile;
|
|
|
|
env.NODE_ENV = envName;
|
2014-09-03 19:42:55 +04:00
|
|
|
child = cp.fork(path.join(config.paths.appRoot, 'index.js'), {env: env});
|
2014-08-17 10:17:23 +04:00
|
|
|
// periodic check until forked Ghost is running and is listening on the port
|
2014-09-03 19:42:55 +04:00
|
|
|
pingCheck = setInterval(function () {
|
2014-08-17 10:17:23 +04:00
|
|
|
var socket = net.connect(port);
|
2014-09-03 19:42:55 +04:00
|
|
|
socket.on('connect', function () {
|
2014-08-17 10:17:23 +04:00
|
|
|
socket.end();
|
|
|
|
if (pingStop()) {
|
|
|
|
resolve(child);
|
|
|
|
}
|
|
|
|
});
|
2014-09-03 19:42:55 +04:00
|
|
|
socket.on('error', function (err) {
|
|
|
|
/*jshint unused:false*/
|
|
|
|
pingTries = pingTries + 1;
|
2014-08-17 10:17:23 +04:00
|
|
|
// continue checking
|
2014-09-03 19:42:55 +04:00
|
|
|
if (pingTries >= 20 && pingStop()) {
|
|
|
|
reject(new Error('Timed out waiting for child process'));
|
2014-08-17 10:17:23 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 200);
|
|
|
|
|
2014-09-03 19:42:55 +04:00
|
|
|
child.on('exit', function (code, signal) {
|
|
|
|
/*jshint unused:false*/
|
2014-08-17 10:17:23 +04:00
|
|
|
child.exited = true;
|
|
|
|
if (pingStop()) {
|
2014-09-03 19:42:55 +04:00
|
|
|
reject(new Error('Child process exit code: ' + code));
|
2014-02-22 05:25:31 +04:00
|
|
|
}
|
2014-08-17 10:17:23 +04:00
|
|
|
// cleanup the temporary config file
|
|
|
|
fs.unlink(newConfigFile);
|
2014-02-22 05:25:31 +04:00
|
|
|
});
|
2014-09-10 08:06:24 +04:00
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
// override kill() to have an async callback
|
2014-09-03 19:42:55 +04:00
|
|
|
baseKill = child.kill;
|
|
|
|
child.kill = function (signal, cb) {
|
2014-08-17 10:17:23 +04:00
|
|
|
if (typeof signal === 'function') {
|
|
|
|
cb = signal;
|
|
|
|
signal = undefined;
|
|
|
|
}
|
2014-02-22 05:25:31 +04:00
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
if (cb) {
|
2014-09-03 19:42:55 +04:00
|
|
|
child.on('exit', function () {
|
2014-08-17 10:17:23 +04:00
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
2014-02-22 05:25:31 +04:00
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
if (child.exited) {
|
|
|
|
process.nextTick(cb);
|
|
|
|
} else {
|
|
|
|
baseKill.apply(child, [signal]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-02-22 05:25:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.ghost = forkGhost;
|
|
|
|
module.exports.config = forkConfig;
|