2014-03-07 01:37:43 +01:00
|
|
|
module.exports = function (grunt) {
|
2015-01-21 20:13:56 +01:00
|
|
|
var config = require('./utils/server-config');
|
2015-01-21 19:16:14 +01:00
|
|
|
|
2015-01-14 22:46:04 +01:00
|
|
|
var maybeStartServer = function (options) {
|
|
|
|
return function () {
|
|
|
|
var http = require('http');
|
2015-01-21 19:16:14 +01:00
|
|
|
var opts = {
|
2015-01-14 22:46:04 +01:00
|
|
|
method: 'HEAD',
|
|
|
|
path: '/',
|
|
|
|
host: 'localhost',
|
|
|
|
port: options.port
|
2015-01-21 19:16:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
grunt.log.debug('checking for server', JSON.stringify(opts));
|
|
|
|
|
|
|
|
var req = http.request(opts);
|
2014-03-07 01:37:43 +01:00
|
|
|
|
2015-01-14 22:46:04 +01:00
|
|
|
function onResponse(res) {
|
2015-01-21 19:16:14 +01:00
|
|
|
grunt.log.debug('Server responded with', res.statusCode);
|
2015-01-21 19:50:24 +01:00
|
|
|
var app = res.headers['x-app-name'];
|
2015-01-21 19:16:14 +01:00
|
|
|
|
2015-01-21 19:50:24 +01:00
|
|
|
if (res.statusCode === 200 && app && app === 'kibana') {
|
2015-01-21 19:16:14 +01:00
|
|
|
grunt.log.ok('Kibana server already started on port', options.port);
|
2015-01-14 22:46:04 +01:00
|
|
|
} else {
|
2015-01-21 19:16:14 +01:00
|
|
|
grunt.log.error('Another server is already running on port', options.port);
|
2015-01-21 19:55:52 +01:00
|
|
|
process.exit(1);
|
2015-01-14 22:46:04 +01:00
|
|
|
}
|
|
|
|
done(res);
|
2014-03-07 01:37:43 +01:00
|
|
|
}
|
|
|
|
|
2015-01-21 19:16:14 +01:00
|
|
|
function onError(err) {
|
|
|
|
if (err.code !== 'ECONNREFUSED') {
|
|
|
|
grunt.log.error('Kibana server check failed', err);
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:46:04 +01:00
|
|
|
grunt.config.set(options.name, true);
|
|
|
|
grunt.task.run(options.tasks);
|
|
|
|
done();
|
|
|
|
}
|
2014-03-07 01:37:43 +01:00
|
|
|
|
2015-01-14 22:46:04 +01:00
|
|
|
var done = (function (cb) {
|
|
|
|
return function (res) {
|
|
|
|
req.removeListener('error', onError);
|
|
|
|
req.removeListener('response', onResponse);
|
|
|
|
if (res) res.socket.destroy();
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
})(this.async());
|
2014-03-07 01:37:43 +01:00
|
|
|
|
2015-01-14 22:46:04 +01:00
|
|
|
req.on('error', onError);
|
|
|
|
req.on('response', onResponse);
|
|
|
|
req.end();
|
|
|
|
};
|
|
|
|
};
|
2015-01-21 19:16:14 +01:00
|
|
|
|
2015-01-14 22:46:04 +01:00
|
|
|
grunt.registerTask('maybe_start_kibana', maybeStartServer({
|
|
|
|
name: 'kibana-server',
|
2015-01-21 19:16:14 +01:00
|
|
|
port: config.kibana.port,
|
2015-01-14 22:46:04 +01:00
|
|
|
tasks: ['kibana_server']
|
|
|
|
}));
|
|
|
|
};
|