Skip to content

Commit

Permalink
make devmode support --silent and --quiet
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Jul 16, 2015
1 parent 5eb9bed commit eb31780
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 75 deletions.
11 changes: 8 additions & 3 deletions src/cli/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ let log = _.restParam(function (color, label, rest1) {
});

let color = require('./color');
exports.green = _.partial(log, color.green);
exports.red = _.partial(log, color.red);
exports.yellow = _.partial(log, color.yellow);

module.exports = class Log {
constructor(quiet, silent) {
this.good = quiet || silent ? _.noop : _.partial(log, color.green);
this.warn = quiet || silent ? _.noop : _.partial(log, color.yellow);
this.bad = silent ? _.noop : _.partial(log, color.red);
}
};
10 changes: 6 additions & 4 deletions src/commands/serve/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ module.exports = function (program) {
.option('-e, --elasticsearch <uri>', 'Elasticsearch instance')
.option('-c, --config <path>', 'Path to the config file')
.option('-p, --port <port>', 'The port to bind to', parseInt)
.option('-q, --quiet', 'Turns off logging')
.option('-q, --quiet', 'Prevent all logging except errors')
.option('-Q, --silent', 'Prevent all logging')
.option('--verbose', 'Turns on verbose logging')
.option('-H, --host <host>', 'The host to bind to')
.option('-l, --log-file <path>', 'The file to log to')
Expand All @@ -53,10 +54,9 @@ module.exports = function (program) {
.option('--dev', 'Run the server with development mode defaults')
.option('--no-watch', 'Prevent watching, use with --dev to prevent server restarts')
.action(function (opts) {

if (opts.dev && opts.watch && !isWorker) {
// stop processing the action and handoff to watch cluster manager
return require('../watch/watch');
return require('../watch/watch')(opts);
}

let settings = readYamlConfig(opts.config || fromRoot('config/kibana.yml'));
Expand All @@ -71,7 +71,9 @@ module.exports = function (program) {
if (opts.elasticsearch) set('elasticsearch.url', opts.elasticsearch);
if (opts.port) set('server.port', opts.port);
if (opts.host) set('server.host', opts.host);
if (opts.quiet) set('logging.quiet', opts.quiet);
if (opts.quiet) set('logging.quiet', true);
if (opts.silent) set('logging.silent', true);
if (opts.verbose) set('logging.verbose', true);
if (opts.logFile) set('logging.dest', opts.logFile);

set('plugins.scanDirs', _.compact([].concat(
Expand Down
36 changes: 17 additions & 19 deletions src/commands/watch/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ let cluster = require('cluster');
let resolve = require('path').resolve;
let EventEmitter = require('events').EventEmitter;

let log = require('../../cli/log');
let fromRoot = require('../../utils/fromRoot');

let cliPath = fromRoot('src/cli/cli.js');
Expand All @@ -22,14 +21,16 @@ module.exports = class Worker extends EventEmitter {
opts = opts || {};
super();

this.log = opts.log;
this.type = opts.type;
this.title = opts.title || opts.type;
this.filter = opts.filter || _.constant(true);
this.changeBuffer = [];
this.changes = [];

let argv = _.union(baseArgv, opts.argv || []);
this.env = {
kbnWorkerType: this.type,
kbnWorkerArgv: JSON.stringify(baseArgv.concat(opts.args || []))
kbnWorkerArgv: JSON.stringify(argv)
};

_.bindAll(this, ['onExit', 'onMessage', 'start']);
Expand All @@ -40,13 +41,13 @@ module.exports = class Worker extends EventEmitter {

onExit(fork, code) {
if (this.fork !== fork) return;
if (code) log.red(`${this.title} crashed`, 'with status code', code);
if (code) this.log.bad(`${this.title} crashed`, 'with status code', code);
else this.start(); // graceful shutdowns happen on restart
}

onChange(path) {
if (!this.filter(path)) return;
this.changeBuffer.push(path);
this.changes.push(path);
this.start();
}

Expand All @@ -56,28 +57,25 @@ module.exports = class Worker extends EventEmitter {
}

flushChangeBuffer() {
let files = _.unique(this.changeBuffer.splice(0));
let files = _.unique(this.changes.splice(0));
let prefix = files.length > 1 ? '\n - ' : '';
return files.reduce(function (list, file, i, files) {
return `${list || ''}${prefix}"${file}"`;
}, '');
}

start() {
if (this.fork) {
if (!this.fork.isDead()) {
this.fork.kill();
this.fork.removeListener('message', this.onMessage);
// once "exit" event is received with 0 status, start() is called again
return;
}

if (this.changeBuffer.length) {
log.yellow(`${this.title} restarting`, `due to changes in ${this.flushChangeBuffer()}`);
}
if (this.fork && !this.fork.isDead()) {
this.fork.kill();
this.fork.removeListener('message', this.onMessage);
// once "exit" event is received with 0 status, start() is called again
return;
}
else {
log.yellow(`${this.title} starting`);

if (this.fork && this.changes.length) {
this.log.warn(`${this.title} restarting`, `due to changes in ${this.flushChangeBuffer()}`);
} else {
this.log.warn(`${this.title} starting`);
}

this.fork = cluster.fork(this.env);
Expand Down
105 changes: 57 additions & 48 deletions src/commands/watch/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,74 @@

let cluster = require('cluster');
let join = require('path').join;

let _ = require('lodash');
let Gaze = require('gaze').Gaze;

let log = require('../../cli/log');
let Log = require('../../cli/Log');
let fromRoot = require('../../utils/fromRoot');

let Worker = require('./Worker');

let gaze = new Gaze([
'src/{cli,commands,server,utils}/**/*',
'src/plugins/*/*', // files at the root of a plugin
'src/plugins/*/lib/**/*', // files within a lib directory for a plugin
'config/**/*',
], {
cwd: fromRoot('.'),
debounceDelay: 200
});
module.exports = function (opts) {

let gaze = new Gaze([
'src/{cli,commands,server,utils}/**/*',
'src/plugins/*/*', // files at the root of a plugin
'src/plugins/*/lib/**/*', // files within a lib directory for a plugin
'config/**/*',
], {
cwd: fromRoot('.'),
debounceDelay: 200
});

let log = new Log(opts.quiet, opts.silent);
let customLogging = opts.quiet || opts.silent || opts.verbose;

let workers = [
new Worker(gaze, {
type: 'optmzr',
title: 'optimizer',
args: [
'--logging.quiet=true',
'--plugins.initialize=false',
'--server.autoListen=false',
'--optimize._workerRole=send'
],
filter: function (path) {
return /\/src\/server\/optimize\//.test(path);
}
}),
let workers = [
new Worker(gaze, {
type: 'optmzr',
title: 'optimizer',
log: log,
argv: _.compact([
customLogging ? null : '--quiet',
'--plugins.initialize=false',
'--server.autoListen=false',
'--optimize._workerRole=send'
]),
filter: function (path) {
return /\/src\/server\/optimize\//.test(path);
}
}),

new Worker(gaze, {
type: 'server',
args: ['--optimize._workerRole=receive']
})
];
new Worker(gaze, {
type: 'server',
log: log,
argv: [
'--optimize._workerRole=receive'
]
})
];

workers.forEach(function (worker) {
worker.on('broadcast', function (msg) {
workers.forEach(function (broadcastTo) {
if (broadcastTo === worker) return;
broadcastTo.fork.send(msg);
workers.forEach(function (worker) {
worker.on('broadcast', function (msg) {
workers.forEach(function (broadcastTo) {
if (broadcastTo === worker) return;
broadcastTo.fork.send(msg);
});
});
});
});

gaze.on('all', function (e, path) {
_.invoke(workers, 'onChange', path);
});
gaze.on('all', function (e, path) {
_.invoke(workers, 'onChange', path);
});

gaze.on('ready', function (watcher) {
log.good('Watching for changes', `(${_.size(watcher.watched())} files)`);
_.invoke(workers, 'start');
});

gaze.on('ready', function (watcher) {
log.green('Watching for changes', `(${_.size(watcher.watched())} files)`);
_.invoke(workers, 'start');
});
gaze.on('error', function (err) {
log.bad('Failed to watch files!\n', err.stack);
process.exit(1);
});

gaze.on('error', function (err) {
log.red('Failed to watch files!\n', err.stack);
process.exit(1);
});
};
2 changes: 1 addition & 1 deletion src/server/logging/LogFormatString.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let workerType = process.env.kbnWorkerType ? `${type(process.env.kbnWorkerType)}

module.exports = class KbnLoggerJsonFormat extends LogFormat {
format(data) {
let time = color('time')(moment(data.timestamp).format('LTS'));
let time = color('time')(moment(data.timestamp).format('HH:mm:ss.SSS'));
let msg = data.error ? color('error')(data.error.stack) : color('message')(data.message);

let tags = _(data.tags)
Expand Down

0 comments on commit eb31780

Please sign in to comment.