attempt to clear up server dev server messaging

This commit is contained in:
spalger 2015-08-10 20:05:14 -07:00
parent 9874366e4b
commit 3bbf036b29
4 changed files with 48 additions and 18 deletions

View file

@ -112,7 +112,7 @@ module.exports = class ClusterManager {
}
onWatcherError(err) {
this.log.bad('Failed to watch files!\n', err.stack);
this.log.bad('failed to watch files!\n', err.stack);
process.exit(1); // eslint-disable-line no-process-exit
}
};

View file

@ -14,6 +14,15 @@ cluster.setupMaster({
silent: false
});
let dead = fork => {
return fork.isDead() || fork.killed;
};
let kill = fork => {
fork.kill('SIGINT'); // make it snappy
fork.killed = true;
};
module.exports = class Worker extends EventEmitter {
constructor(opts) {
opts = opts || {};
@ -23,6 +32,7 @@ module.exports = class Worker extends EventEmitter {
this.type = opts.type;
this.title = opts.title || opts.type;
this.watch = (opts.watch !== false);
this.startCount = 0;
this.online = false;
this.changes = [];
@ -47,6 +57,7 @@ module.exports = class Worker extends EventEmitter {
if (code) {
this.log.bad(`${this.title} crashed`, 'with status code', code);
if (!this.watch) process.exit(code);
} else {
// restart after graceful shutdowns
this.start();
@ -60,8 +71,8 @@ module.exports = class Worker extends EventEmitter {
}
shutdown() {
if (this.fork && !this.fork.isDead()) {
this.fork.kill();
if (this.fork && !dead(this.fork)) {
kill(this.fork);
this.fork.removeListener('message', this.onMessage);
this.fork.removeListener('online', this.onOnline);
this.fork.removeListener('disconnect', this.onDisconnect);
@ -90,15 +101,14 @@ module.exports = class Worker extends EventEmitter {
}
start() {
if (this.fork && !this.fork.isDead()) {
// once "exit" event is received with 0 status, start() is called again
return this.shutdown();
}
// once "exit" event is received with 0 status, start() is called again
if (this.fork) return this.shutdown();
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`);
if (this.changes.length) {
this.log.warn(`restarting ${this.title}`, `due to changes in ${this.flushChangeBuffer()}`);
}
else if (this.startCount++) {
this.log.warn(`restarting ${this.title}...`);
}
this.fork = cluster.fork(this.env);

View file

@ -19,14 +19,23 @@ module.exports = async (kbnServer, kibanaHapiServer, config) => {
})
);
await server.init();
let ready = false;
let sendReady = () => {
process.send(['WORKER_BROADCAST', { optimizeReady: true }]);
if (!process.connected) return;
process.send(['WORKER_BROADCAST', { optimizeReady: ready }]);
};
sendReady();
process.on('message', (msg) => {
if (msg && msg.optimizeReady === '?') sendReady();
});
sendReady();
await server.init();
ready = true;
sendReady();
};

View file

@ -1,5 +1,5 @@
let { fromNode } = require('bluebird');
let { get } = require('lodash');
let { get, once } = require('lodash');
module.exports = (kbnServer, server, config) => {
@ -21,11 +21,22 @@ module.exports = (kbnServer, server, config) => {
cb(new Error('Server timedout waiting for the optimizer to become ready'));
}, config.get('optimize.lazyProxyTimeout'));
let waiting = once(() => {
server.log(['info', 'optimize'], 'Waiting for optimizer completion');
});
if (!process.connected) return;
process.send(['WORKER_BROADCAST', { optimizeReady: '?' }]);
process.on('message', (msg) => {
if (get(msg, 'optimizeReady')) {
clearTimeout(timeout);
cb();
switch (get(msg, 'optimizeReady')) {
case true:
clearTimeout(timeout);
cb();
break;
case false:
waiting();
break;
}
});
});