Skip to content
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

Merged
merged 57 commits into from
Aug 11, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
240242d
Merge branch 'lint/es7Support' into implement/simpleAndLazyOptimizer
Aug 5, 2015
dc6f7f8
Merge branch 'fix/serverBabelOpts' into implement/simpleAndLazyOptimizer
Aug 5, 2015
b3e65eb
Merge branch 'tests/addTimeoutToKbnServerStart' into implement/simple…
Aug 5, 2015
1f95e66
Merge branch 'master' of github.com:elastic/kibana into implement/sim…
Aug 5, 2015
70ef012
Move from watching optimizer to live optimizer
Aug 7, 2015
671319a
Merge branch 'master' of github.com:elastic/kibana into implement/dev…
Aug 7, 2015
cd5b665
fixed the test bundling
Aug 7, 2015
2fe09cb
do not require lazy config option
Aug 7, 2015
ae91063
add no-watch option back to the cli as no-dev-watch
Aug 7, 2015
37b1c3a
added profiling capability and Commons chunk
Aug 7, 2015
b4fe366
inline babel runtime and preload some visualize modules
Aug 7, 2015
12466d6
update babel
Aug 7, 2015
25bd4ec
switch back to a file based optimizer in dev mode
Aug 7, 2015
7c50b7e
restore the loading screen
Aug 7, 2015
dd91047
move babel compilation to .babel.js files
Aug 8, 2015
6cd6399
fix the optimize logging message
Aug 8, 2015
f239b60
when optimizing tests, only optimize tests by default
Aug 8, 2015
41ab382
improve karma support
Aug 9, 2015
f09ef37
support exporting multiple apps
Aug 9, 2015
7b0b24a
Merge branch 'master' of github.com:elastic/kibana into implement/dev…
Aug 9, 2015
830ce37
better match the previous loading screen
Aug 9, 2015
3c2802f
fix cors config for karma
Aug 9, 2015
98f40f6
clean up error propogation and reporting
Aug 10, 2015
f70720a
improve the lazy optimizers handling of edge cases
Aug 10, 2015
c0400a1
english
Aug 10, 2015
ca49a03
ensure that cli actions don't swallow promises
Aug 10, 2015
ea15e00
wait for the lazy optimizer before initializing the lazy proxy
Aug 10, 2015
4006bbb
with watching in the lazy optimizer, prebuild by default
Aug 10, 2015
e79d55b
force the installed plugins directory into git
Aug 10, 2015
c2e169f
--no-dev-watch isn't intuitive
Aug 10, 2015
1c0720a
goodbye mocha timeouts
Aug 10, 2015
2a414b8
transition to all on non-punctuated log messages
Aug 10, 2015
a651f51
read correct watch option
Aug 10, 2015
57e5952
rename Plugins class to PluginCollection
Aug 10, 2015
7743f46
commit to the Class < ClassCollection style
Aug 10, 2015
bd68ae5
Sets can not be accessed by index
Aug 10, 2015
7bc40f0
include forward slashes in regexps
Aug 11, 2015
6793c9f
revert accidental color change
Aug 11, 2015
7e0540c
enable profiling for all optimizers
Aug 11, 2015
82ed06f
added additional preloading
Aug 11, 2015
5821ea8
expose unsafeCache setting, which accelerates build time significatly
Aug 11, 2015
68f6ca8
save
Aug 11, 2015
afc4cbd
fix reference to status page app
Aug 11, 2015
c31109e
Merge branch 'master' of github.com:elastic/kibana into implement/dev…
Aug 11, 2015
8c546bf
support uncommitted kibana.yml additions next to the kibana.yml file
Aug 11, 2015
66e82d8
expand unsafeCache and sourceMaps config schemas
Aug 11, 2015
c5b265c
remove .babel.js extension, compile all files with babel
Aug 11, 2015
1e5c52a
fix angular-nvd3 bundled lib
Aug 11, 2015
7992b6c
disable preload by default, but still start the build immediately
Aug 11, 2015
900e50a
only use kibana.dev.yml in development
Aug 11, 2015
08b9068
manually preload to prevent sourcemap error
Aug 11, 2015
e588977
Merge branch 'master' of github.com:elastic/kibana into implement/dev…
Aug 11, 2015
893c6f8
remove optimize.watch option, dev mode uses the lazy optimizer and pr…
Aug 11, 2015
d9d1e72
fix punctuation in log message
Aug 11, 2015
e4fe82d
older lines
Aug 11, 2015
2140247
Merge branch 'master' of github.com:elastic/kibana into implement/dev…
Aug 11, 2015
a016f53
use fs optimizer in test server
Aug 11, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
improve the lazy optimizers handling of edge cases
  • Loading branch information
spalger committed Aug 10, 2015
commit f70720a1afba42e539698b02956de85eaf8889d4
127 changes: 75 additions & 52 deletions src/optimize/lazy/LazyOptimizer.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,73 @@
let { contains } = require('lodash');
let { once, pick, size } = require('lodash');
let { join } = require('path');
let { fromNode } = require('bluebird');
let Boom = require('boom');

