-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement dev live optimizer #4611
Changes from 52 commits
240242d
dc6f7f8
b3e65eb
1f95e66
70ef012
671319a
cd5b665
2fe09cb
ae91063
37b1c3a
b4fe366
12466d6
25bd4ec
7c50b7e
dd91047
6cd6399
f239b60
41ab382
f09ef37
7b0b24a
830ce37
3c2802f
98f40f6
f70720a
c0400a1
ca49a03
ea15e00
4006bbb
e79d55b
c2e169f
1c0720a
2a414b8
a651f51
57e5952
7743f46
bd68ae5
7bc40f0
6793c9f
7e0540c
82ed06f
5821ea8
68f6ca8
afc4cbd
c31109e
8c546bf
66e82d8
c5b265c
1e5c52a
7992b6c
900e50a
08b9068
e588977
893c6f8
d9d1e72
e4fe82d
2140247
a016f53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ target | |
esvm | ||
.htpasswd | ||
installedPlugins | ||
webpackstats.json | ||
config/kibana.dev.yml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
let cluster = require('cluster'); | ||
let { join } = require('path'); | ||
let { compact, invoke, bindAll, once, get } = require('lodash'); | ||
|
||
let Log = require('../Log'); | ||
let Worker = require('./Worker'); | ||
|
||
module.exports = class ClusterManager { | ||
constructor(opts) { | ||
this.log = new Log(opts.quiet, opts.silent); | ||
this.addedCount = 0; | ||
|
||
this.workers = [ | ||
new Worker({ | ||
type: 'optmzr', | ||
title: 'optimizer', | ||
log: this.log, | ||
argv: compact([ | ||
'--plugins.initialize=false', | ||
'--server.autoListen=false' | ||
]), | ||
watch: false | ||
}), | ||
|
||
new Worker({ | ||
type: 'server', | ||
log: this.log | ||
}) | ||
]; | ||
|
||
// broker messages between workers | ||
this.workers.forEach((worker) => { | ||
worker.on('broadcast', (msg) => { | ||
this.workers.forEach((to) => { | ||
if (to !== worker && to.online) { | ||
to.fork.send(msg); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
bindAll(this, 'onWatcherAdd', 'onWatcherError', 'onWatcherChange'); | ||
|
||
if (opts.watch) this.setupWatching(); | ||
else this.startCluster(); | ||
} | ||
|
||
startCluster() { | ||
invoke(this.workers, 'start'); | ||
} | ||
|
||
setupWatching() { | ||
var chokidar = require('chokidar'); | ||
let utils = require('requirefrom')('src/utils'); | ||
let fromRoot = utils('fromRoot'); | ||
|
||
this.watcher = chokidar.watch([ | ||
'src/plugins', | ||
'src/server', | ||
'src/ui', | ||
'src/utils', | ||
'config', | ||
'installedPlugins' | ||
], { | ||
cwd: fromRoot('.'), | ||
ignored: /[\\\/](node_modules|bower_components|public)[\\\/]/, | ||
}); | ||
|
||
this.watcher.on('add', this.onWatcherAdd); | ||
this.watcher.on('error', this.onWatcherError); | ||
|
||
this.watcher.on('ready', once(() => { | ||
// start sending changes to workers | ||
this.watcher.removeListener('add', this.onWatcherAdd); | ||
this.watcher.on('all', this.onWatcherChange); | ||
|
||
this.log.good('Watching for changes', `(${this.addedCount} files)`); | ||
this.startCluster(); | ||
})); | ||
} | ||
|
||
onWatcherAdd() { | ||
this.addedCount += 1; | ||
} | ||
|
||
onWatcherChange(e, path) { | ||
invoke(this.workers, 'onChange', path); | ||
} | ||
|
||
onWatcherError(err) { | ||
this.log.bad('Failed to watch files!\n', err.stack); | ||
process.exit(1); // eslint-disable-line no-process-exit | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,8 @@ let { isWorker } = require('cluster'); | |
let { resolve } = require('path'); | ||
|
||
let cwd = process.cwd(); | ||
let readYamlConfig = require('./readYamlConfig'); | ||
let src = require('requirefrom')('src'); | ||
let fromRoot = src('utils/fromRoot'); | ||
let KbnServer = src('server/KbnServer'); | ||
|
||
let pathCollector = function () { | ||
let paths = []; | ||
|
@@ -51,20 +49,31 @@ module.exports = function (program) { | |
) | ||
.option('--plugins <path>', 'an alias for --plugin-dir', pluginDirCollector) | ||
.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')(opts); | ||
.option('--no-watch', 'Prevents automatic restarts of the server in --dev mode') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is being used anywhere now - can we just drop no-watch from the optimizer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. --no-watch is still being used, but the optimize.watch config option is not. --no-watch is used to prevent restarting the server. |
||
.action(async function (opts) { | ||
if (opts.dev && !isWorker) { | ||
// stop processing the action and handoff to cluster manager | ||
let ClusterManager = require('../cluster/ClusterManager'); | ||
new ClusterManager(opts); | ||
return; | ||
} | ||
|
||
let readYamlConfig = require('./readYamlConfig'); | ||
let KbnServer = src('server/KbnServer'); | ||
|
||
let settings = readYamlConfig(opts.config || fromRoot('config/kibana.yml')); | ||
|
||
if (opts.dev) { | ||
try { _.merge(settings, readYamlConfig(fromRoot('config/kibana.dev.yml'))); } | ||
catch (e) { null; } | ||
} | ||
|
||
let set = _.partial(_.set, settings); | ||
let get = _.partial(_.get, settings); | ||
|
||
if (opts.dev) { | ||
set('env', 'development'); | ||
set('optimize.watch', opts.watch); | ||
set('optimize.watch', true); | ||
} | ||
|
||
if (opts.elasticsearch) set('elasticsearch.url', opts.elasticsearch); | ||
|
@@ -82,13 +91,22 @@ module.exports = function (program) { | |
|
||
set('plugins.paths', [].concat(opts.pluginPath || [])); | ||
|
||
let server = new KbnServer(_.merge(settings, this.getUnknownOptions())); | ||
let kbnServer = {}; | ||
|
||
server.ready().catch(function (err) { | ||
console.error(err.stack); | ||
try { | ||
kbnServer = new KbnServer(_.merge(settings, this.getUnknownOptions())); | ||
await kbnServer.ready(); | ||
} | ||
catch (err) { | ||
let { server } = kbnServer; | ||
|
||
if (server) server.log(['fatal'], err); | ||
else console.error('FATAL', err); | ||
|
||
kbnServer.close(); | ||
process.exit(1); // eslint-disable-line no-process-exit | ||
}); | ||
} | ||
|
||
return server; | ||
return kbnServer; | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're dropping periods, this should probably be comma separated