Skip to content

Commit

Permalink
Merge pull request #5 from poelzi/master
Browse files Browse the repository at this point in the history
retry counter for crashed starts and delay
  • Loading branch information
aaronblohowiak committed Jul 21, 2011
2 parents 7d9717e + 7336c7f commit 94587a3
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions restartr
Expand Up @@ -11,6 +11,10 @@ var fs = require('fs'),
opts = parseCmdOpts(argv), opts = parseCmdOpts(argv),
cmd = opts.cmd, cmd = opts.cmd,
args = opts.args, args = opts.args,
delay_wait = opts.delay_wait ? opts.delay_wait : 3000,
max_crash_restart = opts.max_crash_restart ? opts.max_crash_restart : 5,
restart_count = 0,
restart_id = 0,
files = opts.files, files = opts.files,
crashed = false; crashed = false;


Expand All @@ -20,13 +24,19 @@ files.forEach(function(file) {


log('Files modified, restarting', 'log'); log('Files modified, restarting', 'log');
child.kill('SIGHUP'); child.kill('SIGHUP');
if (restart_id) clearTimeout(restart_id);
if (crashed) start(); if (crashed) start();
}); });
}); });


delay = function() {
restart_id = setTimeout(start, delay_wait)
}

start = function() { start = function() {
log('\nMonitoring the following files for changes:\n' + inspect(files) + '\n'); log('\nMonitoring the following files for changes:\n' + inspect(files) + '\n');
log('STARTING NEW CHILD', 'log'); log('STARTING NEW CHILD', 'log');
restart_id = 0;
child = spawn(cmd, args, {customFds: [1, 1, 1]}); child = spawn(cmd, args, {customFds: [1, 1, 1]});


var launched = (new Date).getTime(), var launched = (new Date).getTime(),
Expand All @@ -36,7 +46,18 @@ start = function() {
child.on('exit', function() { child.on('exit', function() {
log('\nCHILD EXITED: ' + pid + '\n', 'log'); log('\nCHILD EXITED: ' + pid + '\n', 'log');
crashed = (((new Date).getTime() - launched) < crashThreshhold); crashed = (((new Date).getTime() - launched) < crashThreshhold);
crashed ? log('process crashed, waiting for file change', 'warn') : start(); if(crashed) {
restart_count += 1;
if(restart_count < max_crash_restart) {
log('process crashed. Retry count: ' + restart_count, 'warn');
delay();
} else {
log('process crashed, waiting for file change', 'warn');
}
} else {
restart_count = 0;
start();
}
}); });
}; };


Expand Down Expand Up @@ -145,4 +166,4 @@ function log(msg, level) {
} }
}; };


start(); start();

0 comments on commit 94587a3

Please sign in to comment.