let FsOptimizer = require('../FsOptimizer');
let WeirdControlFlow = require('./WeirdControlFlow');

module.exports = class LazyOptimizer extends FsOptimizer {
constructor(opts) {
super(opts);
this.sent = [];
this.log = opts.log || (() => null);
this.prebuild = opts.prebuild || false;

this.timer = {
ms: null,
start: () => this.timer.ms = Date.now(),
end: () => this.timer.ms = ((Date.now() - this.timer.ms) / 1000).toFixed(2)
};

this.build = new WeirdControlFlow();
}

async init() {
this.initializing = true;

await this.bundles.writeEntryFiles();
await this.initCompiler();
if (this.prebuild) await this.start();
}

start() {
this.log(['info', 'optimize'], `Lazy Optimization for ${this.bundles.desc()} starting`);
let watching;

let start = Date.now();
let prom = this.current = (async () => {
try {
this.compiler.plugin('watch-run', (watching, webpackCb) => {
this.build.work(once(() => {
this.timer.start();
this.logRunStart();
webpackCb();
}));
});

let stats = await fromNode(cb => this.compiler.run(cb));
if (stats.hasErrors() || stats.hasWarnings()) {
let err = new Error('optimization failure');
err.stats = stats;
throw err;
}
this.compiler.plugin('done', stats => {
if (!stats.hasErrors() && !stats.hasWarnings()) {
this.logRunSuccess();
this.build.success();
return;
}

return stats;
let err = this.failedStatsToError(stats);
this.logRunFailure(err);
this.build.failure(err);
watching.invalidate();
});

} catch (e) {
if (e.stats && this.current === prom) {
this.log(['warning', 'optimize'], e.stats.toString({ colors: true }));
}
// TODO: rebuild on errors
throw e;
watching = this.compiler.watch({ aggregateTimeout: 200 }, err => {
if (err) {
this.log('fatal', err);
process.exit(1);
}
}())
.then(() => {
let seconds = ((Date.now() - start) / 1000).toFixed(2);
this.log(['info', 'optimize'], `Lazy optimization of ${this.bundles.desc()} complete in ${seconds} seconds.`);
});

return prom;
if (this.prebuild) await this.build.get();

this.initializing = false;
this.log(['info', 'optimize'], {
tmpl: `Lazy optimization ${this.bundles.desc()} ready.`,
bundles: this.bundles.getIds()
});
}

/**
* Read a file from the in-memory bundles, paths just like those
* produces by the FsOptimizer are used to access files. The first time
* a file is requested it is marked as "sent". If that same file is requested
* a second time then we will rerun the compiler.
*
* !!!ONLY ONE BROWSER TAB SHOULD ACCESS THE IN-MEMORY OPTIMIZERS FILES AT A TIME!!!
*
* @param {[type]} relativePath [description]
* @return {[type]} [description]
*/
async getPath(relativePath) {
let path = join(this.compiler.outputPath, relativePath);
let resend = contains(this.sent, path);

if (resend) {
this.sent = [];
this.current = null;
}

if (!this.current) this.start();
await this.current;

this.sent.push(path);
return path;
await this.build.get();
return join(this.compiler.outputPath, relativePath);
}

bindToServer(server) {
Expand All @@ -92,4 +85,34 @@ module.exports = class LazyOptimizer extends FsOptimizer {
}
});
}

logRunStart() {
this.log(['info', 'optimize'], {
tmpl: `Lazy optimization running.`,
bundles: this.bundles.getIds()
});
}

logRunSuccess() {
this.log(['info', 'optimize'], {
tmpl: 'Lazy optimization <%= status %> in <%= seconds %> seconds.',
bundles: this.bundles.getIds(),
status: 'success',
seconds: this.timer.end()
});
}

logRunFailure(err) {
// errors during initialization to the server, unlike the rest of the
// errors produced here. Lets not muddy the console with extra errors
if (this.initializing) return;

this.log(['fatal', 'optimize'], {
tmpl: 'Lazy optimization <%= status %> in <%= seconds %> seconds.<%= err %>',
bundles: this.bundles.getIds(),
status: 'failed',
seconds: this.timer.end(),
err: err
});
}
};
58 changes: 58 additions & 0 deletions src/optimize/lazy/WeirdControlFlow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

let { fromNode } = require('bluebird');

module.exports = class WeirdControlFlow {
constructor(work) {
this.handlers = [];
}

get() {
return fromNode(cb => {
if (this.ready) return cb();
this.handlers.push(cb);
this.start();
});
}

work(work) {
this._work = work;
this.stop();

if (this.handlers.length) {
this.start();
}
}

start() {
if (this.running) return;
this.stop();
if (this._work) {
this.running = true;
this._work();
}
}

stop() {
this.ready = false;
this.error = false;
this.running = false;
}

success(...args) {
this.stop();
this.ready = true;
this._flush(args);
}

failure(err) {
this.stop();
this.error = err;
this._flush([err]);
}

_flush(args) {
for (let fn of this.handlers.splice(0)) {
fn.apply(null, args);
}
}
};