Skip to content

Commit

Permalink
Prevent repeating startup errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zerious committed Jul 16, 2014
1 parent 678edab commit c39e153
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
53 changes: 46 additions & 7 deletions commands/start.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
var shellify = require('shellify');
var spawn = require('child_process').spawn;
var Duplex = require('stream').Duplex;
var util = require('util');
var previousStart = new Date(0);

// TODO: Make these thresholds configurable.
var restartDelay = 500; // Try to restart in half a second
var okTime = 2e3; // Call a restart "ok" if it goes 2 seconds without failing.

module.exports = function (env) {

Expand All @@ -8,19 +15,51 @@ module.exports = function (env) {
*/
function start() {

// Keep track of our start time.
var started = new Date();
var now = new Date();
var elapsed = now - previousStart;
previousStart = now;

// If it's been a while since we restarted, call this a clean start.
var isCleanStart = elapsed > okTime;

// Spawn a child process and make it output to stdout.
var child = spawn(process.execPath, ['app'], {env: {NODE_ENV: env}});
child.stderr.pipe(process.stdout);
child.stdout.pipe(process.stdout);

var pipeToStdout = function () {
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stdout);
};

// When starting cleanly, pipe child process output directly to stdout.
if (isCleanStart) {
pipeToStdout();
}

// After a fast failure, use a buffer in case we fail again.
else {
var isBuffering = true;
var data = '\n';

var append = function (chunk) {
data += chunk;
};

child.on('data', append);

// When we've started ok, write data to stdout and start piping.
var okTimer = setTimeout(function () {
process.stdout.write(data);
pipeToStdout();
child.removeListener('data', append);
data = null;
}, okTime);
}

// Restart once a second at most.
child.on('close', function () {
var elapsed = new Date() - started;
delay = Math.max(1e3 - elapsed, 0);
setTimeout(start, delay);
process.stdout.write('\u001b[31m.\u001b[39m');
clearTimeout(okTimer);
setTimeout(start, restartDelay);
});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ module.exports = Class.extend({
var art = app.config.asciiArt || ['',
red + bold + ' .A. ' + grey + (" _ _ _ _ " + normal + "v" + require('../package.json').version),
red + bold + ' /@@@\\ ' + grey + " | | (_) __ _| |__ | |_ ___ _ __",
red + ' ./@@' + yellow + 'A' + red + '@@\\. ' + grey + " | | | |/ _` | '_ \\| __/ _ \\ '__|",
red + ' ./@@' + yellow + 'A' + red + '@@\\. ' + grey + " | | | |/ _` | '_ \\| __/ _ \\ '__)",
red + ' /@@' + yellow + '/@@@\\' + red + '@@\\ ' + grey + " | |__| | (_| | | | | || __/ |",
red + '/@@' + yellow + '/@@' + white + 'A' + yellow + '@@\\' + red + '@@\\' + grey + " |____|_|\\__, |_| |_|\\__\\___|_|",
red + '#@@' + yellow + '#@' + white + '/@\\' + yellow + '@#' + red + '@@#' + grey + " |___/",
red + '#@@' + yellow + '#@' + white + '/@\\' + yellow + '@#' + red + '@@#' + grey + " (___/",
red + '#@@' + yellow + '#@' + white + '@@@' + yellow + '@#' + red + '@@# ',
'"#@@' + yellow + '\\@@@/' + red + '@@#" ' + normal,
red + bold + ' \'"#######"\' ' + normal,
Expand Down

0 comments on commit c39e153

Please sign in to comment.