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'),
|
2016-10-17 15:50:29 +03:00
|
|
|
KnexMigrator = require('knex-migrator'),
|
|
|
|
config = require('../../server/config'),
|
|
|
|
knexMigrator = new KnexMigrator();
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new fork of Ghost process with a given config
|
|
|
|
// Useful for tests that want to verify certain config options
|
2016-10-06 22:40:01 +03:00
|
|
|
function forkGhost(newConfig) {
|
2016-10-11 15:37:11 +03:00
|
|
|
var port;
|
|
|
|
|
2016-09-13 21:23:22 +03:00
|
|
|
return findFreePort()
|
2016-10-11 15:37:11 +03:00
|
|
|
.then(function (_port) {
|
|
|
|
port = _port;
|
|
|
|
|
2017-03-09 22:38:20 +03:00
|
|
|
return knexMigrator.reset();
|
|
|
|
})
|
|
|
|
.then(function () {
|
2016-10-17 15:50:29 +03:00
|
|
|
return knexMigrator.init();
|
2016-10-11 15:37:11 +03:00
|
|
|
})
|
|
|
|
.then(function () {
|
2016-09-13 21:23:22 +03:00
|
|
|
newConfig.server = _.merge({}, {
|
|
|
|
port: port
|
|
|
|
}, (newConfig.server || {}));
|
|
|
|
|
|
|
|
if (newConfig.url) {
|
|
|
|
newConfig.url = url.format(_.extend({}, url.parse(newConfig.url), {port: newConfig.server.port, host: null}));
|
|
|
|
} else {
|
|
|
|
newConfig.url = url.format(_.extend({}, url.parse(config.get('url')), {port: newConfig.server.port, host: null}));
|
|
|
|
}
|
2014-08-17 10:17:23 +04:00
|
|
|
|
2016-10-04 18:33:43 +03:00
|
|
|
newConfig.logging = {
|
|
|
|
level: 'fatal',
|
|
|
|
transports: ['stdout'],
|
|
|
|
rotation: false
|
|
|
|
};
|
2016-09-13 21:23:22 +03:00
|
|
|
|
2016-10-06 22:40:01 +03:00
|
|
|
var newConfigFile = path.join(config.get('paths').appRoot, 'config.' + config.get('env') + '.json');
|
2014-08-17 10:17:23 +04:00
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
2016-09-13 21:23:22 +03:00
|
|
|
fs.writeFile(newConfigFile, 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;
|
|
|
|
};
|
|
|
|
|
2016-10-06 22:40:01 +03:00
|
|
|
env.NODE_ENV = config.get('env');
|
2016-09-13 21:23:22 +03:00
|
|
|
child = cp.fork(path.join(config.get('paths').appRoot, 'index.js'), {env: env});
|
|
|
|
|
2014-11-23 20:49:47 +03:00
|
|
|
// return the port to make it easier to do requests
|
2016-09-13 21:23:22 +03:00
|
|
|
child.port = newConfig.server.port;
|
|
|
|
|
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 () {
|
2016-09-13 21:23:22 +03:00
|
|
|
var socket = net.connect(newConfig.server.port);
|
2014-09-03 19:42:55 +04:00
|
|
|
socket.on('connect', function () {
|
2014-08-17 10:17:23 +04:00
|
|
|
socket.end();
|
2016-10-11 15:37:11 +03:00
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
if (pingStop()) {
|
|
|
|
resolve(child);
|
|
|
|
}
|
|
|
|
});
|
2014-09-03 19:42:55 +04:00
|
|
|
socket.on('error', function (err) {
|
|
|
|
/*jshint unused:false*/
|
|
|
|
pingTries = pingTries + 1;
|
2016-10-11 15:37:11 +03:00
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
// continue checking
|
2016-09-14 16:09:47 +03:00
|
|
|
if (pingTries >= 100 && pingStop()) {
|
2014-10-31 00:43:47 +03:00
|
|
|
child.kill();
|
2014-09-03 19:42:55 +04:00
|
|
|
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;
|
2014-10-31 00:43:47 +03:00
|
|
|
|
|
|
|
fs.unlink(newConfigFile, function () {
|
|
|
|
// swallow any errors -- file may not exist if fork() failed
|
|
|
|
});
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
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-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;
